Dino Windows 8 学习笔记(十二) - 动态瓷贴

Dino Windows 8 学习笔记(十二) - 动态瓷贴

31 Days of Windows 8 -- Live Tiles: http://www.jeffblankenburg.com/2012/11/09/31-days-of-windows-8-day-9-live-tiles/
MSDN--创建瓷贴和锁屏 : http://msdn.microsoft.com/library/windows/apps/Hh465377

创建瓷贴的步骤:

 1. 命名空间:
 
using   namespace  Windows::UI::Notifications;
using   namespace  Windows::Data::Xml::Dom;

2. 选取模板   http://msdn.microsoft.com/zh-CN/library/windows/apps/xaml/windows.ui.notifications.tiletemplatetype
3. 设置模板中的属性,最好将WideTile和SquareTile合并在一起,这样不论你的Tile是哪种形态都有动态效果。
4. 更新Tile

下面是一个完整步骤: 
 1 using   namespace  Windows::UI::Notifications; //  Notification命名空间
 2 using   namespace  Windows::Data::Xml::Dom; // DOM标准函数命名空间
 3 namespace  WFC  =  Windows::Foundation::Collections;
 4
 5 XmlDocument ^  tileXml  =  TileUpdateManager::GetTemplateContent(TileTemplateType::TileWideImageAndText01 );//获得模板
 6
 7 XmlNodeList ^  tileTextAttributes  =  tileXml -> GetElementsByTagName( " text " );
 8 tileTextAttributes -> Item( 0 ) -> InnerText  =   " Hello World! My very own tile notification " ;// 设置text属性
 9
10 XmlNodeList ^  tileImageAttributes  =  tileXml -> GetElementsByTagName( " image " );
11 static_cast < XmlElement ^> (tileImageAttributes -> Item( 0 )) -> SetAttribute( " src " " ms-appx:///images/redWide.png " );   //此处如果要使用Assets中的图片的话,直接用SetAttribute("src","Tile.png");                       
12 static_cast < XmlElement ^> (tileImageAttributes -> Item( 0 )) -> SetAttribute( " alt " " red graphic " );// 设置image属性
13
14 XmlDocument ^  squareTileXml  =  TileUpdateManager::GetTemplateContent(TileTemplateType::TileSquareText04); //获得方形模板
15 XmlNodeList ^  squareTileTextAttributes  =  squareTileXml -> GetElementsByTagName( " text " );
16 squareTileTextAttributes -> Item( 0 ) -> AppendChild(squareTileXml -> CreateTextNode( " Hello World! My very own tile notification " ));//设置text属性
17 IXmlNode ^  node  =  tileXml -> ImportNode(squareTileXml -> GetElementsByTagName( " binding " ) -> GetAt( 0 ),  true );
18 tileXml -> GetElementsByTagName( " visual " ) -> Item( 0 ) -> AppendChild(node);//将方形模板插入Wide模板
19
20 TileNotification ^  tileNotification  =   ref   new  TileNotification(tileXml);
21
22 int  seconds  =   10 ;
23 auto cal  =   ref   new  Windows::Globalization::Calendar();
24 cal -> AddSeconds(seconds);    
25 tileNotification -> ExpirationTime  =  cal -> GetDateTime();// 设置消失时间
26
27 TileUpdateManager::CreateTileUpdaterForApplication() -> Update(tileNotification); //显示Tile
28
29

也可以使用XML文件设置属性:
 1      //  create a string with the tile template xml
 2     auto tileXmlString  =   " <tile> "
 3          +   " <visual> "
 4          +   " <binding template='TileWideText03'> "
 5          +   " <text id='1'>Hello World! My very own tile notification</text> "
 6          +   " </binding> "
 7          +   " <binding template='TileSquareText04'> "
 8          +   " <text id='1'>Hello World! My very own tile notification</text> "
 9          +   " </binding> "
10          +   " </visual> "
11          +   " </tile> " ;
12
13      //  create a DOM
14     auto tileDOM  =   ref   new  Windows::Data::Xml::Dom::XmlDocument();
15
16      //  load the xml string into the DOM, catching any invalid xml characters 
17     tileDOM -> LoadXml(tileXmlString);
18
19      //  create a tile notification
20     auto tile  =   ref   new  TileNotification(tileDOM);
21
22      //  Send the notification to the app's application tile
23     TileUpdateManager::CreateTileUpdaterForApplication() -> Update(tile);
24
25     OutputTextBlock -> Text  =  tileDOM -> GetXml();

清理瓷贴
    TileUpdateManager::CreateTileUpdaterForApplication()->Clear();

使用瓷贴队列
    一个应用程序中最多能使用5个瓷贴,如果开启了瓷贴队列,会按照先后顺序放入队列中。之后TileNotification的显示时间和显示顺序将不受程序控制,这时的控制权是在系统手中的。
    为了便于控制瓷贴的显示,我们一般给瓷贴一个Tag用于辨识,当新的瓷贴的Tag与旧瓷贴的Tag相同时,旧瓷贴被新瓷贴代替。如果不同,队列头上的瓷贴被踢出队列。
    最近的瓷贴总是被立即显示。另外,当队列中已经有了5个瓷贴的时候,其中一个使用了Expirate,那么当这个瓷贴消失之后,将不再在队列中,也不会再显示它了。
    使用瓷贴队列的方法是:
     TileUpdateManager::CreateTileUpdaterForApplication()->EnableNotificationQueue(true);
    禁止瓷贴队列的方法:
     TileUpdateManager::CreateTileUpdaterForApplication()->EnableNotificationQueue(false);

瓷贴的图片问题
      用于Tile的图片不能大于200K,像素不能大于1024*1024,但是我们的Tile最大是310*150,所以我们在使用图片的时候要考虑到大小问题。

你可能感兴趣的:(Dino Windows 8 学习笔记(十二) - 动态瓷贴)