Target code for a specific version of Windows 10, like Build 10586

纵饮孤独 提交于 2019-12-12 12:27:51

问题


As you know ElementCompositionPreview.GetElementVisual is accessible on Windows 10 Build 10586, I would like to have one application that can target both Build 10586 and Build 10240.

Does anyone have an idea of how I could use Compositor and ElementCompositionPreview.GetElementVisual when the application is running on Build 10586 and something else when it is running the build 10240.

Something like this:

  #if WINDOWS_UWP Build 10586
       _compositor = new Compositor();
       _root = ElementCompositionPreview.GetElementVisual(myElement);
  #endif


 #if WINDOWS_UWP Build 10240
  //other code
 #endif

Any idea?


回答1:


As you know ElementCompositionPreview.GetElementVisual is accessible on Windows 10 Build 10586, I would like to have one application that can target both Build 10586 and Build 10240

For UWP app, there is only one Target version, for your requirement, we can set Target version to Build 10586 and the Min version: Build 10240

Does anyone have an idea of how I could use Compositor and ElementCompositionPreview.GetElementVisual when the application is running on Build 10586 and something else when it is running the build 10240.

Please use Windows.Foundation.Metadata.ApiInformation API to dynamically detect features:

if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.UI.Xaml.Hosting.ElementCompositionPreview"))
{
                if (Windows.Foundation.Metadata.ApiInformation.IsMethodPresent("Windows.UI.Xaml.Hosting.ElementCompositionPreview", "GetElementVisual"))
                {
                    _compositor = new Windows.UI.Composition.Compositor();
                    _root = Windows.UI.Xaml.Hosting.ElementCompositionPreview.GetElementVisual(btn1);
                }
                else
                {
                    //Do other things
                }
}

Here is a good article to introduce this API: Dynamically detecting features with API contracts (10 by 10)




回答2:


I think you can use System.Reflection to get the OS version from your app for example like this:

var analyticsInfoType = Type.GetType("Windows.System.Profile.AnalyticsInfo, Windows, ContentType=WindowsRuntime");
var versionInfoType = Type.GetType("Windows.System.Profile.AnalyticsVersionInfo, Windows, ContentType=WindowsRuntime");

if (analyticsInfoType == null || versionInfoType == null)
{
    //not on Windows 10
    return;
}

var versionInfoProperty = analyticsInfoType.GetRuntimeProperty("VersionInfo");

object versionInfo = versionInfoProperty.GetValue(null);
var versionProperty = versionInfoType.GetRuntimeProperty("DeviceFamilyVersion");
object familyVersion = versionProperty.GetValue(versionInfo);
long versionBytes;
if (!long.TryParse(familyVersion.ToString(), out versionBytes))
{
    //can't parse version number
    return;
}

Version DeviceVersion = new Version((ushort)(versionBytes >> 48),
  (ushort)(versionBytes >> 32),
  (ushort)(versionBytes >> 16),
  (ushort)(versionBytes));

if ((ushort)(versionBytes >> 16) == 10586)
{
    _compositor = new Compositor();
    _root = ElementCompositionPreview.GetElementVisual(myElement);
}
else
{
    //other code
}


来源:https://stackoverflow.com/questions/36462824/target-code-for-a-specific-version-of-windows-10-like-build-10586

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!