码迷,mamicode.com
首页 > 移动开发 > 详细

【万里征程——Windows App开发】动态磁贴

时间:2015-05-20 00:38:28      阅读:146      评论:0      收藏:0      [点我收藏+]

标签: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开发】动态磁贴

标签:app开发   更新   分辨率   动态   选择   

原文地址:http://blog.csdn.net/nomasp/article/details/45851355

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!