重新想象 Windows 8.1 Store Apps (85) - 警報通知(鬧鍾), Tile 的新特性
作者:webabcd
介紹
重新想象 Windows 8.1 Store Apps 之通知的新特性
- 警報通知(鬧鍾)
- Tile 的新特性
示例
1、演示 win8.1 中新增的警報 toast(鬧鍾)
AlarmToast.xaml
<Page x:Class="Windows81.Notification.AlarmToast" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows81.Notification" 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"> <TextBlock Name="lblMsg" FontSize="14.667" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
AlarmToast.xaml.cs
/* * 演示 win8.1 中新增的警報 toast(鬧鍾) * * 關於 toast 的基礎請參見:http://www.cnblogs.com/webabcd/archive/2013/06/20/3145356.html * * * 注: * 1、需要在 manifest 中設置支持 toast 通知和鎖屏通知 * 2、需要在 manifest 中的 Application/Extensions 內做如下設置 * <!--聲明此 app 是一個“鬧鍾”應用,其會出現在“設置”->“電腦和設備”->“鎖屏應用”->“選擇要顯示提醒的應用”列表中--> <m2:Extension Category="windows.alarm" /> <!--為了演示 Notification/AlarmToast 需要如下設置一個后台任務--> <Extension Category="windows.backgroundTasks" EntryPoint="SampleForAlarmToast"> <BackgroundTasks> <Task Type="audio" /> <Task Type="timer" /> </BackgroundTasks> </Extension> * 3、系統靜音也能播放聲音 * 4、不受免打擾時間的限制 */ using System; using Windows.ApplicationModel.Background; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows81.Notification { public sealed partial class AlarmToast : Page { public AlarmToast() { this.InitializeComponent(); this.Loaded += AlarmToast_Loaded; } protected async override void OnNavigatedTo(NavigationEventArgs e) { try { // 向系統請求成為鬧鍾應用的權限,會彈出確認對話框 await AlarmApplicationManager.RequestAccessAsync().AsTask(); // 獲取當前應用程序的成為鬧鍾應用的權限(AlarmAccessStatus 枚舉) // Unspecified - 用戶未響應應用程序設置警報的權限請求 // AllowedWithWakeupCapability - 用戶已經為應用程序授予設置警報的權限,並且警報可以將計算機從待機狀態喚醒 // AllowedWithoutWakeupCapability - 用戶已經為應用程序授予設置警報的權限,但是警報無法將計算機從待機狀態喚醒 // Denied - 用戶已拒絕該應用程序設置警報的權限 AlarmAccessStatus alarmAccessStatus = AlarmApplicationManager.GetAccessStatus(); lblMsg.Text = alarmAccessStatus.ToString(); } catch (Exception ex) { lblMsg.Text = ex.ToString(); } } void AlarmToast_Loaded(object sender, RoutedEventArgs e) { // 關於 toast 通知詳見:http://www.cnblogs.com/webabcd/archive/2013/06/20/3145356.html string toastXmlString = "<toast duration=\"long\">\n" + "<visual>\n" + "<binding template=\"ToastText02\">\n" + "<text id=\"1\">text1</text>\n" + "<text id=\"2\">text2</text>\n" + "</binding>\n" + "</visual>\n" + "<commands scenario=\"alarm\">\n" + // 除了 alarm 還有 incomingCall "<command id=\"snooze\"/>\n" + // snooze 代表在鬧鍾 toast 中顯示“暫停”按鈕(小睡一會) "<command id=\"dismiss\"/>\n" + // dismiss 代表在鬧鍾 toast 中顯示“取消”按鈕 "</commands>\n" + // 鬧鍾鈴聲: Notification.Default, Notification.IM, Notification.Mail, Notification.Reminder, Notification.SMS, Notification.Looping.Alarm, Notification.Looping.Alarm2, Notification.Looping.Alarm3, Notification.Looping.Alarm4, Notification.Looping.Alarm5, Notification.Looping.Alarm6, Notification.Looping.Alarm7, Notification.Looping.Alarm8, Notification.Looping.Alarm9, Notification.Looping.Alarm10, Notification.Looping.Call, Notification.Looping.Call2, Notification.Looping.Call3, Notification.Looping.Call4, Notification.Looping.Call5, Notification.Looping.Call6, Notification.Looping.Call7, Notification.Looping.Call8, Notification.Looping.Call9, Notification.Looping.Call10 "<audio src=\"Notification.Looping.Alarm2\" loop=\"true\" />\n" + "</toast>\n"; var toastDOM = new Windows.Data.Xml.Dom.XmlDocument(); toastDOM.LoadXml(toastXmlString); var toastNotifier = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier(); // 鬧鍾 toast 提示出現后,如果點擊“暫停”(snooze)按鈕,則第 3 個參數指定的時間過后繼續彈出鬧鍾 toast,本例是 120 秒 var customAlarmScheduledToast = new Windows.UI.Notifications.ScheduledToastNotification(toastDOM, DateTime.Now.AddSeconds(5), TimeSpan.FromSeconds(120), 0); toastNotifier.AddToSchedule(customAlarmScheduledToast); } } }
2、win8.1 中的 tile 模板增加到 75 個
Tile.xaml
<Page x:Class="Windows81.Notification.Tile" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows81.Notification" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <Grid.Resources> <Style x:Key="ItemTitleStyle" TargetType="TextBlock"> <Setter Property="FontSize" Value="14.667"/> </Style> <ItemsPanelTemplate x:Key="StoreFrontGridItemsPanelTemplate"> <WrapGrid MaximumRowsOrColumns="3" VerticalChildrenAlignment="Top" HorizontalChildrenAlignment="Left"/> </ItemsPanelTemplate> <Style x:Key="StoreFrontTileStyle" TargetType="GridViewItem"> <Setter Property="FontFamily" Value="Segoe UI" /> <Setter Property="Height" Value="310" /> <Setter Property="Width" Value="310" /> <Setter Property="Padding" Value="0" /> <Setter Property="Margin" Value="5" /> <Setter Property="HorizontalContentAlignment" Value="Left" /> <Setter Property="VerticalContentAlignment" Value="Top" /> <Setter Property="BorderThickness" Value="0"/> <Setter Property="TabNavigation" Value="Local" /> </Style> <DataTemplate x:Key="StoreFrontTileTemplate"> <Grid HorizontalAlignment="Left" Background="Transparent"> <StackPanel Orientation="Vertical"> <TextBlock TextWrapping="Wrap" VerticalAlignment="Center" Text="{Binding FileName}" HorizontalAlignment="Left" /> <Image Source="{Binding Path}" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0,10,0,0"/> </StackPanel> </Grid> </DataTemplate> </Grid.Resources> <!--顯示 75 種不同的 Tile 模板--> <GridView x:Name="gridView" Margin="120 0 0 0" ItemTemplate="{StaticResource StoreFrontTileTemplate}" ItemContainerStyle="{StaticResource StoreFrontTileStyle}" ItemsPanel="{StaticResource StoreFrontGridItemsPanelTemplate}" BorderBrush="LightGray" VerticalAlignment="Top" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" SelectionMode="None" /> </Grid> </Page>
Tile.xaml.cs
/* * win8.1 中的 tile 模板增加到 75 個 * * * 關於 tile 的基礎請參見:http://www.cnblogs.com/webabcd/archive/2013/06/24/3151864.html * * * win 8.1 中 tile 的新特性如下: * 1、tile 分 4 種:小 = Square70x70, 中等 = Square150x150, 長方形 = Wide310x150, 大 = Square310x310 * 2、可以在 manifest 中指定 app 安裝后默認的 tile 類型 * 3、使用 tile 模板時,其命名規則也變了,參見 NotificationsExtensions 項目(比如 win8 中的 TileSquareImage 在 win8.1 中變為了 TileSquare150x150Image) * 4、tile 基礎(通過 NotificationsExtensions 創建)參見:http://www.cnblogs.com/webabcd/archive/2013/06/24/3151864.html * 5、tile 基礎(通過手工構造 xml)參見:http://www.cnblogs.com/webabcd/archive/2013/06/17/3139740.html * 6、如果想要支持 Square310x310,則必須要支持 Wide310x150 * 7、建議為縮放比例 0.8x、1x、1.4x 和 1.8x 提供相對應的資源,以達到更好的顯示效果 * 8、tile 的通知隊列,除了 TileUpdater.EnableNotificationQueue() 外還支持只在指定大小的 tile 上啟用 tile 隊列:TileUpdater.EnableNotificationQueueForSquare150x150(), TileUpdater.EnableNotificationQueueForSquare310x310(), TileUpdater.EnableNotificationQueueForWide310x150() * 9、新增 SecondaryTile.PhoneticName,可以指定 SecondaryTile 的名字的拼音,系統將據此排序 UI * 10、為了同時支持 win8 和 win8.1 新增了 fallback 屬性,類似如下使用(找不到 TileSquare150x150Image 的話則用 TileSquareImage,找不到 TileWide310x150Image 的話則用 TileWideImage) <tile> <visual version="2"> <binding template="TileSquare150x150Image" fallback="TileSquareImage" branding="None"> <image id="1" src="Assets/Images/w6.png"/> </binding> <binding template="TileWide310x150Image" fallback="TileWideImage" branding="None"> <image id="1" src="Assets/Images/sq5.png"/> </binding> <binding template="TileSquare310x310Image" branding="None"> <image id="1" src="Assets/Images/sq6.png"/> </binding> </visual> </tile> */ using System; using System.Linq; using Windows.ApplicationModel; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows81.Notification { public sealed partial class Tile : Page { public Tile() { this.InitializeComponent(); } protected async override void OnNavigatedTo(NavigationEventArgs e) { // Windows81/Notification/tiles 文件夾內 75 張圖片分別用於演示 Tile 的 75 種模板 var folder = await Package.Current.InstalledLocation.GetFolderAsync(@"Notification\tiles"); var files = await folder.GetFilesAsync(); var dataSource = from p in files select new { FileName = p.DisplayName, Path = "ms-appx:///Notification/tiles/" + p.Name }; gridView.ItemsSource = dataSource; } } }
OK
[源碼下載]