Get window background color directly from ThemeDefinition

WPF Studio, Themes, and Shared Library for WPF Forum

Posted 7 months ago by Doug Gerard
Version: 24.1.2
Avatar

Howdy

I want to ask for the theme definition for the window background color it will use.  

I need this color to generate a list of contrasting color to use in a line plot/chart.

I've tried getting the resources after applying the CurtrentTheme name in the thememanger, but when I do a TryFindResource on Application.Current, I get a resource not found.  "var background = FindResourceColor(AssetResourceKeys.WindowBackgroundNormalBrushKey);" is what fails

What I want to do:

// now for each theme, so through and dig up the background color
// to use for the theme...
UpdateTheme(Theme.HighContrast);
var background = FindResourceColor(AssetResourceKeys.WindowBackgroundNormalBrushKey);
smThemeBackgroundColors[Theme.HighContrast] = background ?? throw new ResourceNotFoundException(nameof(AssetResourceKeys.WindowBackgroundNormalBrushKey));

UpdateTheme(Theme.Light);
background = FindResourceColor(AssetResourceKeys.WindowBackgroundNormalBrushKey);
smThemeBackgroundColors[Theme.Light] = background ??
throw new ResourceNotFoundException(nameof(AssetResourceKeys.WindowBackgroundNormalBrushKey));

UpdateTheme(Theme.Dark);
background = FindResourceColor(AssetResourceKeys.WindowBackgroundNormalBrushKey);
smThemeBackgroundColors[Theme.Dark] = background ??
throw new ResourceNotFoundException(nameof(AssetResourceKeys.WindowBackgroundNormalBrushKey)); 

where UpdateTheme is:

internal static void UpdateTheme(Theme theme)
{
      // decode the theme if needed
      theme = ResolveTheme(theme);

      // Set the current app theme via a registered theme definition name
      ActiproSoftware.Windows.Themes.ThemeManager.CurrentTheme = theme switch
      {
          Theme.Light => Resources.Properties.Resources.PenskeLight,
          Theme.Dark => Resources.Properties.Resources.PenskeDark,
          Theme.HighContrast => Resources.Properties.Resources.PenskeHighContrast,
          _ => Resources.Properties.Resources.PenskeDark
      };

      // track the latest theme...
      smCurrentTheme = theme;
}

Any thoughts on what to try?

Comments (1)

Answer - Posted 7 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Are you trying to lookup the resource while the theme is being initialized?  If so, the theme resources will not be loaded yet.  I was able to use TryFindResource successfully so long as it was called after ThemeManager.EndUpdate.

ThemeManager.BeginUpdate();
try {
  ThemeManager.AreNativeThemesEnabled = true;
  ThemeManager.CurrentTheme = ThemeNames.MetroLight;
}
finally {
  // Resources are not loaded until EndUpdate is called
  ThemeManager.EndUpdate();
}

var resource = Application.Current.TryFindResource(AssetResourceKeys.WindowBackgroundNormalBrushKey);


Actipro Software Support

The latest build of this product (v24.1.4) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.