【问题标题】:Adding Windows Phone 8 Tile functionality to Windows Phone OS 7.1 app将 Windows Phone 8 Tile 功能添加到 Windows Phone OS 7.1 应用程序
【发布时间】:2012-11-29 22:38:47
【问题描述】:

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

【问题讨论】:

  • 我很困惑。您错过了门控检查以确保它不在 WP7.1 上运行。这是为什么?您是否尝试在 WP7.x 上运行创建 WP8 磁贴?显然这是行不通的,因为通过反射调用的 API 在 WP7.x 上不存在。
  • 对不起贾斯汀,我应该更清楚。我正在做这个检查。我已经用这个澄清更新了我的问题。谢谢!

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


【解决方案1】:

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

【讨论】:

  • 我最终没有使用 Mangopollo,但他们在 CodePlex 上的源代码让我找到了解决方案。谢谢指点!
【解决方案2】:

您必须确保跨代码启用反射。

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)运行您的应用时,它才会运行。

【讨论】:

  • 您还必须编辑清单,但我假设您已经完成了。如果没有仔细阅读您再次发布的 MSDN 文章
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多