xaml - 每个平台在 XAML 中定义的 FontFamily

标签 xaml xamarin xamarin.forms

在我的 Xamarin Forms 项目中,我想定义每个平台的 Form family oneect 以及在应用程序中的使用。

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

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

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

最佳答案

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

关于xaml - 每个平台在 XAML 中定义的 FontFamily,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38322274/

相关文章:

c# - PCL 中的 xamarin securestring 等价物

c# - 在同一个 UIViewController 中的第一个 UITableView 的 RowClick 上绑定(bind)第二个 UITableView - ios Xamarin

http - 将 HttpResponseMessage 转换为对象

xaml - 如何在 Avalonia 中将焦点设置到 TextBox

c# - DateTime.Now 在 XAML 中没有绑定(bind)

wpf - 如何更改 AvalonEdit CodeCompletion 窗口上的样式?

c# - 无法从自定义渲染器 Xamarin Forms 加载 View

c# - C#Api Last.FM这是错误吗?

ios - Xamarin IOS CollectionView : cannot vertically align items

xaml - 忽略 XamlWriter.Save 中的不可序列化对象