標籤:app開發 更新 解析度 動態 選擇
動態磚是什麼,相信大家用了這麼久的Windows 8/8.1/10早就非常瞭解了吧。
像什麼小磁貼、中磁貼、寬磁貼、大磁貼,還有這裡的市集Logo等,大家在下面根據不同的解析度選擇合適的圖片就好啦。
下面來做一個更新磁貼頁面的功能,這是頁面XML部分。
<StackPanel Margin="12"> <StackPanel Orientation="Horizontal"> <TextBlock FontSize="28" Text="選擇模板:" VerticalAlignment="Center"/> <ComboBox x:Name="comboBoxTile" Width="400" SelectionChanged="comboBoxTile_SelectionChanged"/> </StackPanel> <TextBox x:Name="textBoxXML" TextWrapping="Wrap" FontSize="22" Header="XML文檔" Width="420" Height="320" HorizontalAlignment="Left" Margin="12"/> <Button Name="btnTile" Content="更新磁貼" Click="btnTile_Click" Style="{StaticResource StyleToastButton}"/></StackPanel>
在後台代碼的Main函數中,擷取TileTemplateType枚舉並綁定到ComboBox上。
var itemsTile = Enum.GetNames(typeof(TileTemplateType));this.comboBoxTile.ItemsSource = itemsTile;
下面的代碼和前面的Toast真的非常類似,所以我才把這兩節連在一起來寫了。Button按鈕的Click事件中,和之前一樣建一個XML,然後載入到TileNotification類的執行個體中。最後就是TileUpdateManager類,也就是磁貼更新。
private void btnTile_Click(object sender, RoutedEventArgs e){ if (this.textBoxXML.Text == "") return; XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(this.textBoxXML.Text); TileNotification tileNotifi = new TileNotification(xdoc); TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotifi);}private void comboBoxTile_SelectionChanged(object sender, SelectionChangedEventArgs e){ TileTemplateType tileTemplate = (TileTemplateType)Enum.Parse(typeof(TileTemplateType), this.comboBoxTile.SelectedItem as string); XmlDocument xdoc = TileUpdateManager.GetTemplateContent(tileTemplate); this.textBoxXML.Text = xdoc.GetXml();}
當然了,如果你的APP不滿足於一個磁貼,你也可以建立第二個磁貼喲!
依舊和Toast通知的XML類似,它也有好多屬性的……
Arguments:使用該字串參數在通過次要磁貼啟動應用程式時會傳遞給Application類的OnLaunched方法,這樣一來應用程式就可以根據傳入的參數來執行特定的操作。
BackgroundColor:設定磁貼的背景色。
DisplayName和ShortName:設定顯示在磁貼上的文本。
Logo等:設定磁貼的表徵圖,用Uri。
ForegroundText:磁貼上文本的顏色,可用的選項有深色、淺色等。
TileID:設定磁貼的唯一標識ID,建立新磁貼前用SecondaryTile.Exists判斷是否已經存在。
在添加第二磁貼的Button的Click事件中:
private async void btnCreateTile(object sender, RoutedEventArgs e){ if(SecondaryTile.Exists(textTileID.Text)) { textBlockMsg.Text="該ID磁貼已經存在"; return ; } Uri uriImg=new Uri("ms-appx:///Assests/uriImg.png"); …… …… // 建立第二磁貼 SecondaryTile secTile=new SecondaryTile(); this.Tag=secTile; secTile.DisplayName=textBlockDisplayName.Text; secTile.TileID=textBlockID.Text; secTile.Arguments="second"; // 在後面有用到 // 設定表徵圖 secTile.VisualElements.BackgroundColor=Windows.UI.Colors.Gold; …… …… bool r=await secTile.RequestCreateAsync(); textBlockMsg.Text=r == true ?"磁貼建立成功啦.":"磁貼建立失敗了哎."; // 返回測試結果
如果希望點擊第二磁貼導航到特定的頁面,就需要重寫該頁面的OnNavigatedTo方法。
preteced async override void OnNavigatedTo(NavigationEventArgs e){ if(e.Parameter is Windows.ApplicationModel.Activation.LaunchActivatedEventArgs) { var arg=e.Parameter as Windows.ApplicationModel.Activation.LaunchActivateEventArgs; …… }}if(rootFrame.Content==null){ if(e.Arguments=="second") rootFrame.Navigate(typeof(OtherPage),e); else rootFrame.Navigate(typeof(MainPage));}
這裡的參數”second”就是上面設定那個Arguments哦,它的作用就在於這裡呢。
感謝您的訪問,希望對您有所協助。 歡迎大家關注、收藏以及評論。
為使本文得到斧正和提問,轉載請註明出處:
http://blog.csdn.net/nomasp
【萬裡征程——Windows App開發】動態磚