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
Runtime updating of AppTheme values #10442
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4c19060
Make things BindingBase
jfversluis 95acd2b
Update OnAppTheme.cs
jfversluis 69b0019
Runtime updating
jfversluis 42bf63a
Fixed
jfversluis cb446bb
Cleanup
jfversluis eaa2fae
Update VisualElement.cs
jfversluis ae29739
Update Application.cs
jfversluis e153bf1
Fix NRE for unit tests
jfversluis bad85ae
Review feedback
jfversluis 0ebc412
Update Application.cs
jfversluis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this have changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh. No. But it's added again on the next line but it just escapes the
>
to agt;
. I'm guessing that's an inconsistency between VS versions/VSWin and VSMac? I'll revert to be sure