How to apply the ColorPalettesResources in runtime?
See original GitHub issueDescribe the bug
Hi, how can I use the ColorPaletteResources to change colors of a control in runtime? I tried to set a new palette in App.Current.Resources / App.Current.Resources.ThemeDictionaries
and update the RequestedTheme by a sample from here but it doesn’t work. What I do wrong?
Steps to reproduce the bug Click the button. Project: ColorPalettesTest.zip
MainPage.xaml
<Page
x:Class="ColorPalettesTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ColorPalettesTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Content="Change Palette" Click="myButton_Click"/>
<TextBox Text="TestText" Margin="5"/>
</StackPanel>
</Page>
MainPage.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace ColorPalettesTest {
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page {
public MainPage() {
this.InitializeComponent();
}
private void myButton_Click(object sender, RoutedEventArgs e) {
SetColorPalette();
}
void SetColorPalette() {
ColorPaletteResources palette = new ColorPaletteResources();
palette.Accent = Colors.Green;
palette.BaseLow = Colors.Black;
if (App.Current.Resources.ThemeDictionaries.ContainsKey("Default"))
App.Current.Resources.ThemeDictionaries["Default"] = palette;
else
App.Current.Resources.ThemeDictionaries.Add("Default", palette);
ReloadPageTheme(this.RequestedTheme);
}
void ReloadPageTheme(ElementTheme startTheme) {
if (this.RequestedTheme == ElementTheme.Dark)
this.RequestedTheme = ElementTheme.Light;
else if (this.RequestedTheme == ElementTheme.Light)
this.RequestedTheme = ElementTheme.Default;
else if (this.RequestedTheme == ElementTheme.Default)
this.RequestedTheme = ElementTheme.Dark;
if (this.RequestedTheme != startTheme)
ReloadPageTheme(startTheme);
}
}
}
Expected behavior The ColorPaletteResources should apply in runtime
Screenshots Actual Version Info
NuGet package version: [Microsoft.WinUI 3.0.0-preview3.201113.0]
Windows app type:
UWP | Win32 |
---|---|
Yes |
Windows 10 version | Saw the problem? |
---|---|
Insider Build (xxxxx) | |
May 2020 Update (19041) | Yes |
November 2019 Update (18363) | |
May 2019 Update (18362) | |
October 2018 Update (17763) | |
April 2018 Update (17134) | |
Fall Creators Update (16299) | |
Creators Update (15063) |
Device form factor | Saw the problem? |
---|---|
Desktop | Yes |
Xbox | |
Surface Hub | |
IoT |
Additional context
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (9 by maintainers)
Top Results From Across the Web
WPF: Changing Resources (colors) from the App.xaml ...
I'd recommend defining the resources in a Resource Dictionary contained in a separate file. Then in code (App.cs to load a default, then ......
Read more >Change the color palette at Runtime
WorkWithPlus generates a color palette when we run the DesignSystemWizard, then we can customize this palette of colors as we wish. If you...
Read more >ColorPaletteResources Class (Windows.UI.Xaml)
Initializes a new instance of the ColorPaletteResources class. Properties. Accent. Gets or sets the Accent color value. AltHigh.
Read more >Enable users to personalize their color experience in your ...
Apply Dynamic Color to your adaptive icons and widgets ... Android performs the following steps to generate color schemes from a user's wallpaper....
Read more >Adding dynamic color to your app
In this codelab, you'll migrate the theming in an app to Material 3 and later apply dynamic color.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was wondering if this may be a good idea: In VisualStudio you go to Add a new file, and one of the options is a CustomTheme - which creates a new ResourceDictionary - pre populated with all the light-weight styles used by WinUI - which can be edited and then referenced in App.xml
Basically what the editor would export, but with the default values already there.
I know there is a Figma plugin in the works, but I wonder if there is some value to including an AppTheme item template in the WinUI SDK/repo - so you can get a ResourceDictionary in your app, with all the light-weight styles there to be edited.
I know the Fluent Theme Editor exists, but its not currently synced with WinUI work, and the visual refresh is changing lots of the resource names and values used in the
Latest
visual design.