jeffwilcox / wp-thememanager

Windows Phone ThemeManager allows for easy overriding of the Dark or Light themes
89 stars 26 forks source link

Application.Current.Resources values do not refresh after applying Theme #9

Open ivanfong01 opened 11 years ago

ivanfong01 commented 11 years ago

When I try to get the current PhoneBackgroundColor/PhoneForegroundColor etc. from Application.Current.Resources, it always return the phone original theme background color but not the theme I forced using theme manager

(I am using WP8)

jeffwilcox commented 11 years ago

Can you provide more information? Since the phone only supports static resources, technically this script updates the Color values (replacements) within the Brush but the brushes will not be resolved again by UI elements already in the tree. This is a limitation of the phone XAML stack.

On Sun, Apr 28, 2013 at 10:30 AM, ivanfong01 notifications@github.comwrote:

When I try to get the current PhoneBackgroundColor/PhoneForegroundColor etc. from Application.Current.Resources, it always return the phone original theme background color but not the theme I forced using theme manager

— Reply to this email directly or view it on GitHubhttps://github.com/jeffwilcox/wp-thememanager/issues/9 .

Grunt101 commented 10 years ago

Hi Jeff,

I'm having the same issue as ivanfong01. Take the following scenario :+1:

  1. Set the emulator theme to White
  2. In the App.xaml.cs make a call to ThemeManager.ToDarkTheme();
  3. When you try reference a color in code (Eg. PhoneBackgroundColor) like the following : Color shouldBeBlackColor = (Color)Application.Current.Resources["PhoneBackgroundColor"]; it returns a white color instead of black, which is expected.

Do you know why this is happening?

jeffwilcox commented 10 years ago

Well everything will often be cached, so older pages will not get updated, etc. The best experience will be to set this at startup and then require a restart of the app if they want to change the theme.

yume-chan commented 10 years ago

At line 525 you wrote

currentColor = (Color)Application.Current.Resources[prefix + "Color"];

But Color is a struct(Color Structure (System.Windows.Media)) so trying to get currentColor will only get a copy and will not affect the original value.

You must remove the value first if it exists then add new value.

// Check if the Color is actually in the dictionary
if (Application.Current.Resources.Contains(prefix + "Color"))
    Application.Current.Resources.Remove(prefix + "Color");

 Application.Current.Resources.Add(prefix + "Color", color);
jeffwilcox commented 10 years ago

At least up through Windows Phone 7.5, the managed layer is only a representation, but the underlying native code calls actually hit the actual graphic cache. At least back when the code was first written, removing and adding would actually do nothing - but this might have changed in 8.0+.

Happy to accept a contribution if you have a legit fix since the NuGet package now only targets Windows Phone 8+.