Xamarin Forms build error - StaticResource not found for key

jacDeveloper picture jacDeveloper · Oct 11, 2017 · Viewed 7.9k times · Source

I'm just learning XAML and Xamrin. I'm trying to learn how static styles work. Here's my XAML code:

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

Compile works fine, but when running, I get this exception:

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

I don't understand why I get this error since the style 'key' is already defined on the previous line. Any help is greatly appreciated.

Answer

Diego Rafael Souza picture Diego Rafael Souza · Oct 11, 2017

The x:Key attribute must have the first letter in uppercase.

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