如何使用自定义图标和自定义应用程序名称创建Toast通知?

问题描述 投票:0回答:2

我正在尝试使用 .Net C# 创建 Windows Toast 通知。与此同时,我想更改通知的属性区域(标题)中的通知图标和应用程序名称。

我可以使用自定义“应用程序名称”放置通知,如下所示:

My screenshot of the following code

我用这段代码来做到这一点:

var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);

var stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode("Title"));
stringElements[1].AppendChild(toastXml.CreateTextNode("Message"));

ToastNotification toast = new(toastXml);
ToastNotificationManager.CreateToastNotifier("App name").Show(toast);

但我没有找到解决方案,如何放置“应用程序名称”左侧的图标。

解决方案不能是以不同的方式修改 toast,因为它只会更改视觉或操作元素。但不是属性区域:

Screenshot from Microsoft

如何在属性区显示图标?

c# .net windows toast
2个回答
1
投票

要设置归属区图标可以直接设置。

var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);

var stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode("Title"));
stringElements[1].AppendChild(toastXml.CreateTextNode("Message"));

ToastNotification toast = new(toastXml);
// here you set your icon
toast.attribution.icon = new ToastImageSource("path to your image");
ToastNotificationManager.CreateToastNotifier("App name").Show(toast);

要设置标题中的图标,您可以更改创建的吐司。

var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);

var stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode("Title"));
stringElements[1].AppendChild(toastXml.CreateTextNode("Message"));

ToastNotification toast = new(toastXml);
// Here you can change the visual element of the toast
// don't know which image will be correct. Can't test at the moment
toast.Visual.TileLarge.HintRemoveMargin = true;
toast.Visual.TileSmall.HintRemoveMargin = true;
toast.Visual.TileMedium.HintRemoveMargin = true;
toast.Visual.TileLarge.Branding = ToastBranding.Logo;
toast.Visual.TileSmall.Branding = ToastBranding.Logo;
toast.Visual.TileMedium.Branding = ToastBranding.Logo;
toast.Visual.TileLarge.AppLogoOverride = new ToastAppLogo();
toast.Visual.TileSmall.AppLogoOverride = new ToastAppLogo();
toast.Visual.TileMedium.AppLogoOverride = new ToastAppLogo();
toast.Visual.TileLarge.AppLogoOverride.Crop = new ToastAppLogoCrop(0, 0, 1, 1);
toast.Visual.TileSmall.AppLogoOverride.Crop = new ToastAppLogoCrop(0, 0, 1, 1);
toast.Visual.TileMedium.AppLogoOverride.Crop = new ToastAppLogoCrop(0, 0, 1, 1);
toast.Visual.TileLarge.AppLogoOverride.Source = new ToastImageSource("file:///C:/path/to/your/icon");
toast.Visual.TileSmall.AppLogoOverride.Source = new ToastImageSource("file:///C:/path/to/your/icon");
toast.Visual.TileMedium.AppLogoOverride.Source = new ToastImageSource("file:///C:/path/to/your/icon");

ToastNotificationManager.CreateToastNotifier("App name").Show(toast);

此外,您还可以设置在标题区域显示的

appLogoOverlay

// get hte ToastGeneric toast content
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastGeneric);

// here you get the visual element of the toast
XmlNodeList toastVisualElements = toastXml.GetElementsByTagName("visual");

// to add an icon you need app logo overlay 
XmlElement appLogoOverlay = toastXml.CreateElement("appLogoOverlay");
// here you set the source of the overlay 
appLogoOverlay.SetAttribute("src", "path to your icon");
// here you can add style attributes 
appLogoOverlay.SetAttribute("hint-crop", "circle");
toastVisualElements[0].AppendChild(appLogoOverlay);

var stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode("Title"));
stringElements[1].AppendChild(toastXml.CreateTextNode("Message"));

ToastNotification toast = new(toastXml);
ToastNotificationManager.CreateToastNotifier("App name").Show(toast);

0
投票

经过长时间的研究和在互联网上搜索,我找到了以下方法来更改吐司的窗口图标。

您必须注册一个

AppID
并设置其徽标,如下所示:

# Function to set the AppID and logo
Function Set-ToastAppIDAndLogo {
    Param (
        [Parameter(Mandatory = $true)] [String]$AppID,
        [Parameter(Mandatory = $true)] [String]$AppDisplayName,
        [Parameter(Mandatory = $true)] [String]$LogoImagePath
    )

    $regPathToastNotificationSettings = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Notifications\Settings'
    $regPathToastApp = 'HKCU:\Software\Classes\AppUserModelId'

    New-Item -Path "$regPathToastNotificationSettings\$AppID" -Force | Out-Null
    Set-ItemProperty -Path "$regPathToastNotificationSettings\$AppID" -Name 'ShowInActionCenter' -Value 1 -Force
    Set-ItemProperty -Path "$regPathToastNotificationSettings\$AppID" -Name 'Enabled' -Value 1 -Force

    New-Item -Path "$regPathToastApp\$AppID" -Force | Out-Null
    Set-ItemProperty -Path "$regPathToastApp\$AppID" -Name 'DisplayName' -Value $AppDisplayName -Force
    Set-ItemProperty -Path "$regPathToastApp\$AppID" -Name 'IconUri' -Value $LogoImagePath -Force
}

然后,在称呼您的吐司时,请确保使用相同的

AppID

Function Show-ToastNotification {
    Param (
        [Parameter(Mandatory = $true)] [String]$BalloonTipText,
        [Parameter(Mandatory = $true)] [String]$BalloonTipTitle,
        [Parameter(Mandatory = $true)] [String]$AppID,
        [Parameter(Mandatory = $true)] [String]$LogoImagePath
    )

    [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
    [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null

    [xml]$ToastTemplate = @"
<toast launch="app-defined-string">
    <visual>
        <binding template="ToastImageAndText02">
            <text id="1">$BalloonTipTitle</text>
            <text id="2">$BalloonTipText</text>
            <image id="1" src='file://$AppDeployLogoImage' />
        </binding>
    </visual>
</toast>
"@

    $ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
    $ToastXml.LoadXml($ToastTemplate.OuterXml)

    $notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppID)
    $notifier.Show($ToastXml)
}

像这样运行脚本:

$toastAppID = "com.reverse.url"
$toastAppDisplayName = "Display name"
$AppDeployLogoImage = "C:/Path/to/image.png"

Set-ToastAppIDAndLogo -AppID $toastAppID -AppDisplayName $toastAppDisplayName -LogoImagePath $AppDeployLogoImage

Show-ToastNotification -BalloonTipText "Test" -BalloonTipTitle "Test Notification" -AppID $toastAppID -LogoImagePath $AppDeployLogoImage

注意

AppID
仅在运行第一个脚本后5分钟才会注册。因此,如果您在前 5 分钟内调用 Toast 通知,徽标可能不会更新。

© www.soinside.com 2019 - 2024. All rights reserved.