【发布时间】:2018-09-04 11:23:19
【问题描述】:
我在 ResourceDictionary 中定义了一些自定义字体
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Fonlow.VA.App">
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:TypeArguments="x:String" x:Key="SuperFont">
<On Platform="Android" Value="Super.ttf#Super" />
<On Platform="UWP" Value="/Assets/Super.ttf#Super" />
<!--<On Platform="iOS" Value="OpenSans-Bold" />-->
</OnPlatform>
<OnPlatform x:TypeArguments="x:String" x:Key="NormalFont">
<On Platform="Android" Value="Normal.ttf#Normal" />
<On Platform="UWP" Value="/Assets/Normal.ttf#Normal" />
<!--<On Platform="iOS" Value="OpenSans-Bold" />-->
</OnPlatform>
</ResourceDictionary>
</Application.Resources>
和
<Label Text="{Binding CurrentOptotype.Text}" FontFamily="{StaticResource SuperFont}" FontSize="{Binding CurrentFontSize}" TextColor="Black" />
到目前为止一切顺利。但是,我会在运行时通过 ViewModel 绑定切换 FontFamily,就像 FontSize 的绑定一样,因为 CurrentFontSize 是 View Model 中的一个属性。 我试过了:
FontFamily="{Binding CurrentFontFamily}"
CurrentFontFamily 的值可以指向现有的系统字体,但我想指向一个自定义字体,指向 ResourceDictionary 中定义的字体。
然后我试过了:
FontFamily="{StaticResource {Binding CurrentFontFamily}}"
对于这种化妆语法显然存在运行时错误。我只是想知道 XAML 中是否有通过 MVVM 视图模型在运行时切换自定义字体?
【问题讨论】:
-
我没有尝试过,但
DynamicResource可能会在这种情况下工作,而不是StaticResource
标签: c# android xaml mvvm xamarin.forms