【WP7】WP7.8使用WP8的磁贴

  微软之前表示,对于所有WP7设备都可以升级到7.8,不过对于7.8,新加的东西实在太少了,只加入了类似WP8的界面,其他几乎没有变化,下面说说关于WP7.8中磁贴的使用

WP7的磁贴是属性实在太有限,只能设置Title,和Background和Count,而且大小不能改变

在WP8中有三种磁贴可以使用,循环图块,图标图块,翻转图块,关于这三种图块MSDN上说的很详细,这里不多介绍,地址如下

      http://msdn.microsoft.com/zh-CN/library/windowsphone/develop/hh202948(v=vs.105).aspx

  http://www.developer.nokia.com/Community/Wiki/Live_Tile_Templates_in_Windows_Phone_8

 使用WP8的磁贴,必须通过反射的方式进行调用,因为默认为WP7.5的库,不带这几个类,但是WP7.8模拟器和WP7.8手机上有这几个类

1、首先判断当前系统版本,WP7.5和WP7.5以下的系统不支持

            private static readonly Version _targetedVersion8 = new Version(8, 0);
            private static readonly Version _targetedVersion78 = new Version(7, 10, 8858);

            //判断系统版本是否高于7.8
            public static bool CanUseLiveTiles
            {
                get { return Environment.OSVersion.Version >= _targetedVersion78; }
            }

            public static bool IsWP8
            {
                get { return Environment.OSVersion.Version >= _targetedVersion8; }
            }

2、通过反射调用

  关于反射的使用,可以参考下面帖子,这里不详细说明

     http://www.cnblogs.com/bomo/archive/2013/03/16/2962430.html

  通过反射获得类,下面演示 CycleTileData 图块的使用,FlipTileData 和 IconicTileData 的使用差不多,这里就不演示了

        string title = "cycletiletitle";
        int count = 20;
        Uri navigateduri = new Uri("/MainPage.xaml", UriKind.Relative);
        //是否支持长图块
        bool usewide = true;

        //获取到CycleTileData类型
        Type tileDataType = Type.GetType("Microsoft.Phone.Shell.CycleTileData, Microsoft.Phone");
        //调用构造函数创建CycleTileData实例
        object cycleTileData = tileDataType.GetConstructor(new Type[] { }).Invoke(null);

        //获取属性设置函数设置属性
        MethodInfo setMethod = cycleTileData.GetType().GetProperty("Title").GetSetMethod();
        setMethod.Invoke(cycleTileData, new object[] { title });
        setMethod = cycleTileData.GetType().GetProperty("Count").GetSetMethod();
        setMethod.Invoke(cycleTileData, new object[] { count });
        setMethod = cycleTileData.GetType().GetProperty("SmallBackgroundImage").GetSetMethod();
        setMethod.Invoke(cycleTileData, new object[] { new Uri("/Assets/logo159x159.png", UriKind.Relative) }); 
        setMethod = cycleTileData.GetType().GetProperty("CycleImages").GetSetMethod();
        setMethod.Invoke(cycleTileData, new object[] { new List<Uri> { new Uri("/Assets/Image1.png", UriKind.Relative), new Uri("/Assets/Image2.png", UriKind.Relative), new Uri("/Assets/Image3.png", UriKind.Relative) } });

Type shellTileType
= Type.GetType("Microsoft.Phone.Shell.ShellTile, Microsoft.Phone"); MethodInfo createmethod = shellTileType.GetMethod("Create", new[] { typeof(Uri), typeof(ShellTileData), typeof(bool) }); createmethod.Invoke(null, new object[] { navigateduri, cycleTileData, usewide });

    

   关于WP7.8的Tile使用,在CodePlex上有个项目对Tile进行了封装,这里介绍下该库的用法用法

    http://mangopollo.codeplex.com/

  1、Utils类用于判断当前设备是否支持WP8磁贴

    两个属性:  CanUseLiveTiles(系统版本大于7.8) 和IsWP8(WP8设备)

  2、三种磁贴的类,CycleTileData ,FlipTileData,IconicTileData

    属性和用法都和WP8一

  3、管理磁贴的类:ShellTileExt

下面演示生成FlipTileData的用法

        //判断是否支持
        if (!Utils.CanUseLiveTiles)
        {
            MessageBox.Show("This feature needs Windows Phone 8");
            return;
        }

        try
        {
            var mytile = new FlipTileData
            {
                Title = "wide flip tile",
                BackTitle = "created by",
                BackContent = "Rudy Huyn",
                Count = 9,
                SmallBackgroundImage = new Uri("/Assets/logo159x159.png", UriKind.Relative),
                BackgroundImage = new Uri("/Assets/Background336x336_1.png", UriKind.Relative),
                BackBackgroundImage = new Uri("/Assets/Background336x336_2.png", UriKind.Relative)
            };
            //创建磁贴
            ShellTileExt.Create(new Uri("/MainPage.xaml?msg=from%20flip%20tile", UriKind.Relative), mytile, true);
        }
        catch
        {
            MessageBox.Show("remove tile before create it again");
        }

  通过该库,我们可以很方便的在WP7.8上创建WP8磁贴

 

  学习不是让我们再造一个轮子,而是教会我们如何造轮子,并且利用别人的轮子创造自己的东西

最后附上库文件

   http://files.cnblogs.com/bomo/Mangopollo.Light.zip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值