Xamarin Forms构建错误 - 找不到密钥的StaticResource

时间:2017-10-11 14:45:01

标签: xamarin.forms staticresource

我只是在学习XAML和Xamrin。我正在尝试学习静态样式的工作原理。这是我的XAML代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Styles"
             x:Class="Styles.MainPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <Style x:key="buttonStyle" TargetType="Button">
                <Setter Property="HorizontalOptions" Value="Center"/>
                <Setter Property="VerticalOptions" Value="Center" />
                <Setter Property="TextColor" Value="Red"/>
                <Setter Property="FontSize" Value="Small"/>
            </Style>
            <Style TargetType="Label">
                <Setter Property="HorizontalOptions" Value="Center"/>
                <Setter Property="VerticalOptions" Value="Center" />
                <Setter Property="TextColor" Value="Blue"/>
                <Setter Property="FontSize" Value="20"/>
            </Style>
            <Style x:key="baseStyle" TargetType="View">
                <Setter Property="HorizontalOptions" Value="Center"/>
                <Setter Property="VerticalOptions" Value="Center" />
            </Style>
            <Style x:key="entryStyle" TargetType="Entry" BasedOn="{StaticResource baseStyle}">
                <Setter Property="TextColor" Value="Green"/>
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources> 

    <ContentPage.Content>
        <StackLayout Padding="20">
            <Label Text="This is label 1 using implicit style"/>
            <Label Text="This is label 2"/>
            <Button 
                Text="Not using the button style"
                    BorderWidth="2"
                HorizontalOptions="Center"
                VerticalOptions="Center"
                    WidthRequest="200"/>
            <Button Style="{StaticResource buttonStyle}" 
                    Text="Using explicit style" 
                    BorderWidth="2"
                    WidthRequest="200"/>
            <Entry Style="{StaticResource entryStyle}" Placeholder="This enty uses an inherited style"/>

            <Button Style="{StaticResource buttonStyle}" 
                    Text="Using explicit style" 
                    BorderWidth="2"
                    WidthRequest="200"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

编译工作正常,但在运行时,我得到了这个例外:

 [ERROR] FATAL UNHANDLED EXCEPTION: Xamarin.Forms.Xaml.XamlParseException: Position 25:58. StaticResource not found for key baseStyle

我不明白为什么我会收到此错误,因为样式'key'已在前一行中定义。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

x:Key属性的第一个字母必须为大写。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Styles"
             x:Class="Styles.MainPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <Style x:Key="buttonStyle" TargetType="Button">
                <Setter Property="HorizontalOptions" Value="Center"/>
                <Setter Property="VerticalOptions" Value="Center" />
                <Setter Property="TextColor" Value="Red"/>
                <Setter Property="FontSize" Value="Small"/>
            </Style>
            <Style TargetType="Label">
                <Setter Property="HorizontalOptions" Value="Center"/>
                <Setter Property="VerticalOptions" Value="Center" />
                <Setter Property="TextColor" Value="Blue"/>
                <Setter Property="FontSize" Value="20"/>
            </Style>
            <Style x:Key="baseStyle" TargetType="View">
                <Setter Property="HorizontalOptions" Value="Center"/>
                <Setter Property="VerticalOptions" Value="Center" />
            </Style>
            <Style x:Key="entryStyle" TargetType="Entry" BasedOn="{StaticResource baseStyle}">
                <Setter Property="TextColor" Value="Green"/>
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources> 

    <ContentPage.Content>
        <StackLayout Padding="20">
            <Label Text="This is label 1 using implicit style"/>
            <Label Text="This is label 2"/>
            <Button 
                Text="Not using the button style"
                    BorderWidth="2"
                HorizontalOptions="Center"
                VerticalOptions="Center"
                    WidthRequest="200"/>
            <Button Style="{StaticResource buttonStyle}" 
                    Text="Using explicit style" 
                    BorderWidth="2"
                    WidthRequest="200"/>
            <Entry Style="{StaticResource entryStyle}" Placeholder="This enty uses an inherited style"/>

            <Button Style="{StaticResource buttonStyle}" 
                    Text="Using explicit style" 
                    BorderWidth="2"
                    WidthRequest="200"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>