3

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?

slugster
  • 49,403
  • 14
  • 95
  • 145
Damien
  • 2,911
  • 1
  • 25
  • 47

2 Answers2

3

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 enter image description here

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)

Franklin Chen - MSFT
  • 4,845
  • 2
  • 17
  • 28
0

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
}
Grace Feng
  • 16,564
  • 2
  • 22
  • 45