@CherryTun 我测试了您提供的链接中的代码,效果很好。图标图像都可以显示。
这是我的代码实现:
图标字体到#Code:
namespace MauiApp1.Helpers
{
static class Icomoon
{
public const string Icon1 = "\ue900";
public const string Icon2 = "\ue901";
public const string Icon3 = "\ue916";
}
}
在 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");
});
...
}
}
}
在 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>
更新:
关于发布MAUI项目的exe文件时不起作用,可以参考:将.NET MAUI应用程序发布为Windows可执行文件。还有其他方法可以发布MAUI项目的exe文件 链接中提到。你可以尝试一下。希望对你有帮助。
@jianweisun-msft 你完全没有抓住这个问题的重点。当您执行未打包的发布 .net maui 应用程序时,字体图标根本不会发布。您看到的结果是您在 Visual Studio 中运行此命令时的结果,这当然不是发布操作,因此您不会看到实际的问题。要复制此内容,您需要做的是按照海报所示进行未打包的发布,然后从发布的输出运行 exe。您会看到字体图标没有显示... PNG 文件确实显示...我正在使用 FontAwesome 字体,但我看到其他人在使用 Material 字体时遇到同样的问题。