在 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));
}

您是否检查过此Microsoft示例。 windows.ui.notifications.toastnotificationmanager.createtoastnotifier

对于 UWP:

var notifications = Windows.UI.Notifications;
// Get the toast notification manager for the current app.
var notificationManager = notifications.ToastNotificationManager;
// The getTemplateContent method returns a Windows.Data.Xml.Dom.XmlDocument object
// that contains the toast notification XML content.
var template = notifications.toastTemplateType.toastImageAndText01;
var toastXml = notificationManager.getTemplateContent(notifications.ToastTemplateType[template]);
// You can use the methods from the XML document to specify the required elements for the toast.
var images = toastXml.getElementsByTagName("image");
images[0].setAttribute("src", "images/toastImageAndText.png");
var textNodes = toastXml.getElementsByTagName("text");
textNodes.forEach(function (value, index) {
var textNumber = index + 1;
var text = "";
for (var j = 0; j < 10; j++) {
text += "Text input " + /*@static_cast(String)*/textNumber + " ";
}
value.appendChild(toastXml.createTextNode(text));
});
// Create a toast notification from the XML, then create a ToastNotifier object
// to send the toast.
var toast = new notifications.ToastNotification(toastXml);
notificationManager.createToastNotifier().show(toast);

更新 :适用于视窗 10

// Get a toast XML template
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
for (int i = 0; i < stringElements.Length; i++)
{
stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
}
// Specify the absolute path to an image
String imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
ToastNotification toast = new ToastNotification(toastXml);

我做错的是我的PC中安装了我的应用程序的先前版本(1.0(,因此当通知到达我正在运行的新应用程序(2.0(时,由于已经在"开始"菜单中创建了一个实例和快捷方式,因此它显示默认图标

因此,一旦我尝试卸载以前版本的应用程序,就会出现设置后的通知图标。

感谢您的合作和宝贵的时间。

最新更新