首页 > 解决方案 > Windows 10 toast 通知图标引用

问题描述

我决定抛弃“老派”的 Windows 气球通知并使用新的 Windows 10 原生 toast 通知。

现在,我正在努力为 toast 通知引用一个图标。根据 Microsocft 文档(此处此处),我应该能够添加这样的通知图标:

// Get a toast XML template
var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);

// Create image element
var image = toastXml.CreateElement("image");
image.SetAttribute("src", "https://picsum.photos/48?image=883");
image.SetAttribute("placement", "appLogoOverride");
toastXml.DocumentElement.AppendChild(image);

相反,会出现一个默认应用程序图标:

在此处输入图像描述

实际工作的唯一方法是使用图像的绝对路径:

"file:///" + Path.GetFullPath("../../Assets/myicon.png");

但是,这并不能满足我的需求,因为我需要引用资源图标或来自网络的图标。

因此我的问题是:

  1. 如何https://picsum.photos/48?image=883在我的 toast 通知中正确引用网络图像 ( )?
  2. 如何正确引用资源图标?
  3. Toast 通知图标中允许使用哪些图像类型?我可以参考,例如.svg图像吗?

标签: c#wpfuwpwindows-10toast

解决方案


如何在我的 toast 通知中正确引用网络图像 ( https://picsum.photos/48?image=883 )?

我们可以通过UWP Community Toolkit Notifications nuget 包轻松实现 toast 通知。如果您想为 引用 Web 图像 AppLogoOverride,请创建AppLogoOverride实例并设置Source属性,如下所示。

例如:

var toastContent = new ToastContent()
{
    Visual = new ToastVisual()
    {
        BindingGeneric = new ToastBindingGeneric()
        {
            Children = 
            {
                new AdaptiveText()
                {
                    Text = "Matt sent you a friend request"
                },
                new AdaptiveText()
                {
                    Text = "Hey, wanna dress up as wizards and ride around on our hoverboards together?"
                }
            },
            AppLogoOverride = new ToastGenericAppLogo()
            {
                Source = "https://unsplash.it/64?image=1005",
                HintCrop = ToastGenericAppLogoCrop.Circle
            }
        }
    },
    Actions = new ToastActionsCustom()
    {
        Buttons = 
        {
            new ToastButton("Accept", "action=acceptFriendRequest&userId=49183")
            {
                ActivationType = ToastActivationType.Background
            },
            new ToastButton("Decline", "action=declineFriendRequest&userId=49183")
            {
                ActivationType = ToastActivationType.Background
            }
        }
    },
    Launch = "action=viewFriendRequest&userId=49183"
};

// Create the toast notification
var toastNotif = new ToastNotification(toastContent.GetXml());

// And send the notification
ToastNotificationManager.CreateToastNotifier().Show(toastNotif);

如何正确引用资源图标?

如果要引用资源图标和 Assets 文件夹中的图标资源,可以参考以下代码。

new AdaptiveImage()
{
   Source = "Assets/Apps/Food/RestaurantMap.jpg"
}

Toast 通知图标中允许使用哪些图像类型?我可以参考,例如 .svg 图像吗?

对于我的测试pngjpgsvg图像可用于 toast 通知。


推荐阅读