1

以下按预期工作:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Resources>
        <ResourceDictionary>
            <Button x:Key="MyButton">Hello</Button>
        </ResourceDictionary>
    </Page.Resources>
    <Grid>
        <StaticResource ResourceKey="MyButton"/>
    </Grid>
</Page>

这不会:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Resources>
        <ResourceDictionary>
            <Button x:Key="MyButton">Hello</Button>
        </ResourceDictionary>
    </Page.Resources>
    <Grid>
        <ItemsControl>
            <StaticResource ResourceKey="MyButton"/>
        </ItemsControl>
    </Grid>
</Page>

(要重现,请在 VS 2012 或 VS 2013 RC 中创建一个新的空白应用程序,并将 MainPage.xaml 的内容替换为上述内容。)

我收到一个未处理的异常 UnhandledExceptionEventArgs.Message = "元素已经是另一个元素的子元素。"

我究竟做错了什么?这应该工作吗?

虽然在这里并不明显,但我认为我有充分的理由将我的一些 UI 元素放入资源字典中。我知道每个 UI 元素只能出现在一个地方,但即使在第二个示例中是这种情况,错误消息也会声称并非如此。

4

0 回答 0