将Windows Phone 8 Tile功能添加到Windows Phone OS 7.1应用程序

时间:2012-11-29 22:38:48

标签: windows-phone-7 windows-phone-8

我尝试使用documentation from MSDN在我现有的Windows Phone OS 7.1应用程序中支持新的Windows Phone磁贴功能。但是,我似乎无法通过反射创建一个IconicTile,因为它不断给我NullReferenceExceptions和AmbiguousMatchExceptions。这是我正在使用的代码:

public static void CreateIconicTile(Uri tileId, string title, int count, string wideContent1, string wideContent2, string wideContent3, Uri smallIconImage, Uri iconImage, Color backgroundColor)
{
    // Get the new IconicTileData type.
    Type iconicTileDataType = Type.GetType("Microsoft.Phone.Shell.IconicTileData, Microsoft.Phone");

    // Get the ShellTile type so we can call the new version of "Update" that takes the new Tile templates.
    Type shellTileType = Type.GetType("Microsoft.Phone.Shell.ShellTile, Microsoft.Phone");

    // Get the constructor for the new IconicTileData class and assign it to our variable to hold the Tile properties.
    StandardTileData CreateTileData = new StandardTileData();

    // Set the properties.
    SetProperty(CreateTileData, "Count", count);
    SetProperty(CreateTileData, "WideContent1", wideContent1);
    SetProperty(CreateTileData, "WideContent2", wideContent2);
    SetProperty(CreateTileData, "WideContent3", wideContent3);
    SetProperty(CreateTileData, "SmallIconImage", smallIconImage);
    SetProperty(CreateTileData, "IconImage", iconImage);
    SetProperty(CreateTileData, "BackgroundColor", backgroundColor);

    // Invoke the new version of ShellTile.Create.
    shellTileType.GetMethod("Create").Invoke(null, new Object[] { tileId, CreateTileData });
}

我还尝试使用Windows Phone OS 7.1方式(ShellTile.Create(...))创建磁贴,然后通过MSDN文档中描述的反射调用UpdateIconicTile方法。但那也没有用。

非常感谢任何帮助。谢谢!

编辑:只是为了澄清,我正在检查平台版本以确保此代码仅在Windows Phone 8设备上运行,并且我已将必要的代码添加到我的清单中。

解决:感谢Martin Suchan在下面给出的答案,我能够解决这个问题。我的问题是Invoke(...)错过了一些属性。这是我用来实际创建磁贴的新行:

shellTileType.GetMethod("Create", new Type[] { typeof(Uri), typeof(ShellTileData), typeof(bool) }).Invoke(null, new Object[] { tileId, CreateTileData, true });

2 个答案:

答案 0 :(得分:3)

您是否尝试过Mangopollo库,其中包含用于在WP8上运行时在WP7.1应用程序中创建新切片的工作包装器?
http://mangopollo.codeplex.com/

答案 1 :(得分:0)

您必须确保在代码中启用了反射。

Iconic Tiles仅适用于Windows Phone 8,因此如果您检查版本,您只能将代码放入Windows Phone 7.1项目

private static Version TargetedVersion = new Version(8, 0);
    public static bool IsTargetedVersion {get{
               return Environment.OSVersion.Version >=    TargetedVersion;}}

现在看看bool IsTargetedVersion是否为真。基本上,

if(IsTargetedVersion){
      //iconic tile code
}

因此,只有当具有兼容功能的Windows手机(即wp8)运行您的应用程序时,它才会起作用。