我想知道是否可以通过我的WPF应用程序访问Windows10中的内置通知服务。我使用的是VS 2015和c#。另外,thing通知也是一样的吗?在Windows10中,它们看起来不再像这样了。如果是,你能指引我到名称空间等正确的方向吗?
是的,我已经在网上搜索过了,只找到了Win 7的通知,但这不是我要找的。
发布于 2016-09-20 20:20:23
找到a code sample that is similar to what you need, but only does Toast Notifications。
基本上,您希望拥有一个引用Windows.UI组件的常规.NET应用程序。
要使用Windows 10通知,您需要编辑csproj文件并添加目标平台。
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<TargetPlatformVersion>8.1</TargetPlatformVersion>
</PropertyGroup>完成此操作后,您应该能够添加对Windows.UI程序集的引用。
右键单击References节点,然后单击左侧窗格中的Windows。选中Windows.UI、Windows.Data和Windows.Foundation的复选框。
接下来,在窗体类文件中,添加using Windows.UI.Notifications;以访问ToastManager组件。
完成此操作后,访问要使用的模板
// 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模板的引用,就必须创建一个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);您也可以为激活的、解除的和失败的事件处理程序附加事件。
发布于 2021-07-07 03:55:50
供将来参考:
Here很好地描述了如何使用新的windows10toast通知。而且你不需要事先安装你的应用程序或者设置AppUserModelID。直接在Visual Studio中工作(我指的是调试模式),最简单的代码如下所示:
new ToastContentBuilder()
.AddText("Something copied to clipboard")
.Show();但是不要忘记在*.csproj文件中更改您的目标平台(本文对所有内容都进行了描述)。
<TargetFramework>net5.0-windows10.0.17763.0</TargetFramework>https://stackoverflow.com/questions/35910332
复制相似问题