WindowsPhone Tile操作大全

自己封装了一些Tile的相关操作,有缺什么回复一下。会及时补上的。

 

/// <summary>
        /// 创建Tile
        /// </summary>
        /// <param name="uri">Tile唯一标识,同时担任导航</param>
        /// <param name="tileData">Tile数据</param>
        public void CreateTile(string uri, StandardTileData tileData)
        {
            ShellTile.Create(new Uri(uri, UriKind.Relative), tileData);
        }

        /// <summary>
        /// 将隔离存储图片,保存为Tile背景图片
        /// </summary>
        /// <param name="imageName">保存的名字</param>
        /// <returns>是否成功</returns>
        private bool SaveTileImage(string imageName)
        {
            using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (appStorage.FileExists(imageName))
                {
                    using (var file = appStorage.OpenFile(imageName, System.IO.FileMode.Open))
                    {
                        using (Stream imgStream = appStorage.OpenFile("/Shared/ShellContent/" + imageName, FileMode.Create))
                        {
                            byte[] bytes = new byte[file.Length];
                            file.Read(bytes, 0, bytes.Length);
                            imgStream.Write(bytes, 0, bytes.Length);
                            return true;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }

        }

        /// <summary>
        /// 将UI组合,保存Tile背景图片
        /// </summary>
        /// <param name="imageName">保存的名字</param>
        /// <param name="ui">要生成图片的UI组合</param>
        /// <returns>是否成功</returns>
        private bool SaveTileImage(string imageName, UIElement ui)
        {
            BitmapImage img = new BitmapImage();
            using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                   WriteableBitmap wb = new WriteableBitmap(173, 173);
                   wb.Render(ui, null);
                   wb.Invalidate();

                   using (var newfile = appStorage.CreateFile("/Shared/ShellContent/" + imageName))
                   {
                      wb.SaveJpeg(newfile, wb.PixelWidth, wb.PixelHeight, 0, 100);
                      return true;
                   }
            }
         return true; 
    }

        /// <summary>
        /// 删除固定到首页的Tile
        /// </summary>
        /// <param name="url"></param>
        /// <param name="imageName"></param>
        public void DeletePinToStart(string url, string imageName)
        {
            ShellTile oldTile = ShellTile.ActiveTiles.FirstOrDefault
               (e => e.NavigationUri.ToString().Contains(url));
            if (oldTile != null)
            {
                oldTile.Delete();
            }
            fh.DeleteFile("/Shared/ShellContent/" + imageName);
        }

        /// <summary>
        /// 获取固定到桌面的Tile
        /// </summary>
        /// <param name="uri">Tile唯一标致</param>
        /// <returns>此贴对象</returns>
        public ShellTile GetTile(string uri)
        {
            ShellTile tile = ShellTile.ActiveTiles.FirstOrDefault
               (e => e.NavigationUri.ToString().Contains(uri));
            return tile;
        }

        /// <summary>
        /// 更新Tile
        /// </summary>
        /// <param name="tile">需要更新的Tile对象</param>
        /// <param name="tileData">Tile数据</param>
        public void UpdateTile(ShellTile tile, StandardTileData tileData)
        {
            tile.Update(tileData);
        }
posted @ 2012-12-07 12:57  巫鸦  阅读(881)  评论(1编辑  收藏  举报