前面我們使用了 MessageDialog 來(lái)作為彈窗,這里來(lái)介紹一個(gè)更加高大上的 Toast 通知。
Toast 通知本質(zhì)上動(dòng)力是由XML來(lái)提供的,一開(kāi)始我還不相信不知道XML原來(lái)有這么大的威力?,F(xiàn)在就來(lái)看看和 Toast 相關(guān)的知識(shí)。
1)實(shí)例化 ToastNotification 類(lèi)。
ToastNotification toast1 = new ToastNotification(xdoc);
2)使用 ToastNotificationManager 來(lái)管理 Toast 通知,包括添加、展示、移除、獲取通知等等。
ToastNotificationManager.CreateToastNotifier().Show(toast1);
3)在第一步中的 xdoc 就是一段 XML 數(shù)據(jù),它從何而來(lái)呢?
XmlDocument xdoc = new XmlDocument();
4)上一步代碼實(shí)例化了一個(gè) XML,但是它沒(méi)有數(shù)據(jù)呀,數(shù)據(jù)從哪來(lái)呢?
xdoc.LoadXml(txtXML.Text);
5)這段代碼就將一段 Text 導(dǎo)入了 XML 中。而 Text 數(shù)據(jù)有很多種獲取方式。在下文中自然會(huì)提到。
Toast 的 XML 模板有許多,我們可以直接來(lái)獲取它們。用枚舉和強(qiáng)大的 var 即可。
var items = Enum.GetNames(typeof(ToastTemplateType));
那么就正式開(kāi)工了,因?yàn)橹貜?fù)的屬性太多了我就大概設(shè)置了 2 個(gè) Style 資源。
<Page.Resources>
<Style TargetType="TextBlock" x:Key="StyleHeaderTextBlock">
<Setter Property="FontSize" Value="40"/>
<Setter Property="FontFamily" Value="華文琥珀"/>
<Setter Property="Foreground" Value="HotPink"/>
<Setter Property="Margin" Value="12"/>
</Style>
<Style TargetType="Button" x:Key="StyleToastButton">
<Setter Property="Width" Value="180"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Background" Value="Aqua"/>
<Setter Property="FontSize" Value="21"/>
<Setter Property="Margin" Value="12"/>
<Setter Property="Content" Value="顯示Toast通知" />
</Style>
</Page.Resources>
下面是第一部分用于生成 Toast 通知。
<StackPanel Orientation="Vertical" Grid.Column="0" Margin="12">
<TextBlock Text="生成Toast通知" Style="{StaticResource StyleHeaderTextBlock}"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock FontSize="24" Foreground="Wheat" Text="請(qǐng)選擇一個(gè)模板:" VerticalAlignment="Center"/>
<ComboBox Name="comboBoxToast" Foreground="Green" Width="275"
SelectionChanged="comboBoxToast_SelectionChanged"/>
</StackPanel>
<TextBox Foreground="Green" x:Name="txtXML" HorizontalAlignment="Left" Width="500"
Height="400" Header="模板X(qián)ML:" TextWrapping="Wrap" FontSize="24"/>
<Button Name="btnShowToast1" Click="btnShowToast1_Click" Style="{StaticResource StyleToastButton}"/>
</StackPanel>
后臺(tái)代碼也蠻容易的,利用上面講的就好了。
public MainPage()
{
this.InitializeComponent();
var items = Enum.GetNames(typeof(ToastTemplateType));
this.comboBoxToast.ItemsSource = items;
}
private void comboBoxToast_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string tempt = ((ComboBox)sender).SelectedItem as string;
if (!string.IsNullOrEmpty(tempt))
{
ToastTemplateType template = (ToastTemplateType)Enum.Parse(typeof(ToastTemplateType), tempt);
XmlDocument xdoc = ToastNotificationManager.GetTemplateContent(template);
txtXML.Text = xdoc.GetXml();
}
}
private void btnShowToast1_Click(object sender, RoutedEventArgs e)
{
if (txtXML.Text == "")
return;
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(txtXML.Text);
ToastNotification toast1 = new ToastNotification(xdoc);
ToastNotificationManager.CreateToastNotifier().Show(toast1);
}
模板是這樣用的……
在 src 中填入圖片的路徑也可以在 Toast 中顯示圖像哦,趕緊試試吧……
接下來(lái)是第二段啦,和前面的很是類(lèi)似……
<StackPanel Orientation="Vertical" Grid.Column="1">
<TextBlock Text="更改Toast通知的提示音" Style="{StaticResource StyleHeaderTextBlock}"/>
<TextBlock Margin="12" Text="請(qǐng)輸入Toast消息內(nèi)容:" FontSize="24"/>
<TextBox Margin="12" Height="50" x:Name="txtMesaage"/>
<TextBlock Margin="12" FontSize="24" Text="請(qǐng)選擇一種提示聲音:"/>
<ComboBox Margin="12" Height="50" x:Name="comboBoxAudio" Width="400" HorizontalAlignment="Left">
<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.Looping.Alarm</ComboBoxItem>
<ComboBoxItem>ms-winsoundevent:Notification.Looping.Call</ComboBoxItem>
</ComboBox>
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="checkBoxLoop" Margin="12" Content="循環(huán)播放"/>
<CheckBox x:Name="checkBoxSilent" Margin="12" Content="靜音"/>
</StackPanel>
<Button Name="btnShowToast2" Click="btnShowToast2_Click" Style="{StaticResource StyleToastButton}"/>
</StackPanel>
上面代碼中的“ms-winsoundevent:Notification.Default”都是填到 src 中的用于設(shè)置聲音,還可以在 loop、silent 中設(shè)置是否循環(huán)以及是否靜音,那到底該怎么用呢?應(yīng)該將這些屬性全部都填入到 XML 中。
xmlContent = string.Format(
"<toast duration='{0}'>" +
"<visual>" +
"<binding template='ToastText01'>" +
"<text id='1'>{1}</text>" +
"</binding>" +
"</visual>" +
"<audio src='{2}' loop='{3}' silent='{4}'/>" +
"</toast>",
toastDuration, msg, audioSrc, isLoop, isSilent
);
上面用的 xmlContent 也要先定義出來(lái),一開(kāi)始設(shè)置為 Empty 就好。
string xmlContent = string.Empty;
isLoop 和 isSilent 屬性都可以借助于三目運(yùn)算符在 CheckBox 中獲取來(lái)。
string isLoop = checkBoxLoop.IsChecked == true ? "true" : "false";
string audioSrc = (comboBoxAudio.SelectedItem as ComboBoxItem).Content.ToString();
string toastDuration = checkBoxLoop.IsChecked == true ? "long" : "short";
string isSilent = checkBoxSilent.IsChecked == true ? "true" : "false";
當(dāng)然,考慮得更加周到些,用戶可以在還沒(méi)有輸入通知內(nèi)容就點(diǎn)了顯示 Toast 通知按鈕,對(duì)此用三目運(yùn)算符也是極好的選擇。
string msg = txtMesaage.Text == "" ? "你還沒(méi)有輸入Toast通知的內(nèi)容呢……" : txtMesaage.Text;
這些準(zhǔn)備工作都寫(xiě)好了以后呢就該設(shè)置 Toast 通知了,和上面的 Toast1 類(lèi)似哦,大家試試。
可是這些通知都沒(méi)有時(shí)間性可言,因?yàn)橛袝r(shí)候我們需要定在一個(gè)時(shí)間來(lái)執(zhí)行 Toast 通知。這自然也是可以實(shí)現(xiàn)的。
先作如下界面設(shè)計(jì)。
<StackPanel Orientation="Vertical" Grid.Column="2">
<TextBlock Text="計(jì)劃時(shí)間顯示Toast通知" Style="{StaticResource StyleHeaderTextBlock}"/>
<StackPanel Orientation="Horizontal" Height="60">
<TextBlock FontSize="28" Text="計(jì)劃在" VerticalAlignment="Center"/>
<TextBox Name="tBoxTime" FontSize="28" Width="60" Height="45" VerticalAlignment="Center"/>
<TextBlock FontSize="28" Text="秒后顯示Toast通知" VerticalAlignment="Center"/>
</StackPanel>
<Button Name="btnShowToast3" Click="btnShowToast3_Click" Style="{StaticResource StyleToastButton}"/>
</StackPanel>
后臺(tái)代碼如下。
private async void btnShowToast3_Click(object sender, RoutedEventArgs e)
{
int toastTime;
try
{
toastTime = int.Parse(tBoxTime.Text.ToString());
XmlDocument xdoc = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
var txtnodes = xdoc.GetElementsByTagName("text");
txtnodes[0].InnerText = "你好,這是一條定時(shí)為"+toastTime.ToString()+ "秒的Toast消息。";
ScheduledToastNotification toast3 = new ScheduledToastNotification(xdoc,
DateTimeOffset.Now.AddSeconds(toastTime));
ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast3);
}
catch (Exception ex)
{
Windows.UI.Popups.MessageDialog messageDialog =
new Windows.UI.Popups.MessageDialog(ex.Message);
await messageDialog.ShowAsync();
}
}
在這個(gè)小程序中因?yàn)閭?cè)重于講解定時(shí)而非 Toast 的通知樣式,因此就選用了比較簡(jiǎn)單的 ToastText01 模板。而后找出 Text 節(jié)點(diǎn),并向該節(jié)點(diǎn)寫(xiě)入內(nèi)容。最后就是創(chuàng)建 Toast 通知了。
ScheduledToastNotification toast3 = new ScheduledToastNotification
(xdoc,DateTimeOffset.Now.AddSeconds(toastTime));
同樣為了防止用戶沒(méi)有在 TextBox 中輸入時(shí)間或輸入了錯(cuò)誤格式的時(shí)間比如“5 秒”而做了 try、catch 異常檢測(cè)。當(dāng)然了,在實(shí)際產(chǎn)品中,這里可就要做得更加完美了,時(shí)間用 TextBox 來(lái)輸入并不是一件好事,而應(yīng)用我們前面介紹的 TimePicker。
給這 3 段程序來(lái)張全家福吧~
更多建議: