![](/img/trans.png)
[英]Custom View with Bindable Property not binding properly on Xamarin.Forms SAP
[英]Binding to StaticResource with a property of View Model in Xamarin.Forms?
我在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模型中的一個屬性。 我試過了:
FontFamily="{Binding CurrentFontFamily}"
而且CurrentFontFamily的值可以指向現有的系統字體,但我想指向一個自定義字體,該字體指向ResourceDictionary中定義的字體。
然后我嘗試了:
FontFamily="{StaticResource {Binding CurrentFontFamily}}"
而且顯然存在針對此類組成語法的運行時錯誤。 我只是想知道XAML中是否有通過MVVM View Model在運行時切換自定義字體的?
你有嘗試過嗎?
FontFamily="{Binding CurrentFontFamily}"
編輯:
您可以使用轉換器執行此操作:
public class FontConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var fontName = value as string;
if(!Application.Current.Resources.ContainsKey(fontName))
throw new KeyNotFoundException($"{fontName} not found in resources");
return (string) Application.Current.Resources[fontName];
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
在您的App.xaml中,添加您的轉換器:
<Application.Resources>
<ResourceDictionary>
....
<extensions:FontConverter x:Key="FontConverter"/>
</ResourceDictionary>
</Application.Resources>
然后,您可以綁定您的財產
FontFamily={Binding FontName, Converter={StaticResource FontConverter}}
如https://blog.xamarin.com/triggers-in-xamarin-forms/中所述 ,數據觸發器是一種解決方法。 按照那里的示例,現在我有:
<Label Text="{Binding CurrentText}" Grid.Column="1" Margin="5,0,0,0"
FontSize="{Binding CurrentFontSize}" TextColor="Black"
HorizontalTextAlignment="Start" VerticalTextAlignment="Center">
<Label.Triggers>
<DataTrigger TargetType="Label"
Binding="{Binding Source={x:Reference fontSelected}, Path=SelectedIndex}"
Value="0">
<Setter Property="FontFamily" Value="{StaticResource NormalFont}"/>
</DataTrigger>
<DataTrigger TargetType="Label"
Binding="{Binding Source={x:Reference chartSelected}, Path=SelectedIndex}"
Value="1">
<Setter Property="FontFamily" Value="{StaticResource SuperFont}"/>
</DataTrigger>
</Label.Triggers>
</Label>
XAML中的FontFamily="{Binding CurrentFontFamily}"
是您將不得不使用的方法。 但是,沒有理由CurrentFontFamily
屬性必須指向系統字體。 Xamarin.Forms FontFamily屬性只是一個字符串,因此沒有理由您不應該執行以下操作:
// previous code...
CurrentFontFamily = (whateverCondition == something)
? (string)Application.Current.Resources["SuperFont"]
: (string)Application.Current.Resources["NormalFont"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.