【问题标题】:No installed components were detected. Element is already the child of another element未检测到已安装的组件。元素已经是另一个元素的子元素
【发布时间】:2023-03-19 00:59:01
【问题描述】:

在 App.xaml 中,我添加了带有按钮的应用程序资源:

 <Application.Resources>
    <Button x:Key="MyButton"/>
</Application.Resources>

MainPage.xaml.cs 中,我尝试以编程方式将这个按钮添加到我的网格中。

 Button btn = (Button)Application.Current.Resources["MyButton"];
 myGrid.Children.Add(btn);

但它给出了这样的错误:

No installed components were detected. Element is already the child of another element.

在 MainPage.xaml 中:

 <Grid x:Name="myGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

</Grid>

我不知道我做错了什么。

谢谢。

【问题讨论】:

    标签: c# windows xaml windows-runtime windows-8.1


    【解决方案1】:

    您不能添加已经是另一个元素的子元素的元素。这就像你的孩子不可能是另一个男人的孩子。

    【讨论】:

    【解决方案2】:

    如果您使用多个在应用程序资源中定义的控件实例,通常会引发此异常。如果是这种情况,您应该这样做:

    <Button x:Key="MyButton" x:Shared="false"/>
    

    编辑:似乎 WInRT 不支持 x:shared 属性。

    有一个使用 ControlTemplates 的解决方法:http://www.gdomc.com/0428/binding-the-content-property-of-a-contentcontrol-in-winrt/

    【讨论】:

    • 我尝试了同样的方法,但它在 app.xaml 中出现如下错误:Syntax Error found in XBF generation
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    相关资源
    最近更新 更多