1

I'm creating a custom Toolkit with some custom controls and similar. But some controls use instead images icons from Fonts like "materialdesignicons-webfont.ttf". This fonts works if I add in ConfigureFont the alias of the font and the name as I stored like this:

.ConfigureFonts( fonts =>
          {
            fonts.AddFont( "OpenSans-Regular.ttf", "OpenSansRegular" );
            fonts.AddFont( "OpenSans-Semibold.ttf", "OpenSansSemibold" );
            fonts.AddFont( "materialdesignicons-webfont.ttf", "Material" );
          } );

But also I have a *MauiAppBuilder *named UseMyMauiToolkit.

So, in general I just need merge the font adding part with the MauiAppBuilder but i have not idea how do it. My MauiAppBuilder class:

public static class AppBuilderExtensions
  {
    #region Public Methods
    //# ref : https://learn.microsoft.com/en-us/dotnet/maui/xaml/namespaces/custom-namespace-schemas?view=net-maui-7.0
    public static MauiAppBuilder UseMyMauiToolkit( this MauiAppBuilder builder )
    {
      Task.Factory.StartNew( AppBuilderExtensions.SetDefaultStyles );

      return builder;
    }

    #endregion

    #region Private Methods

    private static async void SetDefaultStyles()
    {
      await AppBuilderExtensions.ApplicationExists();   

      Application.Current.PageAppearing += AppBuilderExtensions.ApplicationMainAppearing;
    }

    private static void SetDefaultStylesCore()
    {
      var appMergedDictionaries = Application.Current?.Resources?.MergedDictionaries;
      if( appMergedDictionaries != null )
      {
        //# Add the default styles only if not already present in Application.Current.Resources.
        if( appMergedDictionaries.Any( dict => dict.GetType() == typeof( Styles ) ) )
          return;

        appMergedDictionaries.Add( new Styles() );

      }
    }

    private static async Task<bool> ApplicationExists()
    {
      //# Wait until Application.Current is set.
      await Task.Factory.StartNew( () =>
      {
        while( Application.Current == null )
        {
        }
      } );

      return true;
    }

    #endregion

    #region Event Handlers

    private static void ApplicationMainAppearing( object sender, Page e )
    {
      Application.Current.PageAppearing -= AppBuilderExtensions.ApplicationMainAppearing;
      AppBuilderExtensions.SetDefaultStylesCore();
    }

    #endregion
  }
ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196

0 Answers0