我知道这是一篇旧帖子,但我认为这可能会像我在尝试让 Toast Notifications 在 Win 10 上工作时那样偶然发现此问题的人有所帮助。
这似乎是一个很好的大纲 -
Send a local toast notification from desktop C# apps
我在这篇很棒的博客文章中使用了该链接-Pop a Toast Notification in WPF using Win 10 APIs
让我的 WPF 应用程序在 Win10 上运行。与“老派”通知图标相比,这是一个更好的解决方案,因为即使在通知进入操作中心之后,您也可以添加按钮来完成您的 toast 中的特定操作。
注意-第一个链接提到“如果您使用的是 WiX”,但这确实是一项要求。您必须先创建并安装您的 Wix 设置项目,然后 Toasts 才能工作。因为您的应用程序的 appUserModelId 需要先注册。除非您在其中阅读我的 cmets,否则第二个链接不会提及这一点。
提示- 安装应用后,您可以通过在运行行 shell:appsfolder 上运行此命令来验证 AppUserModelId。确保您在详细信息视图中,然后单击 View ,Choose Details 并确保选中 AppUserModeId。将您的 AppUserModelId 与其他已安装的应用进行比较。
这是我使用的代码片段。这里要注意两点,我没有安装第一个链接的第 7 步中提到的“通知库”,因为我更喜欢使用原始 XML。
private const String APP_ID = "YourCompanyName.YourAppName";
public static void CreateToast()
{
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(
ToastTemplateType.ToastImageAndText02);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode("This is my title!!!!!!!!!!"));
stringElements[1].AppendChild(toastXml.CreateTextNode("This is my message!!!!!!!!!!!!"));
// Specify the absolute path to an image
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\Your Path To File\Your Image Name.png";
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = filePath;
// Change default audio if desired - ref - https://docs.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-audio
XmlElement audio = toastXml.CreateElement("audio");
//audio.SetAttribute("src", "ms-winsoundevent:Notification.Reminder");
//audio.SetAttribute("src", "ms-winsoundevent:Notification.IM");
//audio.SetAttribute("src", "ms-winsoundevent:Notification.Mail"); // sounds like default
//audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Call7");
audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Call2");
//audio.SetAttribute("loop", "false");
// Add the audio element
toastXml.DocumentElement.AppendChild(audio);
XmlElement actions = toastXml.CreateElement("actions");
toastXml.DocumentElement.AppendChild(actions);
// Create a simple button to display on the toast
XmlElement action = toastXml.CreateElement("action");
actions.AppendChild(action);
action.SetAttribute("content", "Show details");
action.SetAttribute("arguments", "viewdetails");
// Create the toast
ToastNotification toast = new ToastNotification(toastXml);
// Show the toast. Be sure to specify the AppUserModelId
// on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
}