Windows 8 Store Apps學習(35) 通知: Toast 詳解

來源:互聯網
上載者:User

介紹

重新想象 Windows 8 Store Apps 之 通知

Toast - 基本應用參見 http://www.cnblogs.com/webabcd/archive/2013/06/17/3139740.html

Toast - 純文字 toast

Toast - 圖文 toast

Toast - toast 的提示音

Toast - 按計劃彈出 toast

樣本

1、示範純文字 toast 的 4 個模板

Notification/Toast/ToastWithText.xaml

<Page    x:Class="XamlDemo.Notification.Toast.ToastWithText"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:XamlDemo.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="120 0 0 0">                <TextBox Name="lblMsg" Height="100" TextWrapping="Wrap" AcceptsReturn="True" FontSize="14.667" Margin="0 0 10 0" />                <Button Name="btnTextBodyWrap" Content="TextBodyWrap" Click="btnTextBodyWrap_Click_1" Margin="0 10 0 0" />                <Button Name="bntTextHeadingTextBodyWrap" Content="TextHeading TextBodyWrap" Click="bntTextHeadingTextBodyWrap_Click_1" Margin="0 10 0 0" />                <Button Name="bntTextHeadingWrapTextBody" Content="TextHeadingWrap TextBody" Click="bntTextHeadingWrapTextBody_Click_1" Margin="0 10 0 0" />                <Button Name="bntTextHeadingTextBody" Content="TextHeading TextBody1 TextBody2" Click="bntTextHeadingTextBody_Click_1" Margin="0 10 0 0" />                        </StackPanel>    </Grid></Page>

Notification/Toast/ToastWithText.xaml.cs

/* * 示範純文字 toast 的 4 個模板 * 本樣本的 Toast 的 XmlDocument 內容構造器採用一個開源項目,具體代碼見:NotificationsExtensions/ToastContent.cs *  * XmlDocument GetTemplateContent(ToastTemplateType type) - 擷取系統支援的 Toast 模板 *     ToastTemplateType.ToastText01, ToastTemplateType.ToastText02, ToastTemplateType.ToastText03, ToastTemplateType.ToastText04 */    using NotificationsExtensions.ToastContent;using Windows.UI.Notifications;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;    namespace XamlDemo.Notification.Toast{    public sealed partial class ToastWithText : Page    {        public ToastWithText()        {            this.InitializeComponent();        }            private void btnTextBodyWrap_Click_1(object sender, RoutedEventArgs e)        {            IToastText01 templateContent = ToastContentFactory.CreateToastText01();            templateContent.TextBodyWrap.Text = "我是通知本文,可換行,最多三行。我是通知本文,可換行,最多三行。我是通知本文,可換行,最多三行。";            IToastNotificationContent toastContent = templateContent;                ToastNotification toast = toastContent.CreateNotification();            ToastNotificationManager.CreateToastNotifier().Show(toast);                lblMsg.Text = toastContent.GetContent();        }            private void bntTextHeadingTextBodyWrap_Click_1(object sender, RoutedEventArgs e)        {            IToastText02 templateContent = ToastContentFactory.CreateToastText02();            templateContent.TextHeading.Text = "我是通知標題,不可以換行。我是通知標題,不可以換行。";            templateContent.TextBodyWrap.Text = "我是通知本文,可換行,最多兩行。我是通知本文,可換行,最多兩行。";            IToastNotificationContent toastContent = templateContent;                ToastNotification toast = toastContent.CreateNotification();            ToastNotificationManager.CreateToastNotifier().Show(toast);                lblMsg.Text = toastContent.GetContent();        }            private void bntTextHeadingWrapTextBody_Click_1(object sender, RoutedEventArgs e)        {            IToastText03 templateContent = ToastContentFactory.CreateToastText03();            templateContent.TextHeadingWrap.Text = "我是通知標題,可換行,最多兩行。我是通知標題,可換行,最多兩行。";            templateContent.TextBody.Text = "我是通知本文,不可以換行。我是通知本文,不可以換行。";            IToastNotificationContent toastContent = templateContent;                ToastNotification toast = toastContent.CreateNotification();            ToastNotificationManager.CreateToastNotifier().Show(toast);                lblMsg.Text = toastContent.GetContent();        }            private void bntTextHeadingTextBody_Click_1(object sender, RoutedEventArgs e)        {            IToastText04 templateContent = ToastContentFactory.CreateToastText04();            templateContent.TextHeading.Text = "我是通知標題,不可以換行。我是通知標題,不可以換行。";            templateContent.TextBody1.Text = "我是通知本文1,不可以換行。我是通知本文1,不可以換行。";            templateContent.TextBody2.Text = "我是通知本文2,不可以換行。我是通知本文2,不可以換行。";            IToastNotificationContent toastContent = templateContent;                ToastNotification toast = toastContent.CreateNotification();            ToastNotificationManager.CreateToastNotifier().Show(toast);                lblMsg.Text = toastContent.GetContent();        }    }}

2、示範圖文 toast 的 4 個模板

Notification/Toast/ToastWithImageText.xaml

<Page    x:Class="XamlDemo.Notification.Toast.ToastWithImageText"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:XamlDemo.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="120 0 0 0">                <TextBox Name="lblMsg" Height="100" TextWrapping="Wrap" AcceptsReturn="True" FontSize="14.667" Margin="0 0 10 0" />                <Button Name="btnTextBodyWrap" Content="TextBodyWrap" Click="btnTextBodyWrap_Click_1" Margin="0 10 0 0" />                <Button Name="bntTextHeadingTextBodyWrap" Content="TextHeading TextBodyWrap" Click="bntTextHeadingTextBodyWrap_Click_1" Margin="0 10 0 0" />                <Button Name="bntTextHeadingWrapTextBody" Content="TextHeadingWrap TextBody" Click="bntTextHeadingWrapTextBody_Click_1" Margin="0 10 0 0" />                <Button Name="bntTextHeadingTextBody" Content="TextHeading TextBody1 TextBody2" Click="bntTextHeadingTextBody_Click_1" Margin="0 10 0 0" />                        </StackPanel>    </Grid></Page>

Notification/Toast/ToastWithImageText.xaml.cs

/* * 示範圖文 toast 的 4 個模板(註:圖片不能大於 1024*1024 像素,不能大於 200KB) * 本樣本的 Toast 的 XmlDocument 內容構造器採用一個開源項目,具體代碼見:NotificationsExtensions/ToastContent.cs *  * XmlDocument GetTemplateContent(ToastTemplateType type) - 擷取系統支援的 Toast 模板 *     ToastTemplateType.ToastImageAndText01, ToastTemplateType.ToastImageAndText02, ToastTemplateType.ToastImageAndText03, ToastTemplateType.ToastImageAndText04 *      * 註:圖片可以來自程式包內,可以來自 Application Data(僅支援對 local 中圖片檔案的引用),可以來自一個 http 的遠程地址 */    using NotificationsExtensions.ToastContent;using System;using Windows.UI.Notifications;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;    namespace XamlDemo.Notification.Toast{    public sealed partial class ToastWithImageText : Page    {        public ToastWithImageText()        {            this.InitializeComponent();        }            private void btnTextBodyWrap_Click_1(object sender, RoutedEventArgs e)        {            IToastImageAndText01 templateContent = ToastContentFactory.CreateToastImageAndText01();            templateContent.TextBodyWrap.Text = "我是通知本文,可換行,最多三行。我是通知本文,可換行,最多三行。我是通知本文,可換行,最多三行。";            templateContent.Image.Src = "Assets/Logo.png"; // 用程式包內檔案作通知圖片            templateContent.Image.Alt = "altText";            IToastNotificationContent toastContent = templateContent;                ToastNotification toast = toastContent.CreateNotification();            ToastNotificationManager.CreateToastNotifier().Show(toast);                lblMsg.Text = toastContent.GetContent();        }            private void bntTextHeadingTextBodyWrap_Click_1(object sender, RoutedEventArgs e)        {            IToastImageAndText02 templateContent = ToastContentFactory.CreateToastImageAndText02();            templateContent.TextHeading.Text = "我是通知標題,不可以換行。我是通知標題,不可以換行。";            templateContent.TextBodyWrap.Text = "我是通知本文,可換行,最多兩行。我是通知本文,可換行,最多兩行。";            templateContent.Image.Src = "ms-appx:///Assets/Logo.png"; // 用程式包內檔案作通知圖片            templateContent.Image.Alt = "altText";            IToastNotificationContent toastContent = templateContent;                ToastNotification toast = toastContent.CreateNotification();            ToastNotificationManager.CreateToastNotifier().Show(toast);                lblMsg.Text = toastContent.GetContent();        }            private void bntTextHeadingWrapTextBody_Click_1(object sender, RoutedEventArgs e)        {            IToastImageAndText03 templateContent = ToastContentFactory.CreateToastImageAndText03();            templateContent.TextHeadingWrap.Text = "我是通知標題,可換行,最多兩行。我是通知標題,可換行,最多兩行。";            templateContent.TextBody.Text = "我是通知本文,不可以換行。我是通知本文,不可以換行。";            templateContent.Image.Src = "ms-appdata:///local/Logo.png"; // 用 Application Data 內檔案作通知圖片(註:僅支援 local 中的圖片)            templateContent.Image.Alt = "altText";            IToastNotificationContent toastContent = templateContent;                ToastNotification toast = toastContent.CreateNotification();            ToastNotificationManager.CreateToastNotifier().Show(toast);                lblMsg.Text = toastContent.GetContent();        }            private void bntTextHeadingTextBody_Click_1(object sender, RoutedEventArgs e)        {            IToastImageAndText04 templateContent = ToastContentFactory.CreateToastImageAndText04();            templateContent.TextHeading.Text = "我是通知標題,不可以換行。我是通知標題,不可以換行。";            templateContent.TextBody1.Text = "我是通知本文1,不可以換行。我是通知本文1,不可以換行。";            templateContent.TextBody2.Text = "我是通知本文2,不可以換行。我是通知本文2,不可以換行。";            templateContent.Image.Src = "http://pic.cnblogs.com/avatar/a14540.jpg?id=24173245"; // 用遠程檔案作通知圖片            templateContent.Image.Alt = "altText";            IToastNotificationContent toastContent = templateContent;                ToastNotification toast = toastContent.CreateNotification();            ToastNotificationManager.CreateToastNotifier().Show(toast);                lblMsg.Text = toastContent.GetContent();        }    }}

3、示範 Toast 的提示音

Notification/Toast/ToastWithSound.xaml

<Page    x:Class="XamlDemo.Notification.Toast.ToastWithSound"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:XamlDemo.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="120 0 0 0">                <TextBox Name="lblMsg" Height="100" TextWrapping="Wrap" AcceptsReturn="True" FontSize="14.667" Margin="0 0 10 0" />                <TextBlock Text="通知提示音列表" Margin="0 10 0 0" />            <ListBox Name="listBox" SelectionChanged="listBox_SelectionChanged_1" Margin="0 10 10 0">                <ListBoxItem Content="Default" />                <ListBoxItem Content="Mail" />                <ListBoxItem Content="SMS" />                <ListBoxItem Content="IM" />                <ListBoxItem Content="Reminder" />                <ListBoxItem Content="LoopingCall" />                <ListBoxItem Content="LoopingCall2" />                <ListBoxItem Content="LoopingAlarm" />                <ListBoxItem Content="LoopingAlarm2" />                <ListBoxItem Content="Silent" />            </ListBox>        </StackPanel>    </Grid></Page>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.