Toast Notifications Windows 10 with cppwinrt library

1 c++ windows-runtime

I have found an example how make a Toast Notifications in Windows 10 on c++

https://github.com/WindowsNotifications/desktop-toasts/blob/master/CPP/DesktopToastsSample.cpp

In my opinion this code has C style rather than C++ which doesn't look very good to me.

I've found a wrapper over winrt for ?++ https://github.com/Microsoft/cppwinrt

Now I am trying to write similar code like in the example but with winrt.

In example There are lines

ComPtr<IToastNotificationManagerStatics> toastStatics;
HRESULT hr = Windows::Foundation::GetActivationFactory(
   HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(),
&toastStatics);
Run Code Online (Sandbox Code Playgroud)

我开始写代码

#include "winrt/Windows.UI.Notifications.h"
#include "winrt/Windows.Foundation.h"
using namespace winrt::Windows::UI::Notifications;
using namespace winrt::Windows::Foundation;
void main() {
    auto igetaf = IGetActivationFactory();
    igetaf.GetActivationFactory(???);
Run Code Online (Sandbox Code Playgroud)
  1. 如何将字符串转换为 winrt::hstring
  2. 如何转换 RuntimeClass_Windows_UI_Notifications_ToastNotificationManager hstring

文档甚至没有说明我可以使用哪些字符串

docs.microsoft.com/en-us/uwp/api/windows.foundation.igetactivationfactory(声誉不够:()

你能给我一个工作代码的例子吗??

Ray*_*hen 5

目前有三种主要的方式从 C++ 使用 Windows 运行时类。首先是使用 ABI,这就是您发现的. 正如您所发现的,这非常麻烦。

第二个是 C++/CX,它使用^符号来表示 Windows 运行时类。您可以在 UWP 示例存储库中查看此样式的示例

第三个是 C++/WinRT。在 C++/WinRT 中,对象直接表示为类。有介绍迁移指南

在 C++/WinRT 中,您根本不使用类工厂。(因此不需要hstring为类名创建,也不需要IGetActivationFactory。)

涵盖所有 C++/WinRT 超出了本站点的范围,但这里有一个草图:

using winrt::Windows::UI::Notifications::ToastNotification;
using winrt::Windows::UI::Notifications::ToastNotificationManager;
using winrt::Windows::Data::Xml::Dom::XmlDocument;

XmlDocument doc;
doc.LoadXml(L"<toast...>...</toast>");
ToastNotification toast(doc);
ToastNotificationManager::CreateToastNotifier(L"appid").Show(toast);
Run Code Online (Sandbox Code Playgroud)