【问题标题】:How to change font family for Xamarin Shell FlyoutItem如何更改 Xamarin Shell FlyoutItem 的字体系列
【发布时间】:2021-06-07 07:24:56
【问题描述】:

有什么方法可以更改 Xamarin Forms Shell 中 FlyoutItem 内的字体系列内容吗?

我已经在共享项目中添加了自定义 .ttf 字体作为嵌入资源,并且还使用文件 AssemblyInfo.cs 向程序集注册了字体文件。

任何帮助将不胜感激。

【问题讨论】:

    标签: xaml xamarin.forms embedded-fonts xamarin.forms.shell


    【解决方案1】:

    对于Shell 弹出内容(弹出项+菜单项),您可以使用FlyoutItemLabelStyle 类的样式:

    <Style ApplyToDerivedTypes="True" Class="FlyoutItemLabelStyle" TargetType="Label">
         <Setter Property="FontFamily" Value="YourFontExportName"/>
    </Style>
    

    来自文档Style FlyoutItem and MenuItem objects。

    相关问题

    How to use Font Awesome icons in project as an icon of ImageButton

    Using Material Design Icons with Xamarin Forms - What Am I Missing?

    【讨论】:

      【解决方案2】:

      就像 Cfun 说的,您可以使用 FlyoutItemLabelStyle 设置 Shell Flyout 项目字体。

      1. 将字体作为嵌入式资源 (Build Action: EmbeddedResource) 添加到 Xamarin.Forms 共享项目。

      2. 使用ExportFont 属性在AssemblyInfo.cs 等文件中向程序集注册字体文件。也可以指定一个可选的别名。

        AssemblyInfo.cs:

        [assembly: ExportFont("Trashtalk.ttf", Alias = "Trashtalk")]
        
      3. 以您的风格添加此字体。

        <Style Class="FlyoutItemLabelStyle" TargetType="Label">
                 <Setter Property="FontFamily" Value="Trashtalk" />
             </Style>
        

      截图:

      【讨论】:

      • 您能解释一下这个答案与我的答案有何不同吗? OP提到他已经添加了字体文件,将其设置为嵌入式资源并使用程序集将其导出。所以 OP 已经完成了前面的步骤(由一些围绕 SO 的答案涵盖)并且只询问样式。
      猜你喜欢
      • 2019-11-25
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      相关资源
      最近更新 更多