You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 bundle FontFamily and FontSize, but older Xamarin.Forms versions have limited support for directly binding this Font type to a Label's Font property—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.

Step-by-Step Fix

Let's rewrite your resource dictionary properly, avoiding the problematic <Font> resources and fixing all syntax issues:

  1. Clean up invalid comments
  2. Remove the <Font> resource definitions (we'll set font properties directly in styles)
  3. Update each Label style to use FontFamily and FontSize directly instead of the removed Font resource
  4. 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 FontFamily and FontSize directly 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 OnPlatform font 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:00:21