Skip to content

Commit

Permalink
fix: Cache IsApiContractAvailable properly
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Nov 24, 2021
1 parent a1a7a12 commit 6feacd3
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Uno.UI/Helpers/WinUI/SharedHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

using System;
using System.Collections.Generic;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.Graphics.Display;
Expand Down Expand Up @@ -368,23 +369,25 @@ static public bool IsIsLoadedAvailable()
return s_IsIsLoadedAvailable.Value;
}

static bool isAPIContractVxAvailableInitialized = false;
static bool isAPIContractVxAvailable = false;
private static bool s_dynamicScrollbarsDirty = true;
private static bool s_dynamicScrollbars;
private static readonly Dictionary<ushort, bool> isApiContractVxAvailable = new Dictionary<ushort, bool>();

public static bool IsAPIContractVxAvailable(ushort apiVersion)
{
if (!isAPIContractVxAvailableInitialized)
// Uno specific: WinUI caches using static variables inside of a template function,
// which creates a separate cache for eache apiVersion value. Instead, we use a dictionary
// for the same functionality.
if (!isApiContractVxAvailable.TryGetValue(apiVersion, out var available))
{
isAPIContractVxAvailableInitialized = true;
isAPIContractVxAvailable =
available =
IsSystemDll() ?
true :
ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", apiVersion);
isApiContractVxAvailable[apiVersion] = available;
}

return isAPIContractVxAvailable;
return available;
}

// base helpers
Expand Down

0 comments on commit 6feacd3

Please sign in to comment.