如何在Xamarin窗体(MAUI)中使用dll/nuget从核心项目访问MaterialIconsFont. ttf?

8ftvxx2r  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(244)

我已经创建了两个. Net MAUI项目。一个是核心项目,另一个是子项目。我想为所有的通用功能,如样式,字体和控件等创建nuget包。
为此,我在核心项目中添加了MaterialFontIcons.ttf文件,还创建了一个静态FontIconHelper类来访问ttf文件中所有可用的字体。
我想在我的子项目中使用这个相同的图标,所以我在子项目中使用了核心项目的dll文件。我能够访问FontIconHelper类,但我不能访问ttf文件本身。
我必须在我的智利项目中添加MaterialFontIcons. ttf文件,以及当我运行应用程序时只显示图标,否则它会显示"?"图标。
下面是我所做的:

核心项目步骤:

1从https://materialdesignicons.com/下载的材料设计字体文件
2在资源--〉字体文件夹中添加了MaterialFontIcons.ttf。
3将其构建操作设置为MauiFont。
4在依赖项注入中注册字体以在子项目中使用它。
5创建静态FontIconHelper.cs类以访问ttf图标/字体。
5我已经构建了解决方案,并从它的/bin文件夹中获取了. dll文件

字体IconHelper.cs类
public static class FontIconHelper
{
    public const string MaterialDesignIconsFont = "MaterialDesignIconsFont";

    public const string VectorSquare = "\U000f0001";
    public const string AccessPointNetwork = "\U000f0002";
    public const string AccessPoint = "\U000f0003";
    public const string Account = "\U000f0004";
}

子项目步骤:

1我在这里添加了从主项目生成的dll文件。2尝试访问Xaml中的FontIcon,如下所示:

Added name space: xmlns:helper="clr-namespace:Mobile.UI.Core.Helpers;assembly=Mobile.UI.Core"

<Image x:Name="image" HeightRequest="20" HorizontalOptions="EndAndExpand">
    <Image.Source>
        <FontImageSource
            Glyph="{x:Static helper:FontIconHelper.AccessPoint}"
            Size="20"
            Color="Black"
            FontFamily="{x:Static helper:FontIconHelper.MaterialDesignIconsFont}"/>
    </Image.Source>
</Image>

3当运行应用程序时,它显示带有"?"的方形,而不是实际的图标。
4然后我在Chile项目中添加了ttf文件,并将其构建操作设置为MauiFont,并在MauiProgram.cs中注册了该字体,如下所示:

var builder = MauiApp.CreateBuilder();
builder
    .UseMauiApp<App>()
    .ConfigureFonts(fonts =>
    {
        fonts.AddFont("MaterialdesignIcons-Font.ttf", "MaterialdesignIconsFont");
    })
    .ConfigureServices();

之后,在应用程序中显示图标属性。
有人知道如何直接从核心项目访问Fonts.ttf文件到子项目,而不将其添加到子项目吗?

7kqas0il

7kqas0il1#

尝试使用以下命令将新文件“AssemblyInfo.cs”添加到核心项目根文件夹:

[assembly: XamlCompilation(XamlCompilationOptions.Compile)] 
[assembly: ExportFont("MaterialdesignIcons-Font.ttf", Alias = "MaterialdesignIconsFont")]

相关问题