首页 文章

从C / WinRT DLL激活

提问于
浏览
1

我试图将Windows 10通知的一般包装器编写为DLL . 我已经设法编写了一个DLL,几乎可以完成API提供的所有功能,但我无法获得移动到操作中心的通知 . 据我所知,我需要有一个注册的COM INotificationActivationCallback来通知留在行动中心,虽然我不明白为什么 .

我希望可以从较旧的MinGW编译的代码库访问此库,因此基于使用C样式函数访问C类创建了一个API . 支持实现如下,为简洁起见省略了一些错误处理和其他细节 .

using namespace winrt::Windows;
using namespace UI;


class Win10NotificationManager {
public:
    Win10NotificationManager(NotificationCallback, const wchar_t * appUserModelID, void * userdata);
    virtual ~Win10NotificationManager();

    void createNotification(const wchar_t * xml);
    void clearNotifications();

private:
    void toast_Failed(const Notifications::ToastNotification &, const Notifications::ToastFailedEventArgs & args);
    void toast_Dismissed(const Notifications::ToastNotification &, const Notifications::ToastDismissedEventArgs & args);
    void toast_Activated(const Notifications::ToastNotification &, const Foundation::IInspectable & object);

    std::wstring appUserModelID_;
    NotificationCallback cb_;
    void * userdata_;
};


Win10NotificationManager::Win10NotificationManager(NotificationCallback cb, const wchar_t * appUserModelID, void * userdata)
    : appUserModelID_(appUserModelID)
    , cb_(cb)
    , userdata_(userdata)
{
}

Win10NotificationManager::~Win10NotificationManager() {
}

void Win10NotificationManager::createNotification(const wchar_t * xml) {
    // Create an XmlDocument from string
    Xml::Dom::XmlDocument xmlDoc;
    xmlDoc.LoadXml(xml);

    // Create a toast object
    Notifications::ToastNotification toast(xmlDoc);

    // register event handlers
    toast.Dismissed(Foundation::TypedEventHandler<Notifications::ToastNotification, Notifications::ToastDismissedEventArgs>(this, &Win10NotificationManager::toast_Dismissed));
    toast.Failed(Foundation::TypedEventHandler<Notifications::ToastNotification, Notifications::ToastFailedEventArgs>(this, &Win10NotificationManager::toast_Failed));
    toast.Activated(Foundation::TypedEventHandler<Notifications::ToastNotification, Foundation::IInspectable>(this, &Win10NotificationManager::toast_Activated));

    // show
    auto notifier = Notifications::ToastNotificationManager::CreateToastNotifier(appUserModelID_);
    notifier.Show(toast);
}

void Win10NotificationManager::toast_Failed(const Notifications::ToastNotification &, const Notifications::ToastFailedEventArgs & args) {
    HRESULT hr = args.ErrorCode();
    winrt::check_hresult(hr);
}

void Win10NotificationManager::toast_Dismissed(const Notifications::ToastNotification &, const Notifications::ToastDismissedEventArgs &) {
    cb_(nullptr, userdata_);
}

void Win10NotificationManager::toast_Activated(const Notifications::ToastNotification &, const Foundation::IInspectable & object) {
    Notifications::IToastActivatedEventArgs args = winrt::unbox_value<Notifications::IToastActivatedEventArgs>(object);
    cb_(args.Arguments().begin(), userdata_);
}

void Win10NotificationManager::clearNotifications() {
    auto history = Notifications::ToastNotificationManager::History();
    history.Clear(appUserModelID_);
}

这很有效,除了提到的缺少Action Center持久性 . 由于DLL是通用的(不是我的一个应用程序特定),我想避免将COM激活烘焙到DLL中 . 我不需要在调用过程的生命周期之后保持通知,但如果通知在5秒后没有永久消失,那将会很好 .

如果需要,我可以使用Visual Studio 2017解决方案创建一个要点 .

1 回答

  • 1

    为了从桌面使用通知API,它既需要注册的COM服务器,也需要shell快捷方式 . 我不知道为什么这是必要的,但据我所知,这是让它运作的唯一方法 . 这个问题经常出现,所以我在这里写了一个简单的C / WinRT示例:

    https://gist.github.com/kennykerr/d983767262118ae0366ef1ec282e428a

    希望有所帮助 .

相关问题