【问题标题】:XamlParseException while assigning to property分配给属性时出现 XamlParseException
【发布时间】:2014-02-14 07:39:04
【问题描述】:

这个问题最初是在here 发布的。尽管 OP 接受了解决方案,但我仍然无法弄清楚导致异常的原因。我做了一些进一步的测试,但失败了。

代码非常简单 - Windows Phone App 仅包含 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 设计器正确显示了两个带有图像的按钮。当我尝试部署应用程序时,我得到 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)你can get here

【问题讨论】:

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


    【解决方案1】:

    也许这就是区别

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

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

    图像的构建动作也应该在这里设置为内容。

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

    【讨论】:

    • 这不是问题。第二个按钮可以正常工作。您的代码当然可以工作。问题涉及第一个按钮以及它为什么不起作用的事实。第二个按钮只是检查图像文件是否存在等等。
    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    相关资源
    最近更新 更多