首页 文章

g_object_new:从g_application_send_notification()调用断言

提问于
浏览
2

我使用安装了MSYS2的最新GTK,每当我尝试使用 g_application_send_notification() 时,它总是会产生以下断言:

(Notification Project.exe:27780): GLib-GObject-CRITICAL **: g_object_new:
assertion 'G_TYPE_IS_OBJECT (object_type)' failed

为什么我认为这是一个错误 - 因为我在矿山旁边尝试了许多代码样本(它们都非常像地雷),让它工作的人(包括Lars Uebernickel的通知)并且它们都有相同的哀叹 . 断言,然后崩溃 . 现在我真的不知道这意味着什么,因为它可能在gtk内部,但我真的希望你们中的一些人可能有这方面的线索或经验 .

  • install(GNU coreutils)8.25

  • GIO版本2.52.3

  • mingw32 / mingw-w64-i686-gtk-engine-unico 1.0.2-2 [已安装]

  • mingw32 / mingw-w64-i686-gtk3 3.22.16-1 [已安装]

  • mingw32 / mingw-w64-i686-gtkmm3 3.22.0-1 [已安装]

  • mingw32 / mingw-w64-i686-spice-gtk 0.33-1 [已安装]

  • mingw32 / mingw-w64-i686-webkitgtk3 2.4.11-4 [已安装]

  • mingw64 / mingw-w64-x86_64-gtk-engine-unico 1.0.2-2 [已安装]

  • mingw64 / mingw-w64-x86_64-gtk3 3.22.16-1 [已安装]

  • mingw64 / mingw-w64-x86_64-gtkmm3 3.22.0-1 [已安装]

  • mingw64 / mingw-w64-x86_64-spice-gtk 0.33-1 [已安装]

  • mingw64 / mingw-w64-x86_64-webkitgtk3 2.4.11-4 [已安装]


生成此断言的代码示例:

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gtk/gtk.h>

#define ICON_PATH "/path/trash_16x16.gif"

int main (int argc, char *argv[])
{
    GApplication *app;

    app = g_application_new ("org.one", G_APPLICATION_FLAGS_NONE);
    if(!app)
    {
        g_print ("Error app\n");
    }
    else
    {
        if(g_application_register (app, NULL, NULL))
        {
            GNotification *notification;
            GFile *file;
            GIcon *icon;
            notification = g_notification_new ("one");
            g_notification_set_body (notification, "Hello world");
            file = g_file_new_for_path (ICON_PATH);
            icon = g_file_icon_new (file);
            g_notification_set_icon (notification, G_ICON (icon));
            g_application_send_notification (app, NULL, notification);
            g_object_unref (icon);
            g_object_unref (file);
            g_object_unref (notification);
            g_object_unref (app);
            g_print ("yes\n");
        }
        else
        {
            g_print ("no\n");
        }
    }
    return 0;
}

我能做些什么来绕过这个问题,或者甚至可以解决它?

1 回答

  • 0

    确认后,没有通知后端可以在W32上运行 . 贡献者曾经想要创建这样的,但是所需的API(W32 toast通知)仅限COM,而MinGW-w64还没有必要的 Headers .
    enter image description here


    具体看一下glib gio源代码和GNotificationBackend接口,我们可以看到它很简单 .

    现在,您可以在GTK中执行此操作...或者我们可以以正确的方式(TM)执行此操作并为其实现D-Bus服务器 . 通知服务器的好处是,即使应用程序终止,通知也可以保留 . 此外,GTK已经拥有与通知服务器通信的后端,因此您必须使用任何在W32上无法正常工作的内容 . 此外,这样服务器将透明地使用 Shell_NotifyIcon (在Windows 7及更早版本上)或Toast通知(在Windows 8及更高版本上;如果您有机会实现这些) .

    如果要保持跨平台性,另一个选项是在DLL中创建 Shell_NotifyIcon 实现,如果检测到窗口,则使用 Shell_NotifyIcon ,如果不是,请使用GNotification .

相关问题