Mahapps.metro: Error on ThemeManager.Current.DetectTheme

Created on 25 Apr 2020  路  7Comments  路  Source: MahApps/MahApps.Metro

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)

Bug

Most helpful comment

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

All 7 comments

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

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

MahApps 2.0.0-alpha0821 containes now latest ControlzEx 4.3.0-alpha0107

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

@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.

@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.

```javascript
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();
        }
    }
}

}
```

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...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bplatypus picture bplatypus  路  3Comments

Foxtrek64 picture Foxtrek64  路  4Comments

Zebody picture Zebody  路  4Comments

Hikerakashiya picture Hikerakashiya  路  5Comments

Coder-Bryan picture Coder-Bryan  路  3Comments