C# 分配给属性时发生XamlParseException

C# 分配给属性时发生XamlParseException,c#,wpf,xaml,windows-phone-7,windows-phone-8,C#,Wpf,Xaml,Windows Phone 7,Windows Phone 8,这个问题最初是发布的。虽然OP接受了解决方案,但我仍然无法找出导致异常的原因。我做了一些进一步的测试,结果失败了 代码非常简单-Windows Phone应用程序仅包含xaml内容: <phone:PhoneApplicationPage.Resources> <Image x:Key="IButton" Source="Resources/firstImage.png"/> </phone:PhoneApplicationPage.Resources>

这个问题最初是发布的。虽然OP接受了解决方案,但我仍然无法找出导致异常的原因。我做了一些进一步的测试,结果失败了

代码非常简单-Windows Phone应用程序仅包含xaml内容:

<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 designer正确地显示了两个带有图像的按钮。当我尝试卸载应用程序时,会出现XamlParseException:

未能分配到属性“System.Windows.Controls.ContentControl.Content”

问题涉及第一个按钮-第二个按钮运行正常

很奇怪。我尝试过更改构建操作资源/内容,清理项目,但没有成功

相反,非常相似的WPF应用程序可以毫无问题地工作。您只需点击Run,即可看到两个按钮:

<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。可能是因为这是不同之处

<Button x:Name="second" VerticalAlignment="Center" Grid.Row="1">
<Button.Content>
        <Image Source="Resources/firstImage.png"/>
</Button.Content>
</Button>
一个按钮。这里的内容标签是额外的

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


这至少在我的情况下有效。

这不是问题所在。第二个按钮可以正常工作。你的代码当然会起作用。问题在于第一个按钮以及为什么它不工作。第二个按钮仅用于检查图像文件是否存在,依此类推。