From 290b432858a464965ee634c3314046e79d1311dc Mon Sep 17 00:00:00 2001 From: Marcel Wagner Date: Sat, 21 Mar 2020 16:44:23 +0100 Subject: [PATCH] Refactor loading of AdditionalStyles.xaml file (#2136) * Refactore loading of AdditionalStyles.xaml file * Update comment --- FeatureAreas.props | 1 - ...ollViewerWithScrollControllersPage.xaml.cs | 3 ++ .../APITests/ScrollControllerTests.cs | 24 ++++++++++++- dev/Scroller/TestUI/ScrollerPage.xaml.cs | 3 ++ test/MUXControlsTestApp/App.xaml.cs | 34 ++++++++++++++++--- test/MUXControlsTestApp/MainPage.xaml.cs | 4 +++ 6 files changed, 63 insertions(+), 6 deletions(-) diff --git a/FeatureAreas.props b/FeatureAreas.props index 088a537c47..0cc5eef75b 100644 --- a/FeatureAreas.props +++ b/FeatureAreas.props @@ -79,7 +79,6 @@ true - $(DefineConstants);FEATURE_SCROLLER_ENABLED $(DefineConstants);FEATURE_NAVIGATIONVIEW_ENABLED $(DefineConstants);INNERLOOP_BUILD diff --git a/dev/ScrollViewer/TestUI/ScrollViewerWithScrollControllersPage.xaml.cs b/dev/ScrollViewer/TestUI/ScrollViewerWithScrollControllersPage.xaml.cs index af8f5b1ffb..24fe8fe083 100644 --- a/dev/ScrollViewer/TestUI/ScrollViewerWithScrollControllersPage.xaml.cs +++ b/dev/ScrollViewer/TestUI/ScrollViewerWithScrollControllersPage.xaml.cs @@ -24,6 +24,9 @@ public sealed partial class ScrollViewerWithScrollControllersPage : TestPage public ScrollViewerWithScrollControllersPage() { + // We need the styles of the CompositionScrollController, so lets load it + App.AppendResourceDictionaryToMergedDictionaries(App.AdditionStylesXaml); + this.InitializeComponent(); UseScrollViewer(this.markupScrollViewer); } diff --git a/dev/Scroller/APITests/ScrollControllerTests.cs b/dev/Scroller/APITests/ScrollControllerTests.cs index 20a18131f4..41f69fcbff 100644 --- a/dev/Scroller/APITests/ScrollControllerTests.cs +++ b/dev/Scroller/APITests/ScrollControllerTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. using Common; @@ -25,6 +25,7 @@ using AnimationMode = Microsoft.UI.Xaml.Controls.AnimationMode; using SnapPointsMode = Microsoft.UI.Xaml.Controls.SnapPointsMode; using Scroller = Microsoft.UI.Xaml.Controls.Primitives.Scroller; +using MUXControlsTestApp; namespace Windows.UI.Xaml.Tests.MUXControls.ApiTests { @@ -40,6 +41,9 @@ public void SettingScrollControllerProperties() RunOnUIThread.Execute(() => { + // We need the styles of the CompositionScrollController, so lets load it + App.AppendResourceDictionaryToMergedDictionaries(App.AdditionStylesXaml); + scroller = new Scroller(); Verify.IsNotNull(scroller); @@ -91,6 +95,9 @@ public void ChangeOffsetsWhileScrollControllersAreAttached() RunOnUIThread.Execute(() => { + // We need the styles of the CompositionScrollController, so lets load it + App.AppendResourceDictionaryToMergedDictionaries(App.AdditionStylesXaml); + rectangleScrollerContent = new Rectangle(); scroller = new Scroller(); horizontalScrollController = new CompositionScrollController(); @@ -195,6 +202,9 @@ public void ChangeOffsetsWithAttachedScrollControllers() RunOnUIThread.Execute(() => { + // We need the styles of the CompositionScrollController, so lets load it + App.AppendResourceDictionaryToMergedDictionaries(App.AdditionStylesXaml); + rectangleScrollerContent = new Rectangle(); scroller = new Scroller(); horizontalScrollController = new CompositionScrollController(); @@ -320,6 +330,9 @@ public void ChangeOffsetsWithAdditionalVelocityAndAttachedScrollControllers() RunOnUIThread.Execute(() => { + // We need the styles of the CompositionScrollController, so lets load it + App.AppendResourceDictionaryToMergedDictionaries(App.AdditionStylesXaml); + rectangleScrollerContent = new Rectangle(); scroller = new Scroller(); horizontalScrollController = new CompositionScrollController(); @@ -461,6 +474,9 @@ public void ChangeOffsetsWhileBiDirectionalScrollControllerIsAttached() RunOnUIThread.Execute(() => { + // We need the styles of the CompositionScrollController, so lets load it + App.AppendResourceDictionaryToMergedDictionaries(App.AdditionStylesXaml); + rectangleScrollerContent = new Rectangle(); scroller = new Scroller(); biDirectionalScrollController = new BiDirectionalScrollController(); @@ -552,6 +568,9 @@ public void ChangeOffsetsWithAttachedBiDirectionalScrollController() RunOnUIThread.Execute(() => { + // We need the styles of the CompositionScrollController, so lets load it + App.AppendResourceDictionaryToMergedDictionaries(App.AdditionStylesXaml); + rectangleScrollerContent = new Rectangle(); scroller = new Scroller(); biDirectionalScrollController = new BiDirectionalScrollController(); @@ -647,6 +666,9 @@ public void ChangeOffsetsWithAdditionalVelocityAndAttachedBiDirectionalScrollCon RunOnUIThread.Execute(() => { + // We need the styles of the CompositionScrollController, so lets load it + App.AppendResourceDictionaryToMergedDictionaries(App.AdditionStylesXaml); + rectangleScrollerContent = new Rectangle(); scroller = new Scroller(); biDirectionalScrollController = new BiDirectionalScrollController(); diff --git a/dev/Scroller/TestUI/ScrollerPage.xaml.cs b/dev/Scroller/TestUI/ScrollerPage.xaml.cs index df28639c89..8771e4f23c 100644 --- a/dev/Scroller/TestUI/ScrollerPage.xaml.cs +++ b/dev/Scroller/TestUI/ScrollerPage.xaml.cs @@ -25,6 +25,9 @@ public sealed partial class ScrollerPage : TestPage { public ScrollerPage() { + // Some pages we will navigate to will need the resources, so lets load them now! + App.AppendResourceDictionaryToMergedDictionaries(App.AdditionStylesXaml); + LogController.InitializeLogging(); this.InitializeComponent(); diff --git a/test/MUXControlsTestApp/App.xaml.cs b/test/MUXControlsTestApp/App.xaml.cs index 676ed8de93..a2e87cb065 100644 --- a/test/MUXControlsTestApp/App.xaml.cs +++ b/test/MUXControlsTestApp/App.xaml.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. using System; @@ -31,9 +31,15 @@ namespace MUXControlsTestApp { /// /// Provides application-specific behavior to supplement the default Application class. + /// This is defined globally to be able to remove it later. /// sealed partial class App : Application { + /// + /// AdditionalStyles.xaml file for ScrollViewer tests + /// + public static ResourceDictionary AdditionStylesXaml = new ResourceDictionary(); + /// /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). @@ -194,9 +200,9 @@ private void SplashScreenDismissedAndRootCreated() protected override void OnLaunched(LaunchActivatedEventArgs e) { _isRootCreated = false; -#if FEATURE_SCROLLER_ENABLED // Tracked by Issue 1043 - AppendResourceToMergedDictionaries("AdditionalStyles.xaml"); -#endif + // Load the resource dictionary now + Application.LoadComponent(AdditionStylesXaml, new Uri( + "ms-appx:///Themes/AdditionalStyles.xaml"), ComponentResourceLocation.Nested); // For test purposes, add styles that disable long animations. DisableLongAnimations = true; @@ -323,5 +329,25 @@ private static void AppendResourceToMergedDictionaries(string resource, Resource + resource), ComponentResourceLocation.Nested); (targetDictionary ?? Application.Current.Resources).MergedDictionaries.Add(resourceDictionary); } + + public static void AppendResourceDictionaryToMergedDictionaries(ResourceDictionary dictionary) + { + // Check for null and dictionary not present + if (!(dictionary is null) && + !Application.Current.Resources.MergedDictionaries.Contains(dictionary)) + { + Application.Current.Resources.MergedDictionaries.Add(dictionary); + } + } + + public static void RemoveResourceDictionaryFromMergedDictionaries(ResourceDictionary dictionary) + { + // Check for null and dictionary is in list + if(!(dictionary is null) && + Application.Current.Resources.MergedDictionaries.Contains(dictionary)) + { + Application.Current.Resources.MergedDictionaries.Remove(dictionary); + } + } } } diff --git a/test/MUXControlsTestApp/MainPage.xaml.cs b/test/MUXControlsTestApp/MainPage.xaml.cs index 7a68fd857f..566371a014 100644 --- a/test/MUXControlsTestApp/MainPage.xaml.cs +++ b/test/MUXControlsTestApp/MainPage.xaml.cs @@ -197,6 +197,10 @@ DependencyObject SearchVisualTree(DependencyObject root, string name) protected override void OnNavigatedTo(NavigationEventArgs e) { + // This method gets called when we navigate to MainPage. + // Now we should unload the scroller dictionary! + App.RemoveResourceDictionaryFromMergedDictionaries(App.AdditionStylesXaml); + LanguageChooser.SelectedItem = MUXControlsTestApp.App.LanguageOverride; var testContentLoadedCheckBox = SearchVisualTree(this.Frame, "TestContentLoadedCheckBox") as CheckBox;