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

时间:2019-12-16 06:26:00

标签: c# uwp notifications windows-10

我最近在我的桌面聊天应用程序中创建了一个敬酒通知,现在我的任务是将任何建议做的建议分配给该生成的通知一个通知图标?

这是我使用的代码:

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

2 个答案:

答案 0 :(得分:0)

您是否检查了此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);

答案 1 :(得分:0)

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

因此,一旦我尝试卸载应用程序的先前版本,则会显示设置后的通知图标。

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