Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on ThemeManager.Current.DetectTheme #3779

Closed
mauricio-bv opened this issue Apr 25, 2020 · 7 comments
Closed

Error on ThemeManager.Current.DetectTheme #3779

mauricio-bv opened this issue Apr 25, 2020 · 7 comments
Labels
Milestone

Comments

@mauricio-bv
Copy link

When running the following line:
ControlzEx.Theming.ThemeManager.Current.DetectTheme(Application.Current.MainWindow);

I get the Following error:

System.ArgumentException: 'Resource key "Theme.PrimaryAccentColor" is missing, is null or is not a color.'

See a simple example in my GitHub repo"
https://github.com/mauricio-bv/MahappsIssue.git

Screenshots
image

Environment:

  • MahApps.Metro version [2.0.0-Alpah0748] via Nugets
  • OS: [e.g. Win10 1909]
  • Visual Studio 2019 16.5.4
  • .NET Core 3.1

Additional context
This does not happen when running from the source code (latest commit in Git)

@mauricio-bv mauricio-bv changed the title Error getting Current Team. Error getting Current Theme Apr 25, 2020
@mauricio-bv mauricio-bv changed the title Error getting Current Theme Error on ThemeManager.Current.DetectTheme Apr 25, 2020
@batzen
Copy link
Collaborator

batzen commented Apr 25, 2020

The version of MahApps.Metro supporting the current ThemeManager implementation from ControlzEx is not released to nuget yet.
You can use the nuget feed from the appveyor CI system to get the latest preview versions.
The appveyor nuget feed is https://ci.appveyor.com/nuget/mahapps.metro

@batzen
Copy link
Collaborator

batzen commented Apr 25, 2020

I will add protective code to the theme detection in ControlzEx to not treat "old" dictionaries as current themes.

@punker76
Copy link
Member

MahApps 2.0.0-alpha0821 containes now latest ControlzEx 4.3.0-alpha0107

@punker76 punker76 added the Bug label May 12, 2020
@punker76 punker76 added this to the 2.0.0 milestone May 12, 2020
@Luk164
Copy link

Luk164 commented May 25, 2020

I just updated to 2.0.0 stable release but the issue seems to still be there, any advice?

@punker76
Copy link
Member

@Luk164 If I use the above sample https://github.com/mauricio-bv/MahappsIssue and update to the latest stable then I can't reproduce the error.

Can you look which ControlzEx version your are using? It should be the v4.3.0.

@Luk164
Copy link

Luk164 commented May 25, 2020

@punker76 It seems there were some more significant changes. I am using windows template studio and this class seems to be broken by the update.

Broken class

Filename is ThemeSelectorService.cs.

using System;
using System.Windows;

using Archivator_desktop_WPF_WTS.Contracts.Services;
using Archivator_desktop_WPF_WTS.Models;

using MahApps.Metro;

using Microsoft.Win32;

namespace Archivator_desktop_WPF_WTS.Services
{
 public class ThemeSelectorService : IThemeSelectorService
 {
     //private bool IsHighContrastActive => SystemParameters.HighContrast;

     public ThemeSelectorService()
     {
         SystemEvents.UserPreferenceChanging += OnUserPreferenceChanging;
     }

     public bool SetTheme(AppTheme? theme = null)
     {
         //if (IsHighContrastActive)
         //{
         //    // TO DO: Set high contrast theme name
         //}
         //else
         if (theme == null)
         {
             if (App.Current.Properties.Contains("Theme"))
             {
                 // Saved theme
                 var themeName = App.Current.Properties["Theme"].ToString();
                 theme = (AppTheme)Enum.Parse(typeof(AppTheme), themeName);
             }
             else
             {
                 // Default theme
                 theme = AppTheme.Light;
             }
         }

         var currentTheme = ThemeManager.DetectTheme(Application.Current);
         if (currentTheme == null || currentTheme.Name != theme.ToString())
         {
             ThemeManager.ChangeTheme(Application.Current, $"{theme}.Blue");
             App.Current.Properties["Theme"] = theme.ToString();
             return true;
         }

         return false;
     }

     public AppTheme GetCurrentTheme()
     {
         var themeName = App.Current.Properties["Theme"]?.ToString();
         Enum.TryParse(themeName, out AppTheme theme);
         return theme;
     }

     private void OnUserPreferenceChanging(object sender, UserPreferenceChangingEventArgs e)
     {
         if (e.Category == UserPreferenceCategory.Color ||
             e.Category == UserPreferenceCategory.VisualStyle)
         {
             SetTheme();
         }
     }
 }
}

@Luk164
Copy link

Luk164 commented May 25, 2020

Ok I managed to fix it by downloading the latest tamplate and comparing changes. Thats what I get for using a tool in beta I guess...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants