Material Design Icon Fonts are not working when publish exe file of MAUI project

854 Views Asked by At

I used Material Design Icon Fonts for icon image in my Maui project. When I publish my project as exe file and run, icon images are not appear.

Please share me answer.

enter image description here

enter image description here

1

There are 1 best solutions below

0
On

@CherryTun I tested the code in the link you provided, and it works well. Icon images are all able to display.

Here is my code implementation:

Icon Font to #Code:

namespace MauiApp1.Helpers 
{
      static class Icomoon
       {
            public const string Icon1 = "\ue900";
            public const string Icon2 = "\ue901";
            public const string Icon3 = "\ue916";
       }
}

Register the font in your MauiProgram.cs:

namespace MauiApp1
{
    public static class MauiProgram
   {
         public static MauiApp CreateMauiApp ()        
         {
              var builder = MauiApp.CreateBuilder ();
              builder.UseMauiApp<App> ()                
              .ConfigureFonts (fonts =>{
                           fonts.AddFont ("OpenSans-Regular.ttf","OpenSansRegular");
                           fonts.AddFont ("OpenSans-Semibold.ttf","OpenSansSemibold");
                           fonts.AddFont ("icomoon.ttf", "icomoon");
                        });
             ...
          }  
    }
}

Use it in MainPage.xaml:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:helpers="clr-namespace:MauiApp1.Helpers" 
       x:Class="MauiApp1.MainPage">
       <ScrollView>
              <VerticalStackLayout Spacing="25" 
                                   Padding="30,0" 
                                   VerticalOptions="Center">
                     <Label FontFamily="icomoon" 
                            Text="{x:Static helpers:Icomoon.Icon2}" 
                            FontSize="100"
                            HorizontalOptions="Center"/>
                     <Button BackgroundColor="Aquamarine" 
                             HorizontalOptions="Center" 
                             FontSize="50" 
                             Text="follow">
                            <Button.ImageSource>
                                   <FontImageSource FontFamily="icomoon" 
                                                    Glyph="{x:Static helpers:Icomoon.Icon3}" 
                                                    Size="50"/>
                            </Button.ImageSource>
                     </Button>
              </VerticalStackLayout>
       </ScrollView>
</ContentPage>

Update:

About it's not working when publish exe file of MAUI project, you can refer to this: Publish .NET MAUI Application as Windows Executable. There are other methods to publish exe file of MAUI project mentioned in the link. You can try it. Wish it can help you.