Windows Phone 磁贴

因磁贴默认属性无法满足需求,所以就在背景图片上面下手。把所有要显示到磁贴上面的数据,布局等用后台代码写,布局成“XAML”,然后把生成的“XAML”数据存储为PNG图片或者JPG图片,再把生成的图片任务磁贴的背景图片,来满足需求。

1 var tileData = new StandardTileData()
2             {
3                 Title = string.Empty,
4                 BackgroundImage = new Uri(this.CreateBackground()),
5                 Count = 0,
6                 BackTitle = string.Empty,
7                 BackBackgroundImage = new Uri(this.CreateBackBackground()),
8                 BackContent = string.Empty
9             };
View Code
 1 private string CreateBackground_MainAppClassical()
 2         {
 3             Grid grid = new Grid
 4             {
 5                 Width = 210,
 6                 Height = 210
 7             };
 8 
 9             ........
10             
11             TextBlock txt_day = new TextBlock
12             {
13                 Text = DateTime.Now.Day.ToString(),
14                 FontSize = 90,
15                 Foreground = new SolidColorBrush(Colors.White),
16                 VerticalAlignment = VerticalAlignment.Bottom,
17                 Margin = new Thickness(0, 0, 0, -24)
18             };
19             sp.Children.Add(txt_day);
20             grid.Children.Add(sp);
21             grid.Arrange(new Rect(0d, 0d, 210, 210));
22             WriteableBitmap wbmp = new WriteableBitmap(grid, null);
23             string path = "Shared/ShellContent/tiles/tile_main.png";
24             StoragePNGToIS(path, wbmp);
25             return "isostore:/" + path;
26         }        
View Code
 1 /// <summary>
 2         /// 存储Tile图片到IS (JPG)
 3         /// </summary>
 4         public void StorageJPGToIS(string path, WriteableBitmap wbmp)
 5         {
 6             string directory = "Shared/ShellContent/tiles";
 7             using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
 8             {
 9                 if (!storageFile.DirectoryExists(directory))
10                 {
11                     storageFile.CreateDirectory(directory);
12                 }
13                 using (IsolatedStorageFileStream stream = storageFile.OpenFile(path, FileMode.Create))
14                 {
15                     wbmp.SaveJpeg(stream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 100);
16                 }
17             }
18         }
19 
20         /// <summary>
21         /// 存储Tile图片到IS (PNG)
22         /// </summary>
23         private void StoragePNGToIS(string path, WriteableBitmap wbmp)
24         {
25             string directory = "Shared/ShellContent/tiles";
26             using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
27             {
28                 if (!storageFile.DirectoryExists(directory))
29                 {
30                     storageFile.CreateDirectory(directory);
31                 }
32                 using (IsolatedStorageFileStream fileStream = storageFile.OpenFile(path, FileMode.Create, FileAccess.Write))
33                 {
34                     using (StreamWriter writer = new StreamWriter(fileStream))
35                     {
36                         PngEncoder encoder = new PngEncoder();
37                         encoder.Encode(wbmp.ToImage(), writer.BaseStream);
38                     }
39                 }
40             }
41         }
View Code

存储PNG图片的代码可参考:http://www.win7soft.com/wp/jc/jiaocheng_12498.html

注: 开发环境为VS2012+SDK8.0  开发WP7.X项目

1、更新、创建磁贴的时候,需要使用this.Dispatcher.BeginInvoke()处理线程问题。

2、如果使用的Toolkit里面的ContextMenu弹出菜单固定磁贴的话,ContextMenu的IsZoomEnabled="False"属性需要去掉。

以上两点可能会导致在存储为PNG图片的时候,布局混乱的情况,暂时这些发现,没有找到具体原因。

3、磁贴翻转:

同时设置了背景和背面背景时,磁贴就会有翻转的情况,即:BackgroundImage = new Uri(), BackBackgroundImage = new Uri()

创建的时候,将背面背景设置为Null时,则不会有翻转,即:BackgroundImage = new Uri(), BackBackgroundImage = null

设置了背面背景图后,把磁贴更新为不要背景,即不翻转:BackgroundImage = new Uri(), BackBackgroundImage = new Uri()    ==》 BackgroundImage = new Uri(), BackBackgroundImage = null,此时的要求是把翻转的磁贴更新为不翻转,但是这个时候磁贴同样会翻转。

这个时候需要将BackBackgroundImage = new Uri("", UriKind.Relative),设置为一个不存在的相对路径,才会有效果。

磁贴翻转可参考:http://www.haogongju.net/art/1676949

posted on 2013-05-15 10:10  ljpass6754  阅读(272)  评论(0编辑  收藏  举报

导航