Windows.UI.Notifications丢失

我想在此示例中为Windows 10中的操作中心创建简单的Toast通知。 但我在第2步遇到了问题:

using Windows.UI.Notifications; 

它失踪了。 但是我花了很多时间才找到它而没有结果。 我真的不知道我在哪里可以找到或至少下载它。

我尝试了什么:

  1. 经过长时间的搜索后,我在C:\Windows\System32找到了Windows.UI.dll ,但是当我尝试将它作为参考添加到项目中时,我收到了这个错误。 即使在我尝试复制它并使其完全可访问之后,也没有任何改变

在此处输入图像描述

  1. 我试图重新安装.Net(我使用的是4.5.2)
  2. 安装了Windows 10 SDK
  3. 试图用全球import
  4. 添加
  10.0  
  1. 添加了System.Runtime.dll引用

示例代码可能对您无用:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Toolkit.Uwp.Notifications; using Microsoft.QueryStringDotNET; using Windows.UI.Notifications; namespace MessagerClient.Notifications { class DefaultWindowsNotification { public static void notificationTest() { string title = "Andrew sent you a picture"; string content = "Check this out, Happy Canyon in Utah!"; string image = "http://blogs.msdn.com/something.jpg"; string logo = "ms-appdata:///local/Andrew.jpg"; ToastVisual visual = new ToastVisual() { BindingGeneric = new ToastBindingGeneric() { Children = { new AdaptiveText() { Text = title }, new AdaptiveText() { Text = content }, new AdaptiveImage() { Source = image } }, AppLogoOverride = new ToastGenericAppLogo() { Source = logo, HintCrop = ToastGenericAppLogoCrop.Circle } } }; Console.WriteLine("NOTIFICATION"); //Can`t use because of Windows.UI library ToastNotificationManager.CreateToastNotifier().Show(visual); } } } 

你必须非常努力地在Visual Studio中使用这些UWP合同。 你用错误的TargetPlatformVersion立刻走错了路,很难从中恢复过来。 完整的步骤:

使用文本编辑器编辑.csproj文件,Notepad会这样做。 插入此:

   10.0.10586  

假设您的计算机上安装了10586 SDK版本。 目前,这些版本变化很快。 通过查看C:\ Program Files(x86)\ Windows Kits \ 10 \ Include with Explorer进行仔细检查,您会看到该目录中列出的已安装版本。

打开Winforms项目,使用Project> Add Reference> Windows选项卡>勾选Windows.DataWindows.UI合约。 再次添加Reference并使用Browse选项卡选择System.Runtime。 我在C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework \ .NETFramework \ v4.6.1 \ Facades中选择了一个。 此参考显示一个警告图标,不确定它想说什么,但它似乎没有任何副作用。

通过删除表单上的按钮来测试它,双击以添加Click事件处理程序。 最基本的代码:

 using Windows.UI.Notifications; ... private void button1_Click(object sender, EventArgs e) { var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01); var text = xml.GetElementsByTagName("text"); text[0].AppendChild(xml.CreateTextNode("Hello world")); var toast = new ToastNotification(xml); ToastNotificationManager.CreateToastNotifier("anythinggoeshere").Show(toast); } 

通过使用不同的ToastTemplateType添加图像或更多文本行来修饰。 请记住,您的程序只能在Win10计算机上运行。