3

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

Cherry Tun
  • 31
  • 3
  • Have you set **Build Action** of Material Design Icon Fonts to `MauiFont`? – Jianwei Sun - MSFT Feb 02 '23 at 06:39
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 02 '23 at 08:05
  • Yes, I set Build Action of Material Design Icon Fonts to MauiFont. And I used it according the guideline of this ( https://cedricgabrang.medium.com/custom-fonts-material-design-icons-in-net-maui-acf59c9f98fe ) . – Cherry Tun Feb 02 '23 at 09:17
  • @CherryTun Is there any error message or log? – Jianwei Sun - MSFT Feb 03 '23 at 03:15
  • @JianweiSun-MSFT This answer is right when normal project running. But I just want to send an exe file to another users instead of an MSIX , so I changed commandName 'MSIX' to 'Project' in LunchSetting.Json and I run my project but icons are not appeared. I referenced this ( https://www.youtube.com/watch?v=bsCG8tskQ84 ). Thanks for user answer. – Cherry Tun Feb 04 '23 at 17:29
  • @CherryTun I updated the answer. You can check it. – Jianwei Sun - MSFT Feb 06 '23 at 02:56

1 Answers1

0

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

Jianwei Sun - MSFT
  • 2,289
  • 1
  • 3
  • 7