Windows 8 开发之动态磁贴和辅助磁贴(二级磁贴)
磁贴在系统的开始菜单中代表着应用,动态磁贴可以向用户显示新的、重大的、定制的内容。 Tile 通知是一种固定格式的 XML,其中包含文字和图片内容。在应用程序清单中,必须包含一张正方形的的 logo,如果 应用程序也想使用宽模版 logo,也需要在清单中注明。如果你的应用中同样支持宽 tile,强烈建议你预加载 方形和宽形 在预加 载的 xml 中,从而无论开始菜单中的 tile 是方形或者 宽形的 都可以接收通知。以下是微软提供的磁贴模版:
磁贴和磁贴通知 : http://msdn.microsoft.com/zh-cn/library/windows/apps/hh779724.aspx
磁贴模板目录 : http://msdn.microsoft.com/zh-cn/library/windows/apps/hh761491.aspx
1.动态磁贴
使用 Windows.UI.Notifications 命名空间下的 TileUpdateManager 类, 创建用于更改和更新启动菜单图块的 TileUpdater对象。此类提供对系统提供的平铺模板 XML 内容的访问,以便您可以自定义用于更新您平铺的内容。 具体模版的XML内容,可以根据微软的模版,进行修改。
public static class TileUpdateManager
{
// 摘要:
// 创建并初始化 TileUpdater 的新实例,此操作可让您更改调用应用程序图块的外观。
//
// 返回结果:
// 用于将更改发送到应用程序的平铺的对象。
public static TileUpdater CreateTileUpdaterForApplication();
//
// 摘要:
// 创建并初始化图块 TileUpdater 的新实例,该图块属于和调用应用程序位于同一包中的另一应用程序。TileUpdater 允许开发人员更改该平铺外观。
//
// 参数:
// applicationId:
// 标题的唯一 ID。
//
// 返回结果:
// 用于通过 applicationId 将更改发送到平铺标识的对象。
public static TileUpdater CreateTileUpdaterForApplication(string applicationId);
//
// 摘要:
// 创建并初始化 TileUpdater 的新实例,此操作可让您更改 secondary tile 的外观。平铺可属于调用应用程序或相同包中的其他任何应用程序。
//
// 参数:
// tileId:
// 标题的唯一 ID。
//
// 返回结果:
// 用于通过 tileID 将更新发送到平铺标识的对象。
public static TileUpdater CreateTileUpdaterForSecondaryTile(string tileId);
//
// 摘要:
// 获取预定义的图块模板之一的 XML 内容,这样您可以自定义该内容以进行图块更新。
//
// 参数:
// type:
// 模板的名称。
//
// 返回结果:
// 包含 XML 的对象。
public static XmlDocument GetTemplateContent(TileTemplateType type);
}
//使用模版自定义字的符串
void UpdateTileButton_Click(object sender, RoutedEventArgs e)
{
string tileXmlString = ""
+ ""
+ ""
+ "This tile notification uses ms-appx images"
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "";
Windows.Data.Xml.Dom.XmlDocument tileDOM = new Windows.Data.Xml.Dom.XmlDocument();
tileDOM.LoadXml(tileXmlString);
TileNotification tile = new TileNotification(tileDOM);
TileUpdateManager.CreateTileUpdaterForApplication().Update(tile);
}
另外我们可以下载NotificationsExtensions文件,将该文件引用到我们的项目中,使用该文件中提供的类和方法来创建我们的动态磁贴,这个和前面不同的地方是,我们可以不使用XML模版进行设置磁贴的模版,而是通过具体的模版类,给模版类的属性赋值,实现动态磁贴。下面的代码实现了,最近五个动态磁贴之间的切换,该模版显示的样式为:
宽磁贴:左侧是一个较小的图像,右侧上面是第一行上较大文本的标题字符串,下面是四行四个常规文本的字符串。文本不自动换行。
窄磁贴:上面是一个较大文本的标题字符串,下面是一个最多可包含三行自动换行常规文本的字符串。
private void UpdateTileText(string key,string[] array)//array数组有4个元素,分别显示四行数据
{
try
{
if (array != null && array.Length > 1)
{
//创建方形磁帖模板,ITileSquareText02上面是一个较大文本的标题字符串,下面是一个最多可包含三行自动换行常规文本的字符串。
ITileSquareText02 squareContent = TileContentFactory.CreateTileSquareText02();
squareContent.TextHeading.Text = key;
squareContent.TextBodyWrap.Text = array;
//创建宽模板,ITileWideSmallImageAndText02左侧是一个较小的图像,右侧上面是第一行上较大文本的标题字符串,下面是四行四个常规文本的字符串。文本不自动换行。
ITileWideSmallImageAndText02 tileContent = TileContentFactory.CreateTileWideSmallImageAndText02();
tileContent.Image.Src = "ms-appx:///Assets/Logo.png";
tileContent.TextHeading.Text = word;
for (int i = 0; i < array.Length; i++)
{
switch (i)
{
case 0:
tileContent.TextBody1.Text = array;//第一行数据
break;
case 1:
tileContent.TextBody2.Text = array;//第二行数据
break;
case 2:
tileContent.TextBody3.Text = array;//第三行数据
break;
case 3:
tileContent.TextBody4.Text = array;//第四行数据
break;
}
}
tileContent.SquareContent = squareContent;
TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());
TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueue(true);
}
}
catch
{
}
}
注意:动态磁贴必须要在实体机器上,在模拟器中是无法显示动态磁贴的效果。
2.辅助磁贴(二级磁贴)
辅助磁贴使用户能够将 Windows 应用商店应用的特定内容和深层链接—对固定应用内部一个特定位置的引用—发送到“开始”屏幕上。辅助磁贴使用户能够使用好友、新闻源、股票报价和其他对其很重要的内容来个性化“开始”屏幕体验。创建辅助磁贴的选项最常在 UI 中显示为“附到开始菜单”选项。固定内容也就是为它创建辅助磁贴。此选项常常显示为应用栏上的一个标志符号。通过触摸或单击来选择辅助磁贴,会启动到父应用,以突出一种以固定内容或联系人为中心的体验。只有用户才可以固定辅助磁贴;应用不能在未获得用户许可的情况下以编程方式固定辅助磁贴。用户还可以通过“开始”屏幕或通过父应用,对辅助磁贴进行显式删除控制。
1).在固定辅助磁贴前,用户必须确认,要求对此进行确认的弹出窗口应当显示在调用固定请求的按钮旁边。
public Rect GetElementRect(FrameworkElement element)
{
GeneralTransform buttonTransform = element.TransformToVisual(null);
Point point = buttonTransform.TransformPoint(new Point());
return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
}
2).添加辅助磁贴。首先需要设置辅助磁贴的ID
public const string appbarTileId = "SecondaryTile.AppBar";
private async void AddButtonClicked(object sender, RoutedEventArgs e)
{
//当用户选择应用栏上的按钮时,会显示一个要求用户进行确认的弹出窗口。
//若要确保在显示弹出窗口时不取消应用栏,必须设置应用栏的 IsSticky 属性。
this.BottomAppBar.IsSticky = true;
if(SecondaryTile.Exists(appbarTileId))
{
//取消相应的辅助磁贴
SecondaryTile secondaryTile = new SecondaryTile(appbarTileId);
bool isUnpinned = await secondaryTile.RequestDeleteForSelectionAsync(GetElementRect((FrameworkElement)sender), Windows.UI.Popups.Placement.Above);
ToggleAppBarButton(isUnpinned);
}
else
{
//辅助磁贴的一些属性需要设置后才能固定辅助磁贴.
//·磁贴的唯一 ID
//·短名称
//·显示名称
//·磁贴选项
//·徽标图像
//·激活辅助磁贴时将传递到父应用程序的参数字符串
Uri logo = new Uri("ms-appx:///Assets/logo.jpg");
string tileActivationArguments = appbarTileId + " was pinned at " + DateTime.Now.ToLocalTime().ToString();
SecondaryTile secondaryTile = new SecondaryTile(appbarTileId,
"Secondary tile pinned via AppBar",
"SDK Sample Secondary Tile pinned from AppBar",
tileActivationArguments,
TileOptions.ShowNameOnLogo,
logo);
//指定前景文本颜色和小徽标。
secondaryTile.ForegroundText = ForegroundText.Dark;
secondaryTile.SmallLogo = new Uri("ms-appx:///Assets/small.jpg");
//调用异步方法将辅助磁贴固定。
//实际上这种方法不是将磁贴直接固定到“开始”屏幕,而是会显示一个要求用户允许这样做的确认提示框。
bool isPinned = await secondaryTile.RequestCreateForSelectionAsync(GetElementRect((FrameworkElement)sender), Windows.UI.Popups.Placement.Above);
ToggleAppBarButton(!isPinned);
}
this.BottomAppBar.IsSticky = false;
}
以上就完成了,辅助磁贴的添加和显示。
页:
[1]