c# - 在 WPF 中 Windows 10 类型的 Toast 通知中显示通知图像
问题描述
我最近在我的桌面聊天应用程序中创建了一个 toast 通知,现在我的任务是为这个生成的通知分配一个通知图标,有什么建议可以做吗?
这是我使用的代码:
var data = $@"<toast>
<visual>
<binding template = ""ToastText02 "">
<text id = ""1"" > {notif.Title} < /text>
<text id = ""2"" > {notif.Message} < /text>
</binding>
</visual>
</toast>";
var toastXml = new XmlDocument();
toastXml.LoadXml(data);
var toast = new ToastNotification(toastXml);
toast.Activated += (sender, args) => NavigateUserToConversation(messageDto);
ToastNotificationManager.CreateToastNotifier(AppConstants.APP_ID).Show(toast);
我在 XML 区域尝试了这条线:
<image placement="appLogoOverride" hint-crop="circle" src="https://picsum.photos/48?image=883"/>
这也是:
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText02);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode(notif.Title));
stringElements[1].AppendChild(toastXml.CreateTextNode(notif.Message));
//// Specify the absolute path to an image
XmlNodeList toastImageAttributes = toastXml.GetElementsByTagName("image");
((XmlElement)toastImageAttributes[0]).SetAttribute("src", "pack://application:,,,/Images/BtrackAppIcon.png");
ToastNotification toast = new ToastNotification(toastXml);
在使用第二种情况时,没有显示文本,但它显示了一个新通知
仍然没有运气
关于如何实施它的任何建议
我也使用下面的代码在开始菜单中创建了一个快捷方式有没有办法明确地将图像添加到快捷方式(不在解决方案属性中):
private bool TryCreateShortcut()
{
String shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Windows\\Start Menu\\Programs\\"+AppConstants.APP_ID+".lnk";
if (!File.Exists(shortcutPath))
{
InstallShortcut(shortcutPath);
return true;
}
return false;
}
private void InstallShortcut(String shortcutPath)
{
// Find the path to the current executable
String exePath = Process.GetCurrentProcess().MainModule.FileName;
IShellLinkW newShortcut = (IShellLinkW)new CShellLink();
// Create a shortcut to the exe
ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath));
ErrorHelper.VerifySucceeded(newShortcut.SetArguments(""));
// Open the shortcut property store, set the AppUserModelId property
IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut;
using (PropVariant appId = new PropVariant(AppConstants.APP_ID))
{
ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId));
ErrorHelper.VerifySucceeded(newShortcutProperties.Commit());
}
// Commit the shortcut to disk
IPersistFile newShortcutSave = (IPersistFile)newShortcut;
ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true));
}
解决方案
我做错的是我的电脑上安装了我的应用程序的以前版本(1.0),所以当通知到达我正在运行的新应用程序(2.0)时,因为已经在开始菜单中创建了一个实例和快捷方式,它正在显示默认图标
因此,一旦我尝试卸载以前版本的应用程序,通知图标就会在设置时出现。
感谢您的合作和宝贵的时间。
推荐阅读
- c# - ASP.NET Core Web App 将 404 请求记录为警告
- asp.net - 单击按钮时出错:System.InvalidCastException:'无法将'System.Int32'类型的对象转换为'System.Byte []'类型。
- vb.net - 结合两个列表,仅记录具有 1 个特定唯一属性的记录
- java - 如何避免arraylist,java的嵌套for循环
- python - 随机选择小写和大写,无需手动添加
- mysql - 有一项服务在一个会话中有许多数据库对话。在数据库中插入行时获取表上的锁定模式
- typescript - 这是在 VS Code 或 TypeScript 中报告的错误吗?
- xamarin.ios - 在 Visual Studio 上发布 Xamarin iOS 应用
- angular - 为什么使用 mat datepicker 时未显示所需的消息?
- ssl - 每个集群的证书还是每个服务提供商的证书?