This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Runtime updating of AppTheme values (#10442)
* Make things BindingBase * Update OnAppTheme.cs * Runtime updating * Fixed * Cleanup * Update VisualElement.cs * Update Application.cs * Fix NRE for unit tests * Review feedback * Update Application.cs fixes #10391
- Loading branch information
1 parent
b11153d
commit 6c40121
Showing
11 changed files
with
321 additions
and
131 deletions.
There are no files selected for viewing
42 changes: 27 additions & 15 deletions
42
Xamarin.Forms.Controls/GalleryPages/AppThemeGalleries/AppThemeCodeGallery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,54 @@ | ||
namespace Xamarin.Forms.Controls.GalleryPages.AppThemeGalleries | ||
using System; | ||
|
||
namespace Xamarin.Forms.Controls.GalleryPages.AppThemeGalleries | ||
{ | ||
public class AppThemeCodeGallery : ContentPage | ||
{ | ||
AppThemeColor color = new AppThemeColor { Light = Color.Green, Dark = Color.Red }; | ||
public Color TheColor | ||
{ | ||
get => color.Value; | ||
} | ||
Label _currentThemeLabel; | ||
|
||
public AppThemeCodeGallery() | ||
{ | ||
var currentThemeLabel = new Label | ||
_currentThemeLabel = new Label | ||
{ | ||
Text = Application.Current.RequestedTheme.ToString() | ||
}; | ||
|
||
Application.Current.RequestedThemeChanged += (s, a) => | ||
Application.Current.RequestedThemeChanged += Current_RequestedThemeChanged; | ||
|
||
var onThemeLabel = new Label | ||
{ | ||
currentThemeLabel.Text = Application.Current.RequestedTheme.ToString(); | ||
OnPropertyChanged(nameof(TheColor)); | ||
Text = "TextColor through SetBinding" | ||
}; | ||
|
||
var onThemeLabel = new Label | ||
var onThemeLabel1 = new Label | ||
{ | ||
Text = "TextColor through SetAppTheme" | ||
}; | ||
|
||
var onThemeLabel2 = new Label | ||
{ | ||
Text = "This text is green or red depending on Light (or default) or Dark" | ||
Text = "TextColor through SetAppThemeColor" | ||
}; | ||
|
||
onThemeLabel.SetBinding(Label.TextColorProperty, new Binding(nameof(TheColor))); | ||
BindingContext = this; | ||
onThemeLabel.SetBinding(Label.TextColorProperty, new AppThemeColor() { Light = Color.Green, Dark = Color.Red }); | ||
|
||
onThemeLabel1.SetOnAppTheme(Label.TextColorProperty, Color.Green, Color.Red); | ||
|
||
onThemeLabel2.SetAppThemeColor(Label.TextColorProperty, Color.Green, Color.Red); | ||
|
||
var stackLayout = new StackLayout | ||
{ | ||
HorizontalOptions = LayoutOptions.Center, | ||
VerticalOptions = LayoutOptions.Center, | ||
Children = { currentThemeLabel, onThemeLabel } | ||
Children = { _currentThemeLabel, onThemeLabel , onThemeLabel1,onThemeLabel2 } | ||
}; | ||
|
||
Content = stackLayout; | ||
} | ||
|
||
private void Current_RequestedThemeChanged(object sender, AppThemeChangedEventArgs e) | ||
{ | ||
_currentThemeLabel.Text = Application.Current.RequestedTheme.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace Xamarin.Forms.Core.UnitTests | ||
{ | ||
public class AppThemeTests : BaseTestFixture | ||
{ | ||
[SetUp] | ||
public override void Setup() | ||
{ | ||
base.Setup(); | ||
Application.Current = new MockApplication(); | ||
|
||
Device.SetFlags(new[] { ExperimentalFlags.AppThemeExperimental }); | ||
} | ||
|
||
[Test] | ||
public void ThemeChangeUsingSetAppThemeColor() | ||
{ | ||
var label = new Label | ||
{ | ||
Text = "Green on Light, Red on Dark" | ||
}; | ||
|
||
label.SetAppThemeColor(Label.TextColorProperty, Color.Green, Color.Red); | ||
Assert.AreEqual(Color.Green, label.TextColor); | ||
|
||
SetAppTheme(OSAppTheme.Dark); | ||
|
||
Assert.AreEqual(Color.Red, label.TextColor); | ||
} | ||
|
||
[Test] | ||
public void ThemeChangeUsingSetAppTheme() | ||
{ | ||
var label = new Label | ||
{ | ||
Text = "Green on Light, Red on Dark" | ||
}; | ||
|
||
label.SetOnAppTheme(Label.TextColorProperty, Color.Green, Color.Red); | ||
Assert.AreEqual(Color.Green, label.TextColor); | ||
|
||
SetAppTheme(OSAppTheme.Dark); | ||
|
||
Assert.AreEqual(Color.Red, label.TextColor); | ||
} | ||
|
||
[Test] | ||
public void ThemeChangeUsingSetBinding() | ||
{ | ||
var label = new Label | ||
{ | ||
Text = "Green on Light, Red on Dark" | ||
}; | ||
|
||
label.SetBinding(Label.TextColorProperty, new OnAppTheme<Color> { Light = Color.Green, Dark = Color.Red }); | ||
Assert.AreEqual(Color.Green, label.TextColor); | ||
|
||
SetAppTheme(OSAppTheme.Dark); | ||
|
||
Assert.AreEqual(Color.Red, label.TextColor); | ||
} | ||
|
||
void SetAppTheme(OSAppTheme theme) | ||
{ | ||
((MockPlatformServices)Device.PlatformServices).RequestedTheme = theme; | ||
Application.Current.OnRequestedThemeChanged(new AppThemeChangedEventArgs(theme)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.