Xamarin Forms 构建错误 - 找不到密钥的 StaticResource
Xamarin Forms build error - StaticResource not found for key
我正在学习 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' 已在上一行中定义。非常感谢任何帮助。
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>
我正在学习 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' 已在上一行中定义。非常感谢任何帮助。
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>