(本記事はVisualStudio2012RC+Windows8RP環境で確認しております。製品版では動作が異なる可能性があるのでご注意ください。)
トースト通知はライブタイル同様にアプリを起動していないユーザーにアプリの起動を促すことのできる重要な要素です。
トースト通知にも色々なパターンがあるのですが、まずは簡単に動作を確認してみましょう。
トースト通知を利用するにはマニフェストファイルの編集が必要です。
アプリケーションUIタブの「トースト対応」を「はい」に変更します。
今回は単純にトースト通知の起点はボタンイベントにします。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Button Content="Button" HorizontalAlignment="Left" Margin="207,100,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
</Grid>
ボタンクリックでトースト表示します。
private void Button_Click_1(object sender, RoutedEventArgs e)
{
ToastTemplateType toastTemplate = ToastTemplateType.ToastText01;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode("Toast!!"));
ToastNotification toast = new ToastNotification(toastXml);
toast.ExpirationTime = DateTimeOffset.UtcNow.AddSeconds(30);
toast.Failed += toast_Failed;
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
void toast_Failed(ToastNotification sender, ToastFailedEventArgs args)
{
System.Diagnostics.Debug.WriteLine(args.ErrorCode);
}
これで画面右上にトースト通知が表示されます。
Please give us your valuable comment