windows phone开发学习--磁贴【续】

来源:互联网 发布:网络喷子都是什么人 编辑:程序博客网 时间:2024/06/05 14:55

上次提到能够按照微软给出的磁贴的结构进行更新,但是显然那样做出来实现不了下面的效果(加红框的)


显然,微软没有开放类似于电话,短信数目提示的那种接口,那就只剩下一种办法--修改背景图。

在我的日历应用中,考虑到每一天都需要更新,tile背景图的修改有以下几种方式:

tile 更新目前有以下几种方案:
1、通过联网服务器传递图片更新日历(服务器可以任意修改图片,比较灵活,缺点是如果不联网将不会更新)
2、本地保存日历图片,软件将会变大(不建议这样做)
3、本地自动绘制日历图片(有待研究)

通过比较,建议采用的方法是联网下载图片,操作如下:
1、打开软件
2、启动后台任务联网
3、下载图片,更新tile
4、程序关闭后结束后台任务,由于tile具有记忆性故背景图片不会出现变化

优点:
1、图片全部存放在服务器端,客户端的应用程序较小
2、服务器端可以灵活产生或修改图片,只要保证图片大小均为173*173 px即可
3、图片生成只需要在服务器端执行,相对服务器存储容量而言图片所占空间较小

缺点:
1、不联网的情况下,图片无法正常显示,采取的补救措施是只显示应用图片,读取本地时间数据显示在tile上面
2、联网时间不能超过15秒,图片大小不能超过80K,用户联网的速度不可估,可能出现信号不好等情况,一旦失败,tile不能及时更新


这是昨天得出的结论,今天发现已经过时了,通过上网查找资料,发现一篇拼接图片的文章,点击这里:http://www.cnblogs.com/alexis/archive/2011/07/22/2113253.html

看了以上这篇文章,我想到自己完全可以在本地进行绘图,然后保存到windows phone的沙箱存储之中。

于是,我修改了ScheduledSyncTaskAgent中的 RefreshPlaster(); 方法

 public void RefreshTile()        {            Deployment.Current.Dispatcher.BeginInvoke(() =>            {                Tile t = new Tile();            });        }

我创建了一个工程,里面仅包含一个 windows phone user control页面,Tile就是该页面类,在Tile中我的布局如下:

<Canvas Name="CanvasForTile" Width="173" Height="173">                <Image Source="/365Plus;component/Images/Default.jpg" HorizontalAlignment="Left"                         Name="image1" Stretch="Fill"                        VerticalAlignment="Top" />                <TextBlock  HorizontalAlignment="Left" Margin="5,5,0,0"  Name="YearAndMonthTextBlock" Text="2012年12月" VerticalAlignment="Top"  FontFamily="Portable User Interface" FontSize="20" />                <TextBlock HorizontalAlignment="Left"  FontFamily="Portable User Interface"  Margin="5,35,0,0" Name="DayTextBlock" Text="20" VerticalAlignment="Top"  FontSize="35" />                <TextBlock  HorizontalAlignment="Left" FontFamily="Portable User Interface"   Margin="60,50,0,0" Name="WeekTextBlock" Text="周四" VerticalAlignment="Top"  FontSize="20" />                <TextBlock  HorizontalAlignment="Left" FontFamily="Portable User Interface"  Margin="5,129,0,0" Name="LunarTextBlock" Text="初八" VerticalAlignment="Top" FontSize="26" />        </Canvas>

后台的CS代码为:

  public Tile()        {                InitializeComponent();                            //判断是否是当前日期,如果不是就刷新页面                if (_365Settings.GetDateForTile().Date != DateTime.Today)                {                    CreateAndModify();                }                   }        public void CreateAndModify()        {            YearAndMonthTextBlock.Text = DateTime.Today.ToString("yyyy年MM月");            DayTextBlock.Text = DateTime.Today.Day.ToString();            LunarTextBlock.Text = GetLunarString(DateTime.Today);            WriteableBitmap bitmap = new WriteableBitmap(CanvasForTile, null);            string tiledirectory = "Shared/ShellContent/tiles";//note :父目录必须是 Shared/ShellContent            string tempJPEG = tiledirectory + @"/" + "LiveTile.jpg";            using (var store = IsolatedStorageFile.GetUserStoreForApplication())            {                if (!store.DirectoryExists(tiledirectory))                {                    store.CreateDirectory(tiledirectory);                }                using (var stream = store.OpenFile(tempJPEG, System.IO.FileMode.OpenOrCreate))                {                    bitmap.SaveJpeg(stream, 173, 173, 0, 100);                }            }            _365Settings.SetDateForTile(DateTime.Today);            ShellTile TileToFind = ShellTile.ActiveTiles.First();            StandardTileData data = new StandardTileData();            data.BackBackgroundImage = new Uri("isostore:/" + tempJPEG, UriKind.Absolute);            TileToFind.Update(data);        }        //转换为农历        private string GetLunarString(DateTime date)        {            string str;            if (Holidays.TryGetSolarHoliday(date, out str))            {                return str;            }            if (Jieqi.TryGetJieqi(date, out str))            {                return str;            }            Lunar lunar = new Lunar(date);            if (Holidays.TryGetLunarHoliday(lunar, out str))            {                return str;            }            return lunar.MonthOrDayString();        }

这样就实现了桌面磁贴的更新。



在这个过程中用到几个技术点:

1、图像绘制,在grid中添加控件,image或者textblock,这样以后只需要修改控件属性即可,然后把整个grid转换为一个位图

     WriteableBitmap bitmap = new WriteableBitmap(this.xamlGrid, null);


2、图片存储,这里用到沙箱技术,存放位置是系统为程序分配的区域,对用户来说是不可见的

 string tiledirectory = "Shared/ShellContent/tiles";//note :父目录必须是 Shared/ShellContent            string tempJPEG = tiledirectory + @"/" + "LiveTile.jpg";            using (var store = IsolatedStorageFile.GetUserStoreForApplication())            {                if (!store.DirectoryExists(tiledirectory))                {                    store.CreateDirectory(tiledirectory);                }                using (var stream = store.OpenFile(tempJPEG, System.IO.FileMode.OpenOrCreate))                {                    bitmap.SaveJpeg(stream, 173, 173, 0, 100);                }            }//.......... data.BackBackgroundImage = new Uri("isostore:/" + tempJPEG, UriKind.Absolute);


3、进程调用,后台进程一般情况下是无法调用前面UI操作,必须要通过创建异步进程,可以采用下面的方法进行操作,目前不知道安全性如何。

Deployment.Current.Dispatcher.BeginInvoke(() =>{在这里更新Ui });



以上就是磁贴Tile学习的一些心得和体会了,估计对于磁贴掌握和了解的也就这样了,应该没有什么新内容值得学习了。

原创粉丝点击