Skip to content

Commit

Permalink
Refactor loading of AdditionalStyles.xaml file (#2136)
Browse files Browse the repository at this point in the history
* Refactore loading of AdditionalStyles.xaml file

* Update comment
  • Loading branch information
marcelwgn authored Mar 21, 2020
1 parent dd67c1d commit 290b432
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
1 change: 0 additions & 1 deletion FeatureAreas.props
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
<FeatureRadialGradientBrushEnabled>true</FeatureRadialGradientBrushEnabled>
</PropertyGroup>
<PropertyGroup>
<DefineConstants Condition="$(FeatureScrollerEnabled) =='true'">$(DefineConstants);FEATURE_SCROLLER_ENABLED</DefineConstants>
<DefineConstants Condition="$(FeatureNavigationViewEnabled) =='true'">$(DefineConstants);FEATURE_NAVIGATIONVIEW_ENABLED</DefineConstants>
<DefineConstants Condition="$(SolutionName) == 'MUXControlsInnerLoop'">$(DefineConstants);INNERLOOP_BUILD</DefineConstants>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
24 changes: 23 additions & 1 deletion dev/Scroller/APITests/ScrollControllerTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
{
Expand All @@ -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);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions dev/Scroller/TestUI/ScrollerPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
34 changes: 30 additions & 4 deletions test/MUXControlsTestApp/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -31,9 +31,15 @@ namespace MUXControlsTestApp
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// This is defined globally to be able to remove it later.
/// </summary>
sealed partial class App : Application
{
/// <summary>
/// AdditionalStyles.xaml file for ScrollViewer tests
/// </summary>
public static ResourceDictionary AdditionStylesXaml = new ResourceDictionary();

/// <summary>
/// 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().
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
}
4 changes: 4 additions & 0 deletions test/MUXControlsTestApp/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 290b432

Please sign in to comment.