【问题标题】:Xamarin: Is it possible to include my font recources in the App.cs file?Xamarin:是否可以在 App.cs 文件中包含我的字体资源?
【发布时间】:2021-04-09 12:06:04
【问题描述】:

您好,我当前的 xamarin 应用程序没有全局 App.xaml 文件,只有一个 App.cs,这是我配置它的方式,并希望保持这种方式。

现在的问题是,我想使用一些来自 fontawsome 的字体图标,但为此我需要定义一个可以在 xaml 文件中使用的资源字典。现在我正在将资源复制粘贴到我需要图标的每个 xaml 页面。

我希望资源能够在全球范围内工作,所以有没有办法可以将我的代码转换为 C# 或在我的全局 App.xaml 文件中实现资源,这样我就不必一直复制/粘贴它。

这是我用于资源的代码:

        <ResourceDictionary>
            <OnPlatform x:TypeArguments="x:String" 
                x:Key="FontAwesomeBrands">
                <On Platform="Android" 
          Value="FontAwesome5Brands.otf#Regular" />
                <On Platform="iOS" 
          Value="FontAwesome5Brands-Regular" />
                <On Platform="UWP" 
          Value="/Assets/FontAwesome5Brands.otf#Font Awesome 5 Brands" />
            </OnPlatform>

            <OnPlatform x:TypeArguments="x:String" 
                x:Key="FontAwesomeSolid">
                <On Platform="Android" 
          Value="FontAwesome5Solid.otf#Regular" />
                <On Platform="iOS" 
          Value="FontAwesome5Free-Solid" />
                <On Platform="UWP" 
          Value="/Assets/FontAwesome5Solid.otf#Font Awesome 5 Free" />
            </OnPlatform>

            <OnPlatform x:TypeArguments="x:String" 
                x:Key="FontAwesomeRegular">
                <On Platform="Android" 
          Value="FontAwesome5Regular.otf#Regular" />
                <On Platform="iOS" 
          Value="FontAwesome5Free-Regular" />
                <On Platform="UWP" 
          Value="/Assets/FontAwesome5Regular.otf#Font Awesome 5 Free" />
            </OnPlatform>
        </ResourceDictionary>

【问题讨论】:

  • 到目前为止你有没有尝试过?
  • 是的谷歌搜索没有结果我试图将代码转换为 c# 但它根本不起作用

标签: c# wpf xamarin xamarin.forms resourcedictionary


【解决方案1】:

Xamarin 更新了引用字体的方式,比以前更容易了。

现在,你要做的就是:

  • 将您的字体添加到您的 iOS 和 Android 项目作为参考添加的程序集中
  • 将构建操作(文件属性)设置为嵌入式资源
  • 更新已添加字体的程序集的 AssemblyInfo
  • 在 FontFamily 中设置字体的别名
  • 你很适合使用这些字体

编辑: 如果您无法升级到最新的 XF,请执行以下步骤(坚持下去,它更复杂):

  1. iOS:
  • 在您的 iOS 项目中,在 Resources 文件夹下,将字体添加为 BundleResource

  • 使用此代码编辑您的 Info.plist

     <!-- Custom fonts -->
     <key>UIAppFonts</key>
     <array>
         <string>Fonts/FontAwesome-Regular.otf</string>
         <string>Fonts/FontAwesome-Solid.otf</string>
         <string>Fonts/FontAwesome-Light.otf</string>
     </array>
    
  1. 安卓:
  • 将字体添加为 AndroidAsset(文件属性)
  1. Xamarin 表单:
  • 在您的 App.xaml 资源中创建以这些字体为目标的资源

    <!--#region FONTS-->
    <OnPlatform x:Key="FAR" x:TypeArguments="x:String">
        <On Platform="Android" Value="Fonts/FontAwesome-Regular.otf#FontAwesome5ProRegular" />
        <On Platform="iOS" Value="FontAwesome5Pro-Regular" />
        <On Platform="UWP" Value="Fonts/FontAwesome-Regular.otf#Font Awesome 5 Pro" />
        <On Platform="Default" Value="Fonts/FontAwesome-Regular.otf" />
    </OnPlatform>
    <OnPlatform x:Key="FAS" x:TypeArguments="x:String">
        <On Platform="Android" Value="Fonts/FontAwesome-Solid.otf#FontAwesome5ProSolid" />
        <On Platform="iOS" Value="FontAwesome5Pro-Solid" />
        <On Platform="UWP" Value="Fonts/FontAwesome-Solid.otf#Font Awesome 5 Pro" />
        <On Platform="Default" Value="Fonts/FontAwesome-Solid.otf" />
    </OnPlatform>
    <OnPlatform x:Key="FAL" x:TypeArguments="x:String">
        <On Platform="Android" Value="Fonts/FontAwesome-Light.otf#FontAwesome5ProLight" />
        <On Platform="iOS" Value="FontAwesome5Pro-Light" />
        <On Platform="UWP" Value="Fonts/FontAwesome-Light.otf#Font Awesome 5 Pro" />
        <On Platform="Default" Value="Fonts/FontAwesome-Light.otf" />
    </OnPlatform>
    <!--#endregion FONTS-->
    
  1. 使用带有 StaticResource 的字体(但不使用别名)(删除代码中的空格和给定代码中 Label 之前的空格:编辑器正在吃掉它们,所以我必须添加一个空格以使其可见)

小心:很难找到要使用的字体路径名称:在我给出的第一个示例中,Xamarin 正在为您完成这项工作。使用旧方法,您必须自己查找名称(我第一次为每个平台查找名称时遇到了一些麻烦)。您可以通过在计算机上安装字体名称或在文件属性的详细信息中找到字体名称。

/!\ 也许它适用于字体名称中的空格。我不知道为什么它不适用于我的空间。因此,可能不需要删除字体名称中的空格。

您可以在 Xamarin Forms App.xaml 部分的代码中执行相同的操作。

希望对你有帮助

【讨论】:

  • 我的 xamarin 表单使用的是旧版本,我认为这适用于 4.6?我无法更新(尝试过),然后整个应用程序不再工作。我正在寻找一种在 4.4 上执行此操作的方法,您知道吗?否则你的答案会很完美
  • @ernie 你的意思是 Nk54 的回复不能在 Xamarin.forms 4.4 上运行,我在 Xamarin.forms 4.1 测试,它也可以正常工作。
  • @Nk54 抱歉,我有点病了,现在去看看
  • 我没有 app.xaml 文件
  • @ernie 也许你可以尝试通过this thread添加App.xaml
猜你喜欢
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-15
  • 2010-12-06
  • 1970-01-01
  • 2014-03-20
相关资源
最近更新 更多