当前位置: 移动技术网 > IT编程>移动开发>WP > Windows Phone实用开发技巧(17):自定义应用程序的Tile

Windows Phone实用开发技巧(17):自定义应用程序的Tile

2018年10月15日  | 移动技术网IT编程  | 我要评论

在windows phone 7 (nodo)之前的版本中,我们在应用程序列表中长按某个应用程序的时候,会弹出“pin to start”的选择,选中后会将该应用程序的快捷方式pin到启动界面中,类似于windows 上的桌面快捷方式。那时候呈现在启动界面的图片是应用程序中的background.png,如果你没有改变该变片的话,pin到start中的图片大致如下:
 \

在mango中,我们可以做的更多(在nodo中我们仅能改变背景图片和应用程序的显示名字),在mango中,我们可以:
1. 动态更新pin 到桌面的背景图片
2. 创建一个secondary的tile,让我们的tile 变得更加live
3. 可以使用back agent动态更新count
马宁大大给出了一篇很好的入门文章《马宁的windows phone 7.1初体验(三)——tile》
下面讲讲稍微高级一点的知识

1. 在代码中合成图片 

我在项目中会用到如下三张图片:

\
 第一张backbg.png是用于secondary tile的背景图片,用于和第三张图片合成,生成一张新的secondary tile的背景图片,中间一张图片即tile的背景图片,最终的效果图如下图:

 \

合成图片的思想是利用writeablebitmap可以将uielement保存为图片,下面是详细代码:

public static string createbackground()
        {
            grid grid = new grid
            {
                background = new imagebrush
                {
                    imagesource = new bitmapimage
                    {
                        urisource = new uri("/mangtile;component/images/backbg.png", urikind.relative),
                        createoptions = bitmapcreateoptions.ignoreimagecache
                    }
                },
                width = 173,
                height = 173
            };
            image profileimg = new image
            {
                height=48,
                width=48,
                source = new bitmapimage
                {
                    urisource = new uri("/mangtile;component/images/u97911.jpg", urikind.relative),
                    createoptions = bitmapcreateoptions.ignoreimagecache
                },
            };
            grid.children.add(profileimg);
            grid.arrange(new rect(0d, 0d, 173, 173));
            writeablebitmap wbmp = new writeablebitmap(grid, null);
            string tiledirectory = "shared/shellcontent/tiles";//note :父目录必须是 shared/shellcontent
            string fullpath = tiledirectory + @"/" + "livetile.jpg";
            using (var store = isolatedstoragefile.getuserstoreforapplication())
            {
                if (!store.directoryexists(tiledirectory))
                {
                    store.createdirectory(tiledirectory);
                }
                using (var stream = store.openfile(fullpath, system.io.filemode.openorcreate))
                {
                    wbmp.savejpeg(stream, 173, 173, 0, 100);
                }
            }
            return "isostore:/" + fullpath;}
standardtiledata std = new standardtiledata
            {
                backgroundimage=new uri("/background.png"),
                title = "",
                backtitle = "secondary",
                backbackgroundimage = new uri(createbackground())
            };
shelltile.create(new uri("/mainpage.xaml", urikind.relative),std);
 

 2. shelltile 的activetiles属性

 msdn的解释为contains the collection of an applications tiles pinned to start. 就是包含应用程序已经pin to start的集合,要注意的是:

第一、指的是当前的应用程序,而不是所有的应用程序

第二、不管你的应用程序有没有pin to start,  activetiles中始终包含一个默认的tile(uri为”/“),而且始终为第一个

第三、如果你讲uri为“/mainpage.xaml” pin到桌面了,则activetiles包含两个tile(uri分别为“/”与/mainpage.xaml)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网