与众不同 windows phone (8) - Tile(磁贴)

原文:与众不同 windows phone (8) - Tile(磁贴)

[索引页]
[源码下载]

与众不同 windows phone (8) - Tile(磁贴)

作者:webabcd

介绍
与众不同 windows phone 7.5 (sdk 7.1) 之磁贴

  • 概述
  • 演示如何创建、更新、删除磁贴
  • 演示如何按计划更新磁贴的正面背景图

示例
1、创建、更新、删除磁贴的 Demo
ShellTileDemo.xaml

<phone:PhoneApplicationPage
x:Class="Demo.Tile.ShellTileDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <StackPanel Orientation="Vertical">
<TextBlock Name="lblMsg" />
<Button Name="btnUpdateApplicationTile" Content="更新 Application Tile" Click="btnUpdateApplicationTile_Click" />
<Button Name="btnCreateSecondaryTile" Content="创建 Secondary Tile(可以创建多个)" Click="btnCreateSecondaryTile_Click" />
<Button Name="btnDeleteSecondaryTile" Content="删除全部 Secondary Tile" Click="btnDeleteSecondaryTile_Click" />
</StackPanel> </phone:PhoneApplicationPage>

ShellTileDemo.xaml.cs

/*
* Tile - 磁贴
* Tile 的大小是173 * 173;宽 Tile 的大小 356 * 173,需要把 manifest 中的 <TemplateType5> 修改为 <TemplateType6>,但是不会通过微软审核
* Tile 分为应用程序磁贴(Application Tile)和次要磁贴(Secondary Tile)
* 程序无虑如何都有一个 Application Tile(无论它是否被固定到了开始屏幕),只能更新它(不能创建和删除);而 Secondary Tile 是可以创建、更新和删除的,Secondary Tile 如果存在一定是在开始屏幕上
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; namespace Demo.Tile
{
public partial class ShellTileDemo : PhoneApplicationPage
{
public ShellTileDemo()
{
InitializeComponent();
} protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// 演示获取点击 Tile 后传递过来的参数数据
if (NavigationContext.QueryString.Count > )
lblMsg.Text = "参数 t 的值为:" + this.NavigationContext.QueryString["t"]; base.OnNavigatedTo(e);
} private void btnUpdateApplicationTile_Click(object sender, RoutedEventArgs e)
{
/*
* StandardTileData - 用于描述 Tile 的数据
* Title - 正面标题
* BackgroundImage - 正面背景
* Count - 正面显示的 badge (徽章),范围 1 - 99
* BackTitle - 背面标题
* BackBackgroundImage - 背面背景
* BackContent - 背面内容
*
*
* ShellTile - 用于管理应用程序Tile和次要Tile
* Update(StandardTileData data) - 使用指定的 Tile 数据更新已有的 Tile 信息
* Delete() - 删除此 Tile
* NavigationUri - 此 Tile 的导航地址
*
* ShellTile.ActiveTiles - 固定到开始屏幕的 Tile 集合。注意:其第一个元素必然是 application tile,无论其是否被固定到了首页
* ShellTile.Create(Uri navigationUri, ShellTileData initialData) - 创建一个新的 Secondary Tile(如果有 Secondary Tile,其必然是被固定到开始屏幕的)
* navigationUri - 点击 Tile 后所导航到的地址,此值相当于 key 值,不能重复
* initialData - 需要创建的 Tile 数据
*/ /*
* 注意:
* 创建次要磁贴时背景图必须使用本地资源(程序包内或独立存储中,如果是独立存储则图像必须位于 Shared/ShellContent)
* 更新应用程序磁贴或次要磁贴时,可以使用本地资源或远程资源来更新背景图像
* 磁贴图像可以是 jpg 或 png 或 gif(只显示第一帧),png 或 gif 的透明区域的背景会呈现主题色
* 当使用远程图像时,不能是https,要小于80KB,必须30秒内下载完
*/ // 创建应用程序 Tile 的 Demo
ShellTile applicationTile = ShellTile.ActiveTiles.First(); StandardTileData newTile = new StandardTileData
{
Title = "App Fore Tile Title",
BackgroundImage = new Uri("TileBackgroundBlue.png", UriKind.Relative),
Count = ,
BackTitle = "App Back Tile Title",
BackBackgroundImage = new Uri("TileBackgroundGreen.png", UriKind.Relative),
BackContent = "App Back Tile Content" + Environment.NewLine + "OK"
}; applicationTile.Update(newTile);
} private void btnCreateSecondaryTile_Click(object sender, RoutedEventArgs e)
{
// 创建次要 Tile 的 Demo
StandardTileData newTile = new StandardTileData
{
Title = "Secondary Fore Tile Title",
BackgroundImage = new Uri("TileBackgroundBlue.png", UriKind.Relative),
Count = ,
BackTitle = "Secondary Back Tile Title",
BackBackgroundImage = new Uri("TileBackgroundGreen.png", UriKind.Relative),
BackContent = "Secondary Back Tile Content" + Environment.NewLine + "OK"
}; // uri 是 key 不能重复
ShellTile.Create(new Uri("/Tile/ShellTileDemo.xaml?t=" + DateTime.Now.Ticks.ToString(), UriKind.Relative), newTile);
} private void btnDeleteSecondaryTile_Click(object sender, RoutedEventArgs e)
{
// 删除全部次要磁贴
if (ShellTile.ActiveTiles.Count() > )
{
ShellTile lastTile = ShellTile.ActiveTiles.Last();
lastTile.Delete();
} // xna 关闭应用程序:Microsoft.Xna.Framework.Game.Exit();
// sl 关闭应用程序
throw new Exception();
}
}
}

2、按计划更新磁贴的正面背景图的 Demo
ShellTileScheduleDemo.xaml

<phone:PhoneApplicationPage
x:Class="Demo.Tile.ShellTileScheduleDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <StackPanel Orientation="Vertical">
<Button Name="btnStart" Content="Start Schedule" Click="btnStart_Click" />
<Button Name="btnStop" Content="Stop Schedule" Click="btnStop_Click" />
</StackPanel> </phone:PhoneApplicationPage>

ShellTileScheduleDemo.xaml.cs

/*
* ShellTileSchedule - 用于按计划更新磁贴的正面背景图
* new ShellTileSchedule() - 更新 Application Tile
* new ShellTileSchedule(ShellTile tile) - 更新指定的 Secondary Tile
*
* Recurrence - 更新计划的模式
* UpdateRecurrence.Interval - 定时更新
* UpdateRecurrence.Onetime - 只更新一次
* Interval - 定时更新时的更新间隔(只能是 每小时/每天/每星期/每月)
* MaxUpdateCount - 最大的更新次数(默认值是 0,即无限次更新)
* StartTime - 开始更新的时间
* RemoteImageUri - 需要更新的图像的远程地址
*
* Start() - 启动计划
* Stop() - 停止计划
*
*
* 注意:
* 具体更新时间点是由系统统一调度的(系统每隔一段时间会批处理所有程序的更新计划,这么做是为了省电),也就是说即使你设置了 StartTime = DateTime.Now,也不会马上更新,但是一个小时内应该会更新
* 如果更新计划失败(比如找不到远程图像,远程图像大于80KB,超过30秒还没下载完等)次数太多,则该更新计划会被系统自动取消
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; namespace Demo.Tile
{
public partial class ShellTileScheduleDemo : PhoneApplicationPage
{
private ShellTileSchedule _schedule = new ShellTileSchedule(); public ShellTileScheduleDemo()
{
InitializeComponent();
} private void btnStart_Click(object sender, RoutedEventArgs e)
{
_schedule.Recurrence = UpdateRecurrence.Interval;
_schedule.Interval = UpdateInterval.EveryHour;
_schedule.StartTime = DateTime.Now;
//_schedule.MaxUpdateCount = 100;
_schedule.RemoteImageUri = new Uri(@"http://image.sinajs.cn/newchart/small/nsh000300.gif");
_schedule.Start();
} private void btnStop_Click(object sender, RoutedEventArgs e)
{
_schedule.Stop();
}
}
}

OK
[源码下载]

上一篇:struts2学习笔记(三)—— struts2的常见配置


下一篇:Struts2学习笔记(二):第一个Struts2应用