【问题标题】:Xamarin Forms build error - StaticResource not found for keyXamarin Forms 构建错误 - 找不到密钥的静态资源
【发布时间】:2017-10-11 14:45:01
【问题描述】:

我只是在学习 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

我不明白为什么会出现此错误,因为样式“键”已在前一行中定义。非常感谢任何帮助。

【问题讨论】:

    标签: xamarin.forms staticresource


    【解决方案1】:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多