【问题标题】:Single dll for Windows 8 and Windows 8.1适用于 Windows 8 和 Windows 8.1 的单个 dll
【发布时间】:2013-10-25 06:55:21
【问题描述】:

我在 VS 2012 中为 Windows 8 应用商店应用程序创建了一个类库项目 (dll),但现在引入了 Windows 8.1,有一些新的 API 可用于我想要的操作系统(例如广告 ID 以唯一标识用户)在我的 dll 中使用,但我不想发布针对 Windows 8.1 的单独 dll。我的目标是分发可以在 Windows 8 和 Windows 8.1 商店应用程序中引用的单个 dll。如果我将创建一个针对 8.1 的 dll,那么 8.0 应用程序将无法使用我的 dll。

有没有办法检查运行时可用的特定 Api 或 Windows 8.1 应用程序的任何预处理器,以便我的 dll 在运行时识别操作系统并执行代码,例如

string deviceId=string.Empty;

#if W8.1
deviceId=Windows.System.UserProfile.AdvertisingManager.AdvertisingId;
#endif

或者请建议任何其他方式,以便我只能将一个 dll 分发给我的用户?

【问题讨论】:

  • 你可以使用version helper APIs
  • 假设您可以在 Windows 应用程序中进行反射,您可以通过反射来实现吗? (但我不明白为什么不)按名称访问类和属性
  • 我正在尝试使用 Type 查找 API 可用性,但 Type tp = Type.GetType("Windows.System.UserProfile.AdvertisingManager"); 返回 null。但是当我使用 Type tp1 = typeof(Windows.System.UserProfile.AdvertisingManager); 它返回我的类但它会给我在 VS2012 中的编译错误,因为这个 api 不可用
  • 我犯了这个错误。 Type.GetType 需要完全限定名称。正确的代码是:Type tp = Type.GetType("Windows.System.UserProfile.AdvertisingManager, Windows.System, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime"); if (tp != null) { PropertyInfo properties = tp.GetRuntimeProperty("AdvertisingId"); if (properties != null) { string deviceId = (string)properties.GetValue(null); } }

标签: c#-4.0 visual-studio-2012 dll windows-store-apps


【解决方案1】:

最终通过反思来做到这一点。 AdvertisingManager API 在 Windows 8 中不可用,但如果应用程序在 Windows 8.1 上运行,相同的 dll(目标框架是 Windows 8)将通过反射访问 AdvertisingManager。因此,无需为不同版本分发两个 dll。

      Type tp = Type.GetType("Windows.System.UserProfile.AdvertisingManager, Windows.System, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime");
        if (tp != null)
        {
            PropertyInfo properties = tp.GetRuntimeProperty("AdvertisingId");
            if (properties != null)
            {
                string deviceId = (string)properties.GetValue(null);
            }
        }

输出

案例 1:在 Windows 8 上运行的 Windows 8 应用

在这种情况下,tp 将返回 null,因为 AdvertisingManager API 在 Windows 8 中不可用。

案例 2:在 Windows 8.1 上运行的 Windows 8 应用

由于 AdvertisingManager API 在 Windows 8.1 中可用,所有面向 Windows 8 的应用都可以访问此 API,并在这种情况下获取 AdvertisingId。

案例 3:在 Windows 8.1 上运行的 Windows 8.1 应用

该 API 可直接在 Windows 8.1 应用程序中使用。所以,不需要走反射路径。

【讨论】:

    【解决方案2】:

    您可以尝试根据 .NET 框架版本创建自己的条件指令。 Windows 8.1 使用 .NET 4.5.1

    这个 MSDN 示例可能会对您有所帮助 => Building Multiple TargetFramework Version Libraries

    【讨论】:

    • 我认为这会在编译时设置目标框架,但这不是他想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    相关资源
    最近更新 更多