繁体   English   中英

在 WPF 中 Windows 10 类型的 Toast 通知中显示通知图像

[英]Display Notification Image in Toast Notification of type Windows 10 in WPF

我最近在我的桌面聊天应用程序中创建了一个 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);

更新:适用于 Windows 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)时,因为已经在开始菜单中创建了一个实例和快捷方式,它正在显示默认图标

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM