首页 > 解决方案 > 在 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));
    }

标签: c#uwpnotificationswindows-10

解决方案


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

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

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


推荐阅读