Skip to content

Commit 32b8af8

Browse files
authored
AppThemeBinding supports DynamicResource values (#1783)
* AppThemeBinding supports DynamicResource values * Edit.
1 parent 73b5c76 commit 32b8af8

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

docs/user-interface/system-theme-changes.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ The following XAML example shows how to use the [`AppThemeBinding`](xref:Microso
5353

5454
In this example, the text color of the first <xref:Microsoft.Maui.Controls.Label> is set to green when the device is using its light theme, and is set to red when the device is using its dark theme. Similarly, the <xref:Microsoft.Maui.Controls.Image> displays a different image file based upon the current system theme.
5555

56-
In addition, resources defined in a <xref:Microsoft.Maui.Controls.ResourceDictionary> can be consumed with the [`StaticResource`](xref:Microsoft.Maui.Controls.Xaml.StaticResourceExtension) markup extension:
56+
Resources defined in a <xref:Microsoft.Maui.Controls.ResourceDictionary> can be consumed in an [`AppThemeBinding`](xref:Microsoft.Maui.Controls.Xaml.AppThemeBindingExtension) with the [`StaticResource`](xref:Microsoft.Maui.Controls.Xaml.StaticResourceExtension) markup extension:
5757

5858
```xaml
5959
<ContentPage ...>
@@ -86,6 +86,29 @@ In addition, resources defined in a <xref:Microsoft.Maui.Controls.ResourceDictio
8686

8787
In this example, the background color of the <xref:Microsoft.Maui.Controls.Grid> and the <xref:Microsoft.Maui.Controls.Button> style changes based on whether the device is using its light theme or dark theme.
8888

89+
::: moniker range=">=net-maui-8.0"
90+
91+
In addition, resources defined in a <xref:Microsoft.Maui.Controls.ResourceDictionary> can also be consumed in an [`AppThemeBinding`](xref:Microsoft.Maui.Controls.Xaml.AppThemeBindingExtension) with the [`DynamicResource`](xref:Microsoft.Maui.Controls.Xaml.DynamicResourceExtension) markup extension:
92+
93+
```xaml
94+
<ContentPage ...>
95+
<ContentPage.Resources>
96+
<Color x:Key="Primary">DarkGray</Color>
97+
<Color x:Key="Secondary">HotPink</Color>
98+
<Color x:Key="Tertiary">Yellow</Color>
99+
<Style x:Key="labelStyle" TargetType="Label">
100+
<Setter Property="Padding" Value="5"/>
101+
<Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Secondary}, Dark={StaticResource Primary}}" />
102+
<Setter Property="BackgroundColor" Value="{AppThemeBinding Light={DynamicResource Primary}, Dark={DynamicResource Secondary}}" />
103+
</Style>
104+
</ContentPage.Resources>
105+
<Label x:Name="myLabel"
106+
Style="{StaticResource labelStyle}"/>
107+
</ContentPage>
108+
```
109+
110+
::: moniker-end
111+
89112
### Extension methods
90113

91114
.NET MAUI includes `SetAppThemeColor` and `SetAppTheme<T>` extension methods that enable <xref:Microsoft.Maui.Controls.VisualElement> objects to respond to system theme changes.

docs/whats-new/dotnet-8.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ For information about what's new in .NET 8, see [What's new in .NET 8](/dotnet/c
2424
- The position at which a drag or drop gesture occurred can be obtained by calling the `GetPosition` method on a <xref:Microsoft.Maui.Controls.DragEventArgs>, <xref:Microsoft.Maui.Controls.DragStartingEventArgs>, or <xref:Microsoft.Maui.Controls.DropEventArgs> object. For more information, see [Recognize a drag and drop gesture](~/fundamentals/gestures/drag-and-drop.md).
2525
- <xref:Microsoft.Maui.Controls.WebView> gains a `UserAgent` property. For more information, see [WebView](~/user-interface/controls/webview.md).
2626
- Inline media playback of HTML5 video, including autoplay and picture in picture, has been enabled by default for the <xref:Microsoft.Maui.Controls.WebView> on iOS. For more information, see [Set media playback preferences on iOS and Mac Catalyst](~/user-interface/controls/webview.md#set-media-playback-preferences-on-ios-and-mac-catalyst).
27+
- Reources defined in a <xref:Microsoft.Maui.Controls.ResourceDictionary> can also be consumed in an [`AppThemeBinding`](xref:Microsoft.Maui.Controls.Xaml.AppThemeBindingExtension) with the [`DynamicResource`](xref:Microsoft.Maui.Controls.Xaml.DynamicResourceExtension) markup extension. For more information, see [Define and consume theme resources](~/user-interface/system-theme-changes.md#define-and-consume-theme-resources).
2728

2829
The following types or members have been deprecated:
2930

0 commit comments

Comments
 (0)