使用動(dòng)態(tài)磁貼示例

2018-08-12 21:55 更新

使用動(dòng)態(tài)磁貼示例

動(dòng)態(tài)磁貼是什么,相信大家用了這么久的 Windows 8/8.1/10 早就非常了解了吧。

像什么小磁貼、中磁貼、寬磁貼、大磁貼,還有這里的應(yīng)用商店 Logo 等,大家在下面根據(jù)不同的分辨率選擇合適的圖片就好啦。

下面來做一個(gè)更新磁貼頁面的功能,這是頁面 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>

在后臺(tái)代碼的 Main 函數(shù)中,獲取 TileTemplateType 枚舉并綁定到 ComboBox 上。

var itemsTile = Enum.GetNames(typeof(TileTemplateType));
this.comboBoxTile.ItemsSource = itemsTile;

下面的代碼和前面的 Toast 真的非常類似,所以我才把這兩節(jié)連在一起來寫了。Button 按鈕的 Click 事件中,和之前一樣建一個(gè) XML,然后加載到 TileNotification 類的實(shí)例中。最后就是 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();
}

當(dāng)然了,如果你的 APP 不滿足于一個(gè)磁貼,你也可以創(chuàng)建第二個(gè)磁貼喲!

依舊和 Toast 通知的 XML 類似,它也有好多屬性的……

  • Arguments:使用該字符串參數(shù)在通過次要磁貼啟動(dòng)應(yīng)用程序時(shí)會(huì)傳遞給 Application 類的 OnLaunched 方法,這樣一來應(yīng)用程序就可以根據(jù)傳入的參數(shù)來執(zhí)行特定的操作。

  • BackgroundColor:設(shè)置磁貼的背景色。

  • DisplayName和ShortName:設(shè)置顯示在磁貼上的文本。

  • Logo等:設(shè)置磁貼的圖標(biāo),用 Uri。

  • ForegroundText:磁貼上文本的顏色,可用的選項(xiàng)有深色、淺色等。

  • TileID:設(shè)置磁貼的唯一標(biāo)識(shí) ID,創(chuàng)建新磁貼前用 SecondaryTile.Exists 判斷是否已經(jīng)存在。

在添加第二磁貼的 Button 的 Click 事件中:

private async void btnCreateTile(object sender, RoutedEventArgs e)
{
    if(SecondaryTile.Exists(textTileID.Text))
    {
        textBlockMsg.Text="該ID磁貼已經(jīng)存在";
        return ;
    }
    Uri uriImg=new Uri("ms-appx:///Assests/uriImg.png");
    ……
    ……
    // 創(chuàng)建第二磁貼
    SecondaryTile secTile=new SecondaryTile();
    this.Tag=secTile;
    secTile.DisplayName=textBlockDisplayName.Text;
    secTile.TileID=textBlockID.Text;
    secTile.Arguments="second"; // 在后面有用到
    // 設(shè)置圖標(biāo)
    secTile.VisualElements.BackgroundColor=Windows.UI.Colors.Gold;
    ……
    ……
    bool r=await secTile.RequestCreateAsync();
    textBlockMsg.Text=r == true ?"磁貼創(chuàng)建成功啦.":"磁貼創(chuàng)建失敗了哎.";  // 返回測(cè)試結(jié)果

如果希望點(diǎn)擊第二磁貼導(dǎo)航到特定的頁面,就需要重寫該頁面的 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));
}

這里的參數(shù)"second"就是上面設(shè)置那個(gè) Arguments 哦,它的作用就在于這里呢。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)