-
Let suppose I defined my Themes as follows: internal static class MyThemes
{
public static readonly Resource<Brush> BrushA = ThemeResource.Create<Brush>(nameof(BrushA), Colors.Red, Colors.Orange);
public static readonly Resource<Brush> BrushB = ThemeResource.Create<Brush>(nameof(BrushB), Colors.Blue, Colors.LightBlue);
} I can easily bind themes to some controls: new TextBlock()
.Text("Using themed BrushA")
.Foreground(ThemeResource.Get<Brush>(nameof(MyThemes.BrushA))),
new TextBlock()
.Text("Using themed BrushB")
.Foreground(ThemeResource.Get<Brush>(nameof(MyThemes.BrushB))), So far so good. new TextBlock()
.Text("Toggle Themed BrushA or BrushB")
// code below does not compile :(
.Foreground(x => x.Binding(() => vm.BrushAorB).Convert(
BrushAorB => ThemeResource.Get<Brush>(BrushAorB ? nameof(MyThemes.BrushA) : nameof(MyThemes.BrushB))) I know that code above is wrong (well, it does not compile....) Here you have full snippet: public sealed partial class MainPage : Page
{
public MainPage() => this
.DataContext(new BindableMainPageModel(), (page, vm) => page
.Resources(r => r.Add(MyThemes.BrushA))
.Resources(r => r.Add(MyThemes.BrushB))
.Background(ThemeResource.Get<Brush>("ApplicationPageBackgroundThemeBrush"))
.Content(new StackPanel()
.VerticalAlignment(VerticalAlignment.Center)
.HorizontalAlignment(HorizontalAlignment.Center)
.Children(
new TextBlock()
.Text("Hello Uno Platform!"),
new ToggleSwitch()
.OnContent("Dark mode")
.OffContent("Light mode")
.Name(out ToggleSwitch c, c => c.Toggled += (s, e) =>
{
FrameworkElement? content = XamlRoot?.Content as FrameworkElement;
if (content is not null)
content.RequestedTheme = content.RequestedTheme == ElementTheme.Dark ? ElementTheme.Light : ElementTheme.Dark;
})
.IsOn(x => x.Binding(() => vm.IsAppInDarkMode).TwoWay()),
new TextBlock()
.Text("Using themed BrushA")
.Foreground(ThemeResource.Get<Brush>(nameof(MyThemes.BrushA))),
new TextBlock()
.Text("Using themed BrushB")
.Foreground(ThemeResource.Get<Brush>(nameof(MyThemes.BrushB))),
new ToggleSwitch()
.OnContent("BrushA")
.OffContent("BrushB")
.IsOn(x => x.Binding(() => vm.BrushAorB).TwoWay()),
new TextBlock()
.Text("Toggle Themed BrushA or BrushB")
// code below does not compile :(
.Foreground(x => x.Binding(() => vm.BrushAorB).Convert(
BrushAorB => ThemeResource.Get<Brush>(BrushAorB ? nameof(MyThemes.BrushA) : nameof(MyThemes.BrushB)))
)))
);
}
internal partial record MainPageModel
{
public IState<bool> IsAppInDarkMode => State<bool>.Value(this, () => false);
public IState<bool> BrushAorB => State<bool>.Value(this, () => true);
} Here is the full project: UnoResourceConditionalBindingApp.zip |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
cc @dansiegel, @kazo0, @eriklimakc |
Beta Was this translation helpful? Give feedback.
-
@kucint a couple of things on this...
Can you provide some more insight into what you're trying to do? Why the need to mix and match between 2 different Theme Resources? |
Beta Was this translation helpful? Give feedback.
@kucint thanks for the details, creating two styles is definitely an option. This could be considered the best way of doing it because we avoid using converters with ThemeResources directly.
First approach
I went ahead and modified the sample you sent, firstly by adding the two Styles to your MyThemes class: