【问题标题】:Property Resources is null or is not IEnumerable属性资源为空或不是 IEnumerable
【发布时间】:2018-04-06 18:56:34
【问题描述】:

当我学习制作 Xamarin.Forms 页面时,我在 Visual Studio 2015 的中断模式下遇到此错误。

未处理的异常: Xamarin.Forms.Xaml.XamlParseException:位置 12:10。属性资源为 null 或不是 IEnumerable

我的 App.xaml 代码如下

    <?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Bisells.App">
    <Application.Resources>

        <!-- Application resource dictionary -->
        <!--  colors  -->
        <!--<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  -->
        <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" />

        <!--  fonts  -->
        <Font
                x:Key="HeaderFont"
                FontFamily="{StaticResource MediumFontFamily}"
                FontSize="30" />
        <Font
                x:Key="SubHeaderFont"
                FontFamily="{StaticResource MediumFontFamily}"
                FontSize="18" />
        <Font
                x:Key="TitleFont"
                FontFamily="{StaticResource MediumFontFamily}"
                FontSize="20" />
        <Font
                x:Key="BodyFont"
                FontFamily="{StaticResource RegularFontFamily}"
                FontSize="18" />


        <!--  styles  -->
        <Style x:Key="PageHeaderLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" />
            <Setter Property="Font" Value="{StaticResource HeaderFont}" />
        </Style>

        <Style x:Key="SubHeaderLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource BodyTextColor}" />
            <Setter Property="Font" Value="{StaticResource SubHeaderFont}" />
        </Style>

        <Style x:Key="ClassTimeLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource TimeColor}" />
            <Setter Property="Font" Value="{StaticResource TitleFont}" />
        </Style>

        <Style x:Key="ClassNameLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" />
            <Setter Property="Font" Value="{StaticResource TitleFont}" />
        </Style>

        <Style x:Key="ClassInstructorLabel" TargetType="Label">
            <Setter Property="TextColor" Value="{StaticResource BodyTextColor}" />
            <Setter Property="Font" Value="{StaticResource BodyFont}" />
        </Style>
    </Application.Resources>
</Application>

xamarin 也没有显示异常发生的确切位置

【问题讨论】:

    标签: xaml xamarin.forms


    【解决方案1】:

    你忘记了ResourceDictionary 标签:

    <Application.Resources>
        <ResourceDictionary>
            <!-- Application resource dictionary -->
            <!--  colors  -->
            <!--<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>
        </ResourceDictionary>
     </Application.Resources>
    

    【讨论】:

    • 感谢异常消失了,但新的异常被解雇了Xamarin.Forms.Xaml.XamlParseException: Position 37:17. Cannot assign property "FontFamily": Property does not exists, or is not assignable, or mismatching type between value and property
    • 你确定你可以在这样的资源中使用字体吗?
    • 不知道。将此作为模板from here
    【解决方案2】:

    像这样在 App.xaml 中定义样式:

    <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>
    

    然后在 xaml 中使用:

    <Label Text="{Binding Name}" Style="{DynamicResource FontLabel}" FontSize="Medium" FontAttributes="Bold" LineBreakMode="NoWrap"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2023-03-28
      • 2017-12-26
      • 2019-02-15
      相关资源
      最近更新 更多