未检测到已安装的组件。元素已经是另一个元素的子元素

2024-03-20

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

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

In 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>

我不知道我做错了什么。

Thanks.


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

<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/ http://www.gdomc.com/0428/binding-the-content-property-of-a-contentcontrol-in-winrt/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

未检测到已安装的组件。元素已经是另一个元素的子元素 的相关文章

随机推荐