分配给属性时出现XamlParseException

时间:2014-02-14 07:39:05

标签: c# wpf xaml windows-phone-7 windows-phone-8

此问题最初发布为here。虽然OP接受了解决方案,但我还是无法弄清楚导致异常的原因。我做了一些进一步的测试并且失败了。

代码非常简单 - 只有xaml内容的Windows Phone App:

<phone:PhoneApplicationPage.Resources>
    <Image x:Key="IButton" Source="Resources/firstImage.png"/>
</phone:PhoneApplicationPage.Resources>

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <Button x:Name="first" Content="{StaticResource IButton}" VerticalAlignment="Center" Grid.Row="0"/>
    <Button x:Name="second" VerticalAlignment="Center" Grid.Row="1">
        <Image Source="Resources/firstImage.png"/>
    </Button>
</Grid>

乍一看,一切看起来都不错,VS设计师正确显示两个带有图像的按钮。当我尝试deply App时,我得到了XamlParseException:

  

无法分配属性'System.Windows.Controls.ContentControl.Content'。

问题与第一个按钮有关 - 第二个按钮正在运行

很奇怪。我尝试过更改Build Action(资源/内容),清理项目,但没有成功。

相反,非常相似的WPF应用程序没有问题。您只需按运行并看到两个按钮:

<Window.Resources>
    <Image x:Key="IButton" Source="Resources/firstImage.png"/>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <Button x:Name="first" Content="{StaticResource IButton}" VerticalAlignment="Center" Grid.Row="0"/>
    <Button x:Name="second" VerticalAlignment="Center" Grid.Row="1">
        <Image Source="Resources/firstImage.png"/>
    </Button>
</Grid> 

有谁知道什么是错的?两个应用程序(WP / WPF)你can get here

1 个答案:

答案 0 :(得分:0)

可能是差异

<Button x:Name="second" VerticalAlignment="Center" Grid.Row="1">
<Button.Content>
        <Image Source="Resources/firstImage.png"/>
</Button.Content>
</Button>

Button.Content标签在这里是额外的。

此外,图像的构建操作应设置为此处的内容。

这至少在我的案例中有效。

相关问题