背水一戰 Windows 10 (113) - 鎖屏: 將 Application 的 Badge 通知和 Tile 通知發送到鎖屏, 將 secondary tile 的 Badge 通知和 Til

[源碼下載]


html

背水一戰 Windows 10 (113) - 鎖屏: 將 Application 的 Badge 通知和 Tile 通知發送到鎖屏, 將 secondary tile 的 Badge 通知和 Tile 通知發送到鎖屏



做者:webabcd


介紹
背水一戰 Windows 10 之 鎖屏html5

  • 將 Application 的 Badge 通知和 Tile 通知發送到鎖屏
  • 將 secondary tile 的 Badge 通知和 Tile 通知發送到鎖屏



示例
一、演示如何將 Application 的 Badge 通知和 Tile 通知發送到鎖屏
LockScreen/ApplicationNotification.xamlc++

<Page
    x:Class="Windows10.LockScreen.ApplicationNotification"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.LockScreen"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <Button Name="btnBadgeNotification" Content="發送 Application 的 Badge 通知" Click="btnBadgeNotification_Click" Margin="5" />

            <Button Name="btnTileNotification" Content="發送 Application 的 Tile 通知" Click="btnTileNotification_Click" Margin="5" />

        </StackPanel>
    </Grid>
</Page>

LockScreen/ApplicationNotification.xaml.csweb

/*
 * 演示如何將 Application 的 Badge 通知和 Tile 通知發送到鎖屏
 * 
 * 
 * 注:
 * 一、須要在 Package.appxmanifest 中配置好是否支持鎖屏徽章和文本,以及要配置好徽章圖標。相似以下:
 * <LockScreen Notification="badgeAndTileText" BadgeLogo="Assets\LockScreenLogo.png" />
 * 二、在 app 安裝好後,去「設置」->「個性化」->「鎖屏界面」中作以下選擇
 * 在「選擇要顯示詳細狀態的應用」中選擇你的 app,則對此 app 發送 tile 通知後,此 tile 通知的文本就會顯示在鎖屏
 * 在「選擇要顯示快速狀態的應用」中選擇你的 app,則對此 app 發送 badge 通知後,此 badge 通知的圖標就會顯示在鎖屏
 */

using System;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Windows10.LockScreen
{
    public sealed partial class ApplicationNotification : Page
    {
        public ApplicationNotification()
        {
            this.InitializeComponent();
        }

        // 發送 Application 的 Badge 通知
        private void btnBadgeNotification_Click(object sender, RoutedEventArgs e)
        {
            // 用於描述 badge 通知的 xml 字符串(數字在 1 - 99 之間,若是大於 99 則會顯示 99+ ,若是是 0 則會移除 badge,若是小於 0 則無效)
            string badgeXml = "<badge value='3'/>";

            // 將 xml 字符串轉換爲 Windows.Data.Xml.Dom.XmlDocument 對象
            XmlDocument badgeDoc = new XmlDocument();
            badgeDoc.LoadXml(badgeXml);

            // 實例化 BadgeNotification 對象
            BadgeNotification badgeNotification = new BadgeNotification(badgeDoc);

            // 將指定的 BadgeNotification 對象更新到 application
            BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForApplication();
            badgeUpdater.Update(badgeNotification);
        }

        // 發送 Application 的 Tile 通知
        private void btnTileNotification_Click(object sender, RoutedEventArgs e)
        {
            /*
             * 這裏有幾個要特別注意的地方:
             * 一、鎖屏只能顯示 tile 的文本,不能顯示 tile 的圖片之類的
             * 二、鎖屏只會顯示 tile 的 TileWide 模板的文本
             * 三、要想在鎖屏顯示文本,則 tile 的 text 必需要有 id 屬性並指定一個整型值
             * 四、鎖屏文本會按照 tile 的 text 的 id 的大小按順序顯示,若是 id 值同樣則只顯示後面的
             * 五、tile 上的文本顯示規則與其 text 的 id 無關(只有鎖屏文本纔有關係)
             */

            // 用於描述 tile 通知的 xml 字符串
            string tileXml = $@"
                <tile>
                    <visual>
                        <binding template='TileSmall'>
                            <text id='1'>Small 1(小){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                        <binding template='TileMedium'>
                            <text id='1'>Medium 1(中){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                        <binding template='TileWide'>
                            <text id='1'>Wide 1(寬){DateTime.Now.ToString("HH:mm:ss")}</text>
                            <text id='3'>Wide 2(寬){DateTime.Now.ToString("HH:mm:ss")}</text>
                            <text id='2'>Wide 3(寬){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                        <binding template='TileLarge'>
                            <text id='1'>Large 1(大){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                    </visual>
                </tile>";

            /*
             * 若是須要鎖屏顯示的 tile 內容與 TileWide 顯示的 tile 內容不同的話,能夠像下面這樣寫
             * 設置 TileWide 所屬 binding 的 hint-lockDetailedStatus1 屬性和 hint-lockDetailedStatus2 屬性和 hint-lockDetailedStatus3 屬性
             */
            string tileXml2 = $@"
                <tile>
                    <visual>
                        <binding template='TileSmall'>
                            <text>Small 1(小){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                        <binding template='TileMedium'>
                            <text>Medium 1(中){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                        <binding template='TileWide'
                                 hint-lockDetailedStatus1='hint-lockDetailedStatus1' 
                                 hint-lockDetailedStatus2='hint-lockDetailedStatus2' 
                                 hint-lockDetailedStatus3='hint-lockDetailedStatus3'>
                            <text>Wide 1(寬){DateTime.Now.ToString("HH:mm:ss")}</text>
                            <text>Wide 2(寬){DateTime.Now.ToString("HH:mm:ss")}</text>
                            <text>Wide 3(寬){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                        <binding template='TileLarge'>
                            <text>Large 1(大){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                    </visual>
                </tile>";

            // 將 xml 字符串轉換爲 Windows.Data.Xml.Dom.XmlDocument 對象
            XmlDocument tileDoc = new XmlDocument();
            tileDoc.LoadXml(tileXml);

            // 實例化 TileNotification 對象
            TileNotification tileNotification = new TileNotification(tileDoc);

            // 將指定的 TileNotification 對象更新到 application tile
            TileUpdater tileUpdater = TileUpdateManager.CreateTileUpdaterForApplication();
            tileUpdater.Update(tileNotification);
        }
    }
}


二、演示如何將 secondary tile 的 Badge 通知和 Tile 通知發送到鎖屏
LockScreen/SecondaryTileNotification.xamlexpress

<Page
    x:Class="Windows10.LockScreen.SecondaryTileNotification"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.LockScreen"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <TextBlock Name="lblMsg" Margin="5" />
            
            <Button Name="btnBadgeNotification" Content="發送 secondary tile 的 Badge 通知" Click="btnBadgeNotification_Click" Margin="5" />

            <Button Name="btnTileNotification" Content="發送 secondary tile 的 Tile 通知" Click="btnTileNotification_Click" Margin="5" />

        </StackPanel>
    </Grid>
</Page>

LockScreen/SecondaryTileNotification.xaml.cswindows

/*
 * 演示如何將 secondary tile 的 Badge 通知和 Tile 通知發送到鎖屏
 * 
 * 
 * SecondaryTile - secondary tile
 *     LockScreenDisplayBadgeAndTileText - 是否容許此 secondary tile 在鎖屏上顯示徽章和文本
 *     LockScreenBadgeLogo - 鎖屏上徽章圖標的地址
 * 
 * 注:
 * 在 app 安裝好後,先要將 secondary tile 固定到開始屏幕,而後去「設置」->「個性化」->「鎖屏界面」中作以下選擇
 * 在「選擇要顯示詳細狀態的應用」中選擇你的 secondary tile,則對此 secondary tile 發送 tile 通知後,此 tile 通知的文本就會顯示在鎖屏
 * 在「選擇要顯示快速狀態的應用」中選擇你的 secondary tile,則對此 secondary tile 發送 badge 通知後,此 badge 通知的圖標就會顯示在鎖屏
 */

using System;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using Windows.UI.StartScreen;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace Windows10.LockScreen
{
    public sealed partial class SecondaryTileNotification : Page
    {
        private const string TILEID = "tile_lockscreen";

        public SecondaryTileNotification()
        {
            this.InitializeComponent();
        }

        // 在開始屏幕固定一個 secondary tile 磁貼
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            Uri square150x150Logo = new Uri("ms-appx:///Assets/Square150x150Logo.png");
            Uri wide310x150Logo = new Uri("ms-appx:///Assets/Wide310x150Logo.png");
            Uri square310x310Logo = new Uri("ms-appx:///Assets/Square310x310Logo.png");
            SecondaryTile secondaryTile = new SecondaryTile(TILEID, "個人 secondary tile", "arguments", square150x150Logo, TileSize.Wide310x150);
            secondaryTile.VisualElements.Wide310x150Logo = wide310x150Logo;
            secondaryTile.VisualElements.Square310x310Logo = square310x310Logo;

            // 容許此 secondary tile 在鎖屏上顯示徽章和文本
            secondaryTile.LockScreenDisplayBadgeAndTileText = true;
            // 此 secondary tile 在鎖屏上顯示徽章時的圖標
            secondaryTile.LockScreenBadgeLogo = new Uri("ms-appx:///Assets/LockScreenLogo.png");

            try
            {
                bool isPinned = await secondaryTile.RequestCreateAsync();
                lblMsg.Text = isPinned ? "固定成功" : "固定失敗";
            }
            catch (Exception ex)
            {
                lblMsg.Text = "固定失敗: " + ex.ToString();
            }
        }

        // 發送 secondary tile 的 Badge 通知
        private void btnBadgeNotification_Click(object sender, RoutedEventArgs e)
        {
            // 用於描述 badge 通知的 xml 字符串(數字在 1 - 99 之間,若是大於 99 則會顯示 99+ ,若是是 0 則會移除 badge,若是小於 0 則無效)
            string badgeXml = "<badge value='3'/>";

            // 將 xml 字符串轉換爲 Windows.Data.Xml.Dom.XmlDocument 對象
            XmlDocument badgeDoc = new XmlDocument();
            badgeDoc.LoadXml(badgeXml);

            // 實例化 BadgeNotification 對象
            BadgeNotification badgeNotification = new BadgeNotification(badgeDoc);

            // 將指定的 BadgeNotification 對象更新到指定的 secondary tile 磁貼
            BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile(TILEID);
            badgeUpdater.Update(badgeNotification);
        }

        // 發送 secondary tile 的 Tile 通知
        private void btnTileNotification_Click(object sender, RoutedEventArgs e)
        {
            /*
             * 這裏有幾個要特別注意的地方:
             * 一、鎖屏只能顯示 tile 的文本,不能顯示 tile 的圖片之類的
             * 二、鎖屏只會顯示 tile 的 TileWide 模板的文本
             * 三、要想在鎖屏顯示文本,則 tile 的 text 必需要有 id 屬性並指定一個整型值
             * 四、鎖屏文本會按照 tile 的 text 的 id 的大小按順序顯示,若是 id 值同樣則只顯示後面的
             * 五、tile 上的文本顯示規則與其 text 的 id 無關(只有鎖屏文本纔有關係)
             */

            // 用於描述 tile 通知的 xml 字符串
            string tileXml = $@"
                <tile>
                    <visual>
                        <binding template='TileSmall'>
                            <text>Small 1(小){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                        <binding template='TileMedium'>
                            <text>Medium 1(中){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                        <binding template='TileWide'>
                            <text id='1'>Wide 1(寬){DateTime.Now.ToString("HH:mm:ss")}</text>
                            <text id='3'>Wide 2(寬){DateTime.Now.ToString("HH:mm:ss")}</text>
                            <text id='2'>Wide 3(寬){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                        <binding template='TileLarge'>
                            <text>Large 1(大){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                    </visual>
                </tile>";

            /*
             * 若是須要鎖屏顯示的 tile 內容與 TileWide 顯示的 tile 內容不同的話,能夠像下面這樣寫
             * 設置 TileWide 所屬 binding 的 hint-lockDetailedStatus1 屬性和 hint-lockDetailedStatus2 屬性和 hint-lockDetailedStatus3 屬性
             */
            string tileXml2 = $@"
                <tile>
                    <visual>
                        <binding template='TileSmall'>
                            <text>Small 1(小){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                        <binding template='TileMedium'>
                            <text>Medium 1(中){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                        <binding template='TileWide'
                                 hint-lockDetailedStatus1='hint-lockDetailedStatus1' 
                                 hint-lockDetailedStatus2='hint-lockDetailedStatus2' 
                                 hint-lockDetailedStatus3='hint-lockDetailedStatus3'>
                            <text>Wide 1(寬){DateTime.Now.ToString("HH:mm:ss")}</text>
                            <text>Wide 2(寬){DateTime.Now.ToString("HH:mm:ss")}</text>
                            <text>Wide 3(寬){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                        <binding template='TileLarge'>
                            <text>Large 1(大){DateTime.Now.ToString("HH:mm:ss")}</text>
                        </binding>
                    </visual>
                </tile>";

            // 將 xml 字符串轉換爲 Windows.Data.Xml.Dom.XmlDocument 對象
            XmlDocument tileDoc = new XmlDocument();
            tileDoc.LoadXml(tileXml);

            // 實例化 TileNotification 對象
            TileNotification tileNotification = new TileNotification(tileDoc);

            // 將指定的 TileNotification 對象更新到 secondary tile
            TileUpdater tileUpdater = TileUpdateManager.CreateTileUpdaterForSecondaryTile(TILEID);
            tileUpdater.Update(tileNotification);
        }
    }
}



OK
[源碼下載]app

相關文章
相關標籤/搜索