背水一戰 Windows 10 (107) - 通知(Toast): 提示音, 特定場景

[源碼下載]


html

背水一戰 Windows 10 (107) - 通知(Toast): 提示音, 特定場景



做者:webabcd


介紹
背水一戰 Windows 10 之 通知(Toast)html5

  • 提示音
  • 特定場景



示例
一、本例用於演示 toast 的提示音
Notification/Toast/Audio.xamlc++

<Page
    x:Class="Windows10.Notification.Toast.Audio"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Notification.Toast"
    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">

            <StackPanel Orientation="Horizontal">
                <ComboBox x:Name="cmbLoop" Header="loop" Margin="5">
                    <ComboBoxItem IsSelected="True">false</ComboBoxItem>
                    <ComboBoxItem>true</ComboBoxItem>
                </ComboBox>
                <ComboBox x:Name="cmbSilent" Header="silent" Margin="5">
                    <ComboBoxItem IsSelected="True">false</ComboBoxItem>
                    <ComboBoxItem>true</ComboBoxItem>
                </ComboBox>
                <ComboBox x:Name="cmbSrc" Header="src" Margin="5">
                    <ComboBoxItem IsSelected="True">ms-winsoundevent:Notification.Default</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.IM</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Mail</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Reminder</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.SMS</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Alarm</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Alarm2</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Alarm3</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Alarm4</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Alarm5</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Alarm6</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Alarm7</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Alarm8</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Alarm9</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Alarm10</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Call</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Call2</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Call3</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Call4</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Call5</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Call6</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Call7</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Call8</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Call9</ComboBoxItem>
                    <ComboBoxItem>ms-winsoundevent:Notification.Looping.Call10</ComboBoxItem>
                </ComboBox>
            </StackPanel>

            <Button Name="buttonShowToast1" Content="顯示 toast(系統提示音)" Click="buttonShowToast1_Click" Margin="5" />

            <Button Name="buttonShowToast2" Content="顯示 toast(ms-appx 或 ms-appdata 地址的音頻文件)" Click="buttonShowToast2_Click" Margin="5" />

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

Notification/Toast/Audio.xaml.csweb

/*
 * 本例用於演示 toast 的提示音
 * 單擊 toast 激活 app 後(前臺方式激活),如何獲取相關信息請參見 Demo.xaml.cs 中的代碼
 * 
 * 
 * 本例 xml 說明:
 * audio - 提示音
 *     src - 播放的提示音的地址。支持系統提示音,ms-appx 地址的音頻文件,ms-appdata 地址的音頻文件
 *     loop - 是否循環,默認值爲 false
 *     silent - 是否靜音,默認值爲 false
 */

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

namespace Windows10.Notification.Toast
{
    public sealed partial class Audio : Page
    {
        public Audio()
        {
            this.InitializeComponent();
        }

        // 彈出 toast 通知(系統提示音)
        private void buttonShowToast1_Click(object sender, RoutedEventArgs e)
        {
            // 清除本 app 的以前的所有 toast 通知
            // ToastNotificationManager.History.Clear();

            string toastXml = $@"
                <toast activationType='foreground' launch='Notification-Toast-Audio-Arguments 1'>
                    <visual>
                        <binding template='ToastGeneric'>
                            <text>audio</text>
                            <text>演示 toast 的提示音</text>
                        </binding>
                    </visual>
                    <audio src='{cmbSrc.SelectionBoxItem}' loop='{cmbLoop.SelectionBoxItem}' silent='{cmbSilent.SelectionBoxItem}' />
                </toast>";

            XmlDocument toastDoc = new XmlDocument();
            toastDoc.LoadXml(toastXml);
            
            ToastNotification toastNotification = new ToastNotification(toastDoc);
            ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
            toastNotifier.Show(toastNotification);
        }

        // 彈出 toast 通知(ms-appx 或 ms-appdata 地址的音頻文件)
        private void buttonShowToast2_Click(object sender, RoutedEventArgs e)
        {
            // 清除本 app 的以前的所有 toast 通知
            // ToastNotificationManager.History.Clear();

            string toastXml = $@"
                <toast activationType='foreground' launch='Notification-Toast-Audio-Arguments 1'>
                    <visual>
                        <binding template='ToastGeneric'>
                            <text>audio</text>
                            <text>演示 toast 的提示音</text>
                        </binding>
                    </visual>
                    <audio src='ms-appx:///Assets/audio.aac' />
                </toast>";

            XmlDocument toastDoc = new XmlDocument();
            toastDoc.LoadXml(toastXml);

            ToastNotification toastNotification = new ToastNotification(toastDoc);
            ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
            toastNotifier.Show(toastNotification);
        }
    }
}


二、本例用於演示 toast 的不一樣特定場景
Notification/Toast/Scenario.xamlexpress

<Page
    x:Class="Windows10.Notification.Toast.Scenario"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Notification.Toast"
    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="buttonShowToast1" Content="顯示 toast(默認場景)" Click="buttonShowToast1_Click" Margin="5" />

            <Button Name="buttonShowToast2" Content="顯示 toast(reminder 場景)" Click="buttonShowToast2_Click" Margin="5" />

            <Button Name="buttonShowToast3" Content="顯示 toast(alarm 場景)" Click="buttonShowToast3_Click" Margin="5" />

            <Button Name="buttonShowToast4" Content="顯示 toast(incomingCall 場景)" Click="buttonShowToast4_Click" Margin="5" />

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

Notification/Toast/Scenario.xaml.cswindows

/*
 * 本例用於演示 toast 的不一樣特定場景
 * 單擊 toast 激活 app 後(前臺方式激活),如何獲取相關信息請參見 Demo.xaml.cs 中的代碼
 * 
 * 
 * 本例 xml 說明:
 * scenario - 場景類別,不指定則爲默認場景
 *     default - 默認場景
 *     reminder - 彈出的 toast 框不會消失
 *     alarm - 彈出的 toast 框不會消失,且使用鬧鈴提示音
 *     incomingCall - 彈出的 toast 框不會消失,且按鈕樣式會與其餘場景不一樣(在 mobile app 中會全屏顯示)
 */

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

namespace Windows10.Notification.Toast
{
    public sealed partial class Scenario : Page
    {
        public Scenario()
        {
            this.InitializeComponent();
        }

        // 彈出 toast 通知(默認場景)
        private void buttonShowToast1_Click(object sender, RoutedEventArgs e)
        {
            // 清除本 app 的以前的所有 toast 通知
            // ToastNotificationManager.History.Clear();

            string toastXml = @"
                <toast activationType='foreground' launch='Notification-Toast-Scenario-Arguments 1'>
                    <visual>
                        <binding template='ToastGeneric'>
                            <text>toast - title</text>
                            <text>toast - content 1</text>
                        </binding>
                    </visual>
                </toast>";

            XmlDocument toastDoc = new XmlDocument();
            toastDoc.LoadXml(toastXml);

            ToastNotification toastNotification = new ToastNotification(toastDoc);
            ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
            toastNotifier.Show(toastNotification);
        }

        // 彈出 toast 通知(reminder 場景)
        // 經測試,沒有按鈕的話則沒法實現 reminder 場景的特性
        private void buttonShowToast2_Click(object sender, RoutedEventArgs e)
        {
            // 清除本 app 的以前的所有 toast 通知
            // ToastNotificationManager.History.Clear();

            string toastXml = @"
                <toast activationType='foreground' scenario='reminder' launch='Notification-Toast-Scenario-Arguments 2'>
                    <visual>
                        <binding template='ToastGeneric'>
                            <text>toast - title</text>
                            <text>toast - content 2</text>
                        </binding>
                    </visual>
                    <actions>
                        <action content='確認' arguments='confirm' />
                    </actions>
                </toast>";

            XmlDocument toastDoc = new XmlDocument();
            toastDoc.LoadXml(toastXml);

            ToastNotification toastNotification = new ToastNotification(toastDoc);
            ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
            toastNotifier.Show(toastNotification);
        }

        // 彈出 toast 通知(alarm 場景)
        // 經測試,沒有按鈕的話則沒法實現 alarm 場景的特性
        private void buttonShowToast3_Click(object sender, RoutedEventArgs e)
        {
            // 清除本 app 的以前的所有 toast 通知
            // ToastNotificationManager.History.Clear();

            string toastXml = @"
                <toast activationType='foreground' scenario='alarm' launch='Notification-Toast-Scenario-Arguments 3'>
                    <visual>
                        <binding template='ToastGeneric'>
                            <text>toast - title</text>
                            <text>toast - content 3</text>
                        </binding>
                    </visual>
                    <actions>
                        <action content='確認' arguments='confirm' />
                    </actions>
                </toast>";

            XmlDocument toastDoc = new XmlDocument();
            toastDoc.LoadXml(toastXml);

            ToastNotification toastNotification = new ToastNotification(toastDoc);
            ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
            toastNotifier.Show(toastNotification);
        }

        // 彈出 toast 通知(incomingCall 場景)
        private void buttonShowToast4_Click(object sender, RoutedEventArgs e)
        {
            // 清除本 app 的以前的所有 toast 通知
            // ToastNotificationManager.History.Clear();

            string toastXml = @"
                <toast activationType='foreground' scenario='incomingCall' launch='Notification-Toast-Scenario-Arguments 4'>
                    <visual>
                        <binding template='ToastGeneric'>
                            <text>toast - title</text>
                            <text>toast - content 4</text>
                        </binding>
                    </visual>
                    <actions>
                        <action content='確認' arguments='confirm' />
                    </actions>
                </toast>";

            XmlDocument toastDoc = new XmlDocument();
            toastDoc.LoadXml(toastXml);

            ToastNotification toastNotification = new ToastNotification(toastDoc);
            ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
            toastNotifier.Show(toastNotification);
        }
    }
}



OK
[源碼下載]app

相關文章
相關標籤/搜索