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

3
我正在尝试使用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 });

我有点困惑。你缺少了门控检查,以确保它不在WP7.1上运行。为什么会这样呢?你是想在WP7.x上创建WP8磁贴吗?显然这是行不通的,因为通过反射调用的API在WP7.x上不存在。 - JustinAngel
抱歉 Justin,我应该更清楚地表达。我正在进行这个检查。我已经更新了我的问题并做出了澄清。谢谢! - Matt McCormick
2个回答

3

我最终没有使用Mangopollo,但是他们在CodePlex上的源代码指引我找到了解决方案。感谢你的指引! - Matt McCormick

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;}}

现在看看布尔值IsTargetedVersion是否为真。基本上,

if(IsTargetedVersion){
      //iconic tile code
}

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


你还需要编辑清单文件,但我假设你已经完成了。如果没有,请再次阅读你发布的那篇MSDN文章。 - cjds

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接