MaterialDesignInXAML / MaterialDesignInXamlToolkit

Google's Material Design in XAML & WPF, for C# & VB.Net.
http://materialdesigninxaml.net
MIT License
15.16k stars 3.43k forks source link

PaletteHelper doesn't work anymore #2432

Closed kacciocciacciara closed 3 years ago

kacciocciacciara commented 3 years ago

I have the following snippet of code that updates my base theme:

PaletteHelper paletteHelper = new PaletteHelper();
ITheme theme = paletteHelper.GetTheme();
theme.SetBaseTheme(Settings.Interface.UseDarkTheme ? Theme.Dark : Theme.Light);
paletteHelper.SetTheme(theme);

version 4.1.0 - working update to 4.2.1 - not working rollback to 4.1.0 - working

In the new version, my application starts with the light theme and never updates to dark.

Keboo commented 3 years ago

Hi @kacciocciacciara are you able to share more information about your code. I am not currently able to replicate the issue.

Can you confirm that you are seeing the Settings.Interface.UseDarkTheme change from true to false?

kacciocciacciara commented 3 years ago

Yes, I can confirm. It's the first thing I thought it could be the cause, but the boolean value updates correctly. There aren't any errors nor exceptions, it looked like the SetTheme method did nothing.

Keboo commented 3 years ago

@kacciocciacciara are you able to share the project? I am not able to replicate the issue.

kacciocciacciara commented 3 years ago

Hi @Keboo unfortunately, I can't. It's a project I'm working for my company and I can't share any source. I'm inclined to think that the issue is within my code and not in the toolkit itself. Next week I'll make a quick project with the toolkit's latest version and I'll give you a feedback ASAP. Thank you for your time.

kacciocciacciara commented 3 years ago

Hi @Keboo I made the test project with the same logic of my actual project. I confirm that this solution works with 4.1.0 but doesn't with 4.2.1. Please let me know if I did a mistake using the package. Thank you DemoMaterialDesignXAML.zip .

Keboo commented 3 years ago

Hi @kacciocciacciara thank you for the example. That is very helpful. There was some performance caching that was introduced in 4.2.1 that fixed a different bug which is the reason for the behavior you are now seeing. The issue you are running into is because you are setting the color theme resources twice in your App.xaml. Specifically this:

<materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="Blue" SecondaryColor="Green" ColorAdjustment="{materialDesign:ColorAdjustment}"/>

And this:

<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Green.xaml" />

Both set the color theme resources. I recommend that you only use one of them (my preference is the first one). Only having one of those in the App.xaml will resolve the issue you are seeing.

kacciocciacciara commented 3 years ago

Hi @Keboo I tried your solution and it works like intended. The only "problem" now is that accessing resources like PrimaryHueMidBrush is difficult because I need to remember the exact name without the help of XAML IntelliSense. If you have a suggestion about this, I'm all ears. Thank you very much for your assistance.

Keboo commented 3 years ago

@kacciocciacciara Unforuently right now you sort of have to decide between the XAML intellisense of the second option I posted, and the convenience of the BundledTheme. That is something that is working on being addressed in the 4.3.0 release as part of #2435

kacciocciacciara commented 3 years ago

@Keboo I read the issue about the color naming. Very interesting, and I feel like it's better than the actual system. I'll close this issue, you answered all I needed to know. Thank you again!