albi005 / MaterialColorUtilities

Material You color algorithms for .NET
https://albi005.github.io/MaterialColorUtilities/
Apache License 2.0
68 stars 8 forks source link

How can you use it with mdc-maui? #35

Closed Gami13 closed 1 day ago

Gami13 commented 1 day ago

The author described it in https://github.com/mdc-maui/mdc-maui/issues/36#issuecomment-1811473881 but never said what _appResources is, does't seem to work when initializing it to Application.Current.Resources

albi005 commented 1 day ago

The base MaterialColorService has an _appResources field. Override the Initialize method to get it (or Application.Current.Resources should also work):

public class CustomMaterialColorService : MaterialColorService<CorePalette, Scheme<uint>, Scheme<Color>, LightSchemeMapper, DarkSchemeMapper>
{
  private ResourceDictionary _appResources = null!;
  // ...
  public override void Initialize(ResourceDictionary resourceDictionary)
  {
      _appResources = resourceDictionary;
      base.Initialize(resourceDictionary);
  }
}

I also forgot to add that you have to call Initialize in App.xaml.cs before creating any pages:

+using MaterialColorUtilities.Maui;

public class App : Application
{
    public App(CustomMaterialColorService colorService)
    {
+       colorService.Initialize(this.Resources);

        MainPage = // ...
    }
}
Gami13 commented 1 day ago

This worked! Thank you so much for your help!

Gami13 commented 1 day ago

In fact it worked so well that it fixed mdc-maui's bug with cards always using light mode, amazing