每个平台在XAML中定义的FontFamily [英] FontFamily defined in XAML per platform

查看:242
本文介绍了每个平台在XAML中定义的FontFamily的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my Xamarin Forms project I would like to define the Form family onect per platform and the use that in the application.

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

So far I've hardcoded the FontFamily per controltype

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

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

Is it posible to set Fotnfamily globally in my XAML code preferable itn a OnPlatform tag?

推荐答案

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

Define a style in the App.xaml and then reference that style throughout the app. That way you can set the font once in the App.xaml using the OnPlatform tag and never have to worry about OnPlatform in all your other XAML files.

<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"/>

这篇关于每个平台在XAML中定义的FontFamily的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆