Skip to content

Commit 92f8328

Browse files
committed
Fixes/checks for XamlRoot downlevel.
1 parent 74b7c12 commit 92f8328

File tree

17 files changed

+97
-42
lines changed

17 files changed

+97
-42
lines changed

Microsoft.Toolkit.Uwp.SampleApp/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected override async void OnLaunched(LaunchActivatedEventArgs e)
7676
await RunAppInitialization(e?.Arguments);
7777
}
7878

79-
SystemInformation.Instance.TrackAppUse(e, Window.Current.Content.XamlRoot);
79+
SystemInformation.Instance.TrackAppUse(e);
8080
}
8181

8282
/// <summary>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Eyedropper/EyedropperPage.xaml.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ private void Load()
2929
SampleController.Current.RegisterNewCommand("Global Eyedropper", async (sender, args) =>
3030
{
3131
var eyedropper = new Eyedropper();
32-
eyedropper.XamlRoot = base.XamlRoot;
3332
var color = await eyedropper.Open();
3433
InAppNotification.Show($"You get {color}.", 3000);
3534
});

Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridColumn.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Toolkit.Uwp.UI.Controls.DataGridInternals;
99
using Microsoft.Toolkit.Uwp.UI.Controls.Primitives;
1010
using Microsoft.Toolkit.Uwp.UI.Data.Utilities;
11+
using Microsoft.Toolkit.Uwp.Utilities;
1112
using Windows.UI.Xaml;
1213
using Windows.UI.Xaml.Controls;
1314
using Windows.UI.Xaml.Data;
@@ -1060,7 +1061,7 @@ internal void ComputeLayoutRoundedWidth(double leftEdge)
10601061
if (this.OwningGrid != null && this.OwningGrid.UseLayoutRounding)
10611062
{
10621063
double scale;
1063-
if (OwningGrid.XamlRoot != null)
1064+
if (TypeHelper.IsXamlRootAvailable && OwningGrid.XamlRoot != null)
10641065
{
10651066
scale = OwningGrid.XamlRoot.RasterizationScale;
10661067
}

Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/Utilities/TypeHelper.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ internal static class TypeHelper
1717
internal const char PropertyNameSeparator = '.';
1818
internal const char RightIndexerToken = ']';
1919

20-
private static bool isAPIContractAvailableInitialized = false;
20+
private static bool isAPIsAvailableInitialized = false;
2121
private static bool isRS3OrHigher = false;
22+
private static bool isXamlRootAvailable = false;
2223

2324
// Methods
2425
private static Type FindGenericType(Type definition, Type type)
@@ -412,19 +413,33 @@ internal static bool IsRS3OrHigher
412413
{
413414
get
414415
{
415-
if (!isAPIContractAvailableInitialized)
416+
if (!isAPIsAvailableInitialized)
416417
{
417-
InitializeAPIContractAvailable();
418+
InitializeAPIsAvailable();
418419
}
419420

420421
return isRS3OrHigher;
421422
}
422423
}
423424

424-
internal static void InitializeAPIContractAvailable()
425+
internal static bool IsXamlRootAvailable
426+
{
427+
get
428+
{
429+
if (!isAPIsAvailableInitialized)
430+
{
431+
InitializeAPIsAvailable();
432+
}
433+
434+
return isXamlRootAvailable;
435+
}
436+
}
437+
438+
internal static void InitializeAPIsAvailable()
425439
{
426440
isRS3OrHigher = Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 5);
427-
isAPIContractAvailableInitialized = true;
441+
isXamlRootAvailable = Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent("Windows.UI.Xaml.UIElement", "XamlRoot");
442+
isAPIsAvailableInitialized = true;
428443
}
429444
}
430445
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace Microsoft.Toolkit.Uwp.UI.Controls
6+
{
7+
/// <summary>
8+
/// Internal class used to provide helpers for controls
9+
/// </summary>
10+
internal static partial class ControlHelpers
11+
{
12+
private static bool isXamlRootAvailable = Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent("Windows.UI.Xaml.UIElement", "XamlRoot");
13+
private static bool isXamlRootAvailableInitialized = false;
14+
15+
internal static bool IsXamlRootAvailable
16+
{
17+
get
18+
{
19+
if (!isXamlRootAvailableInitialized)
20+
{
21+
InitializeXamlRootAvailable();
22+
}
23+
24+
return isXamlRootAvailable;
25+
}
26+
}
27+
28+
internal static void InitializeXamlRootAvailable()
29+
{
30+
isXamlRootAvailable = Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent("Windows.UI.Xaml.UIElement", "XamlRoot");
31+
isXamlRootAvailableInitialized = true;
32+
}
33+
}
34+
}

Microsoft.Toolkit.Uwp.UI.Controls/Eyedropper/Eyedropper.Logic.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void UpdateWorkArea()
5353
var top = WorkArea.Top;
5454
double right;
5555
double bottom;
56-
if (XamlRoot != null)
56+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
5757
{
5858
right = XamlRoot.Size.Width - WorkArea.Right;
5959
bottom = XamlRoot.Size.Height - WorkArea.Bottom;
@@ -113,7 +113,7 @@ internal async Task UpdateAppScreenshotAsync()
113113
double width;
114114
double height;
115115
UIElement content;
116-
if (XamlRoot != null)
116+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
117117
{
118118
scale = XamlRoot.RasterizationScale;
119119
width = XamlRoot.Size.Width;

Microsoft.Toolkit.Uwp.UI.Controls/Eyedropper/Eyedropper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ public async Task<Color> Open(Point? startPoint = null)
109109
Child = _rootGrid
110110
};
111111

112-
if (XamlRoot != null)
112+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
113113
{
114114
_popup.XamlRoot = XamlRoot;
115115
}
116116

117-
if (_popup.XamlRoot != null)
117+
if (ControlHelpers.IsXamlRootAvailable && _popup.XamlRoot != null)
118118
{
119119
_rootGrid.Width = _popup.XamlRoot.Size.Width;
120120
_rootGrid.Height = _popup.XamlRoot.Size.Height;
@@ -151,7 +151,7 @@ private void HookUpEvents()
151151
Unloaded -= Eyedropper_Unloaded;
152152
Unloaded += Eyedropper_Unloaded;
153153

154-
if (XamlRoot != null)
154+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
155155
{
156156
XamlRoot.Changed -= XamlRoot_Changed;
157157
XamlRoot.Changed += XamlRoot_Changed;
@@ -197,7 +197,7 @@ private async void XamlRoot_Changed(XamlRoot sender, XamlRootChangedEventArgs ar
197197
private void UnhookEvents()
198198
{
199199
Unloaded -= Eyedropper_Unloaded;
200-
if (XamlRoot != null)
200+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
201201
{
202202
XamlRoot.Changed -= XamlRoot_Changed;
203203
}

Microsoft.Toolkit.Uwp.UI.Controls/Eyedropper/EyedropperToolButton.Properties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private static void OnEyedropperEnabledChanged(DependencyObject d, DependencyPro
8080
if (eyedropperToolButton.EyedropperEnabled)
8181
{
8282
VisualStateManager.GoToState(eyedropperToolButton, eyedropperToolButton.IsPointerOver ? EyedropperEnabledPointerOverState : EyedropperEnabledState, true);
83-
if (eyedropperToolButton.XamlRoot != null)
83+
if (ControlHelpers.IsXamlRootAvailable && eyedropperToolButton.XamlRoot != null)
8484
{
8585
eyedropperToolButton._eyedropper.XamlRoot = eyedropperToolButton.XamlRoot;
8686
}

Microsoft.Toolkit.Uwp.UI.Controls/Eyedropper/EyedropperToolButton.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private void HookUpEvents()
6262
Unloaded += EyedropperToolButton_Unloaded;
6363
ActualThemeChanged -= EyedropperToolButton_ActualThemeChanged;
6464
ActualThemeChanged += EyedropperToolButton_ActualThemeChanged;
65-
if (XamlRoot != null)
65+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
6666
{
6767
XamlRoot.Changed -= XamlRoot_Changed;
6868
XamlRoot.Changed += XamlRoot_Changed;
@@ -87,7 +87,7 @@ private void UnhookEvents()
8787
Click -= EyedropperToolButton_Click;
8888
Unloaded -= EyedropperToolButton_Unloaded;
8989
ActualThemeChanged -= EyedropperToolButton_ActualThemeChanged;
90-
if (XamlRoot != null)
90+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
9191
{
9292
XamlRoot.Changed -= XamlRoot_Changed;
9393
}
@@ -206,7 +206,7 @@ private async Task UpdateEyedropperWorkAreaAsync()
206206
if (TargetElement != null)
207207
{
208208
UIElement content;
209-
if (XamlRoot != null)
209+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
210210
{
211211
content = XamlRoot.Content;
212212
}
@@ -218,7 +218,7 @@ private async Task UpdateEyedropperWorkAreaAsync()
218218
var transform = TargetElement.TransformToVisual(content);
219219
var position = transform.TransformPoint(default(Point));
220220
_eyedropper.WorkArea = new Rect(position, new Size(TargetElement.ActualWidth, TargetElement.ActualHeight));
221-
if (XamlRoot != null)
221+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
222222
{
223223
_eyedropper.XamlRoot = XamlRoot;
224224
}

Microsoft.Toolkit.Uwp.UI.Controls/InfiniteCanvas/Controls/InfiniteCanvasVirtualDrawingSurface.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ internal void ConfigureSpriteVisual(double width, double height, float zoomFacto
7878

7979
internal void SetScale(float zoomFactor)
8080
{
81-
if (XamlRoot != null)
81+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
8282
{
8383
_screenScale = XamlRoot.RasterizationScale;
8484
}

0 commit comments

Comments
 (0)