我已经构建了一个应用程序,每当FileSystemWatcher
检测到更改时,该应用程序就会在Windows 10上显示Toast通知。我正在使用Microsoft.Toolkit.Uwp.Notifications
和Windows.UI.Notifications
命名空间。在启用了调试器的情况下在VS2019中运行代码时,它可以正常工作,但是使用自写的WiX安装程序安装应用程序后,出现错误消息,随后该应用程序关闭。
该错误消息非常笼统,对于到哪里查找我一无所知:
无法显示[FileSystemWatcher名称]的通知。找不到元素。 (来自HRESULT的异常:0x80070490)在Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier()处在 ...在.. callstack的其余部分来自应用程序,使用CreateToast指向方法
负责创建和显示敬酒的类
using System;
using Windows.Data.Xml.Dom;
using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications;
namespace MyApp.Notifs
{
public class NotificationToaster
{
private string appID;
//appID is read from the Properties
public NotificationToaster(string appID)
{
this.appID = appID;
}
public void ShowToast(string title, string content)
{
string toastXmlString =
$@"<toast>
<visual>
<binding template=""ToastGeneric"">
<text hint-maxLines=""1""> {title} </text>
<text> {content} </text>
<group>
<subgroup>
<text> {DateTime.Now} </text>
</subgroup>
</group>
</binding>
</visual>
<audio src=""ms-winsoundevent:Notification.Default""/>
</toast>";
CreateToast(toastXmlString);
}
private void CreateToast(string toast)
{
//Load Toast data from string to XML
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(toast);
// Create the toast notification
ToastNotification toastNotif = new ToastNotification(xmlDoc);
//Attach Manager to raise notifications
var toastNotifier = ToastNotificationManager.CreateToastNotifier(appID);
// And send the notification
toastNotifier.Show(toastNotif);
}
}
}
经过一些搜索,我尝试了几种解决方案。我尝试的第一件事之一是将应用程序包装到Windows Application Packaging Project中,如here所述。 (不过,我还没有将WPF项目从Framework移植到Core。)
此处有关SO的较早的文章建议在创建通知程序时使用应用程序ID作为参数。使用静态GUID执行此操作时,它将停止发送通知,同时会击中所有Breakpoitns。在调试之外,程序不再崩溃,但是也不会发送通知。
任何有关这里可能出问题的指针都将提供巨大帮助。
如果不打包应用程序,则必须按照docs中的说明,在“开始”中应用程序的快捷方式上声明应用程序用户模型ID(AUMID)和吐司激活器CLSID。
<Shortcut Id="ApplicationStartMenuShortcut" Name="Wix Sample" Description="Wix Sample" Target="[INSTALLFOLDER]WixSample.exe" WorkingDirectory="INSTALLFOLDER">
<!--AUMID-->
<ShortcutProperty Key="System.AppUserModel.ID" Value="YourCompany.YourApp"/>
<!--COM CLSID-->
<ShortcutProperty Key="System.AppUserModel.ToastActivatorCLSID" Value="{replaced-with-your-guid-C173E6ADF0C3}"/>