每个平台在XAML中定义的FontFamily

Jen*_*olt 3 xaml xamarin xamarin.forms

在我的Xamarin Forms项目中,我想定义每个平台的Form系列以及应用程序中的用法.

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

  <Style x:Key="TahomaBase_Label" TargetType="Label" BaseResourceKey="SubtitleStyle">
    <Setter Property="FontFamily" Value="Tahoma" />
  ...
 </Style>
Run Code Online (Sandbox Code Playgroud)

是否可以在我的XAML代码中全局设置Fotnfamily而不是OnPlatform标签?

Ste*_*sen 16

在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>
Run Code Online (Sandbox Code Playgroud)

然后:

<Label Text="{Binding Name}" Style="{DynamicResource FontLabel}" FontSize="Medium" FontAttributes="Bold" LineBreakMode="NoWrap"/>
Run Code Online (Sandbox Code Playgroud)