简体   繁体   English

Windows 10 通知 WPF

[英]Windows 10 Notification WPF

I'm trying to figure out if it is possible through my WPF application, to access the built in notification service that exists in Windows 10. I'm using VS 2015 and c#.我试图弄清楚是否可以通过我的 WPF 应用程序访问 Windows 10 中存在的内置通知服务。我正在使用 VS 2015 和 c#。 Also, is the toasternotification the same thing?另外,烤面包机通知是一样的吗? They dont look like that anymore in Windows 10. If yes, Could you please guide me in the right direction to namespace etc?它们在 Windows 10 中不再像那样了。如果是,请您指导我正确的方向命名空间等?

Yes, I have searched the web and only found toasternotification for Win 7. And that is not what I'm looking for.是的,我在网上搜索过,只找到了 Win 7 的 toasternotification。这不是我要找的。

Found a code sample that is similar to what you need, but only does Toast Notifications .找到了一个类似于您需要的代码示例,但仅适用于 Toast Notifications

You basically want to have a regular .NET application that references the Windows.UI components.您基本上希望拥有一个引用 Windows.UI 组件的常规 .NET 应用程序。

To use the Windows 10 Notifications you need to edit your csproj file and add the target platform,要使用 Windows 10 通知,您需要编辑 csproj 文件并添加目标平台,

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<TargetPlatformVersion>8.1</TargetPlatformVersion>
</PropertyGroup>

Once you do this, you should be able to add a reference to the Windows.UI assemblies.完成此操作后,您应该能够添加对 Windows.UI 程序集的引用。

Right click the References node, and click on Windows on the left side pane.右键单击 References 节点,然后单击左侧窗格中的 Windows。 Select the checkbox for Windows.UI, Windows.Data and Windows.Foundation.选中 Windows.UI、Windows.Data 和 Windows.Foundation 的复选框。

Next on your form class file, add using Windows.UI.Notifications;接下来在你的表单类文件中,添加using Windows.UI.Notifications; to access the ToastManager component.访问 ToastManager 组件。

Once you have that, access the template you want to use完成后,访问您要使用的模板

 // Get a toast XML template
 var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText02);

 // Fill in the text elements
 var stringElements = toastXml.GetElementsByTagName("text");
 stringElements[0].AppendChild(toastXml.CreateTextNode("Title"));
 stringElements[1].AppendChild(toastXml.CreateTextNode("Content"));

Here are the different Toast type enumerations . 以下是不同的 Toast 类型枚举

Once you have a reference to the Toast template you have to create a ToastNotification and send it to the ToastNotificationManager一旦你引用了 Toast 模板,你必须创建一个 ToastNotification 并将其发送到 ToastNotificationManager

 // Create the toast and attach event listeners
 var toast = new ToastNotification(toastXml);
 toast.Activated += ToastActivated;
 toast.Dismissed += ToastDismissed;
 toast.Failed += ToastFailed;

 // Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
 ToastNotificationManager.CreateToastNotifier("My Toast").Show(toast);

You can attach events for the Activated, Dismissed and Failed event handlers too.您也可以为 Activated、Dismissed 和 Failed 事件处理程序附加事件。

For future reference:备查:

Here is described very well how to use new windows 10 toast notifications. 这里很好地描述了如何使用新的 Windows 10 Toast 通知。 And you don't need to install your app beforehand or set AppUserModelID .而且您不需要事先安装您的应用程序或设置AppUserModelID Works directly from Visual Studio (I mean debug mode) and simplest code looks like this:直接从 Visual Studio 工作(我的意思是调试模式),最简单的代码如下所示:

new ToastContentBuilder()
    .AddText("Something copied to clipboard")
    .Show();

But don't forget to change your target platform in your *.csproj file (everything is described in the article).但是不要忘记在您的 *.csproj 文件中更改您的目标平台(文章中描述了所有内容)。

<TargetFramework>net5.0-windows10.0.17763.0</TargetFramework>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM