【问题标题】:FontFamily defined in XAML per platform每个平台在 XAML 中定义的 FontFamily
【发布时间】:2016-07-12 07:14:24
【问题描述】:

在我的 Xamarin Forms 项目中,我想为每个平台定义 Form 系列并在应用程序中使用它。

到目前为止,我已经对每个控件类型的 FontFamily 进行了硬编码

  <Style x:Key="TahomaBase_Label" TargetType="Label" BaseResourceKey="SubtitleStyle">
    <Setter Property="FontFamily" Value="Tahoma" />
  ...
 </Style>

是否可以在我的 XAML 代码中全局设置 Fotnfamily,最好使用 OnPlatform 标记?

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    在 App.xaml 中定义一个样式,然后在整个应用程序中引用该样式。这样,您可以使用 OnPlatform 标记在 App.xaml 中设置一次字体,而不必担心所有其他 XAML 文件中的 OnPlatform。

    <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="PlatformFontSample.App">
        <Application.Resources>
            <ResourceDictionary>
                <OnPlatform x:Key="FontFamilyName" x:TypeArguments="x:String" iOS="MarkerFelt-Thin" Android="OpenSans" WinPhone="Segoe UI" />
                <Style x:Key="FontLabel" TargetType="Label">
                    <Setter Property="FontFamily" Value="{DynamicResource FontFamilyName}" />
                </Style>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    然后:

    <Label Text="{Binding Name}" Style="{DynamicResource FontLabel}" FontSize="Medium" FontAttributes="Bold" LineBreakMode="NoWrap"/>
    

    【讨论】:

    • 谢谢,这是我的第一次尝试,但我收到一个错误:Xamarin.Forms.Xaml.dll 中出现“Xamarin.Forms.Xaml.XamlParseException”类型的异常,但未在用户代码中处理附加信息:位置 11:8。在 xmlns 中找不到类型 FontFamily
    • 我的错;我相应地对其进行了编辑。 x:TypeArguments 属性必须是 x:String 而不是 FontFamily
    • 好的。但后来我得到一个新错误:附加信息:位置 8:6。 Property Resources 为 null 或不是 IEnumerable 我创建了一个新项目并添加了您的代码。
    • 我猜你错过了ResourceDictionary 标签。使用完整的 App.xaml 再次更新示例。
    • 是的,我做到了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多