Xamarin.Forms中FontFamily属性赋值报错问题求助
Fixing "Cannot assign property 'FontFamily'" Error in Xamarin.Forms App.xaml
Hey there, let's break down what's going wrong here and fix it step by step.
First, let's unpack the root causes of your error:
- The original code uses
<Font>resources to bundleFontFamilyandFontSize, but older Xamarin.Forms versions have limited support for directly binding thisFonttype to a Label'sFontproperty—this is likely where the "property does not exist or type mismatch" error comes from. - Your modified code has several syntax and reference issues:
- A broken comment:
<Color x:Key="BodyTextColor">#C3C3C3</Color>-->is invalid and will throw parsing errors. - You removed the original
<Font>resources but still reference them in your styles (like{StaticResource HeaderFont}), which no longer exist. - The code is truncated at
ClassInstru, leaving an incomplete style definition.
- A broken comment:
Step-by-Step Fix
Let's rewrite your resource dictionary properly, avoiding the problematic <Font> resources and fixing all syntax issues:
- Clean up invalid comments
- Remove the
<Font>resource definitions (we'll set font properties directly in styles) - Update each Label style to use
FontFamilyandFontSizedirectly instead of the removedFontresource - Ensure all styles are complete
Here's the corrected full code:
<Application.Resources> <ResourceDictionary> <!-- Colors --> <!-- Commented out old colors properly --> <!--<Color x:Key="HeaderTextColor">#585858</Color> <Color x:Key="BodyTextColor">#C3C3C3</Color>--> <Color x:Key="TimelineColor">#E4B6C3</Color> <Color x:Key="HeaderTextColor">#3C3C3C</Color> <Color x:Key="BodyTextColor">#869EAC</Color> <Color x:Key="TimeColor">#A64C79</Color> <!-- Font Families (keep these, they're correctly platform-targeted) --> <OnPlatform x:Key="RegularFontFamily" x:TypeArguments="x:String" Android="sans-serif" iOS="HelveticaNeue" /> <OnPlatform x:Key="LightFontFamily" x:TypeArguments="x:String" Android="sans-serif-light" iOS="HelveticaNeue-Light" /> <OnPlatform x:Key="MediumFontFamily" x:TypeArguments="x:String" Android="sans-serif-medium" iOS="HelveticaNeue-Medium" /> <!-- Font Sizes --> <x:Double x:Key="HeaderFontSize">30</x:Double> <x:Double x:Key="SubHeaderFontSize">18</x:Double> <x:Double x:Key="BodyFontSize">18</x:Double> <x:Double x:Key="TitleFontSize">20</x:Double> <!-- Label Styles --> <Style x:Key="PageHeaderLabel" TargetType="Label"> <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" /> <Setter Property="FontFamily" Value="{StaticResource MediumFontFamily}" /> <Setter Property="FontSize" Value="{StaticResource HeaderFontSize}" /> </Style> <Style x:Key="SubHeaderLabel" TargetType="Label"> <Setter Property="TextColor" Value="{StaticResource BodyTextColor}" /> <Setter Property="FontFamily" Value="{StaticResource MediumFontFamily}" /> <Setter Property="FontSize" Value="{StaticResource SubHeaderFontSize}" /> </Style> <Style x:Key="ClassTimeLabel" TargetType="Label"> <Setter Property="TextColor" Value="{StaticResource TimeColor}" /> <Setter Property="FontFamily" Value="{StaticResource MediumFontFamily}" /> <Setter Property="FontSize" Value="{StaticResource TitleFontSize}" /> </Style> <Style x:Key="ClassNameLabel" TargetType="Label"> <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" /> <Setter Property="FontFamily" Value="{StaticResource MediumFontFamily}" /> <Setter Property="FontSize" Value="{StaticResource TitleFontSize}" /> </Style> <Style x:Key="ClassInstructorLabel" TargetType="Label"> <Setter Property="TextColor" Value="{StaticResource BodyTextColor}" /> <Setter Property="FontFamily" Value="{StaticResource RegularFontFamily}" /> <Setter Property="FontSize" Value="{StaticResource BodyFontSize}" /> </Style> </ResourceDictionary> </Application.Resources>
Why This Works
- By setting
FontFamilyandFontSizedirectly in each style, we avoid the type mismatch issues with the<Font>resource that caused your original error. - We fixed all syntax errors (broken comments, truncated styles, missing resource references) that were compounding the problem.
- The
OnPlatformfont family definitions are kept intact since they correctly target native fonts for Android and iOS.
Give this a try, and your error should disappear!
内容的提问来源于stack exchange,提问作者MilkBottle
相关产品推荐
相关产品推荐

