繁体   English   中英

内容设置不止一次

[英]Content is set more than once

在Visual Studio 2013中使用WPF时,我遇到了一个错误:

错误2属性“内容”设置了多次。

错误1属性“内容”只能设置一次

现在,首先。 我转向谷歌的错误消息,并获得了一个链接到StackOverflow的顶级结果。

XAML - 属性“内容”设置不止一次

属性“内容”设置不止一次

属性内容设置不止一次

包括MSDN帖子:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/686be076-954f-4373-a2e6-fc42a6a27718/error-the-property-content-is-set-more-than-once?forum= WPF

虽然基于原始海报代码提供了信息丰富的定制解决方案,但我还没有遇到一个实际的基本解决方案,详细说明了这个错误的原因(XAML新手),虽然这可能是多个报告问题的重复。 我个人宁愿避免发布有问题的代码来获得量身定制的解决方案。 我更愿意来到这里,并询问社区的原因,为什么新手XAMP / WPF开发人员可能会遇到这个应用程序和解决方案,而不是顶级,顶级的最佳实践。 而是来自更多WPF / XAMP开发人员的建议,关于如何轻松识别解决方案并继续进行更远的调试步骤


为了论证:

<Window x:Class="WPFT.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="197.198" Width="427.95">
    <TextBlock>Hello</TextBlock>
    <TextBlock>World</TextBlock>
</Window>

一个窗口只能包含1个元素。

在你的代码中

<Window x:Class="WPFT.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="197.198" Width="427.95">
    <TextBlock>Hello</TextBlock>
    <TextBlock>World</TextBlock>
</Window>

你的窗口有2个文本块,你可以尝试类似的东西

<Window x:Class="WPFT.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="197.198" Width="427.95">
    <Grid>
        <TextBlock>Hello</TextBlock>
        <TextBlock>World</TextBlock>
    </Grid>
</Window>

如果使用Content依赖项属性在任何UIElement中设置多个元素,则会出现此错误。 您需要在面板中包含多个元素,以便Content属性只有一个子元素。 例如...

<Button>
    <StackPanel Orientation="Horizontal">
        <Image />
        <TextBlock />
    </StackPanel>
</Button>

<Border>
    <StackPanel>
        <TextBlock />
        <Image />
        <DatePicker />
    </StackPanel>
</Border>

Button和Border元素之间的区域是用于指定的简写:

<Button>
    <Button.Content>
        <!-- Content goes here -->
    </Button.Content>
</Button>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM