Windows 10 通知 WPF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35910332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:42:30  来源:igfitidea点击:

Windows 10 Notification WPF

c#wpfnotificationsvisual-studio-2015windows-10

提问by Filip J?nsson

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#. 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?

我试图弄清楚是否可以通过我的 WPF 应用程序访问 Windows 10 中存在的内置通知服务。我正在使用 VS 2015 和 c#。另外,烤面包机通知是一样的吗?它们在 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。这不是我要找的。

采纳答案by Leo Hinojosa

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. Select the checkbox for Windows.UI, Windows.Data and Windows.Foundation.

右键单击 References 节点,然后单击左侧窗格中的 Windows。选中 Windows.UI、Windows.Data 和 Windows.Foundation 的复选框。

Next on your form class file, add using Windows.UI.Notifications;to access the ToastManager component.

接下来在您的表单类文件上,添加using Windows.UI.Notifications;以访问 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 事件处理程序附加事件。