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

ⅰ亾dé卋堺 提交于 2019-12-10 10:07:30

问题


In App.xaml I have added Application Resources with a button as:

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

In MainPage.xaml.cs, I tried to add this button programatically in my grid.

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

But it gives error like this:

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

In MainPage.xaml:

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

</Grid>

I don't know what I'm doing wrong.

Thanks.


回答1:


You cannot add the element that is already child of another element. It's like your child cannot be the child of another guy man.




回答2:


This exception is usually thrown if you're using more than one instance of the control you defined in your application resources. If that is the case, you should do:

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

EDIT: it seems WInRT doesn't support x:shared attribute.

There is a workaround using ControlTemplates: http://www.gdomc.com/0428/binding-the-content-property-of-a-contentcontrol-in-winrt/



来源:https://stackoverflow.com/questions/38184819/no-installed-components-were-detected-element-is-already-the-child-of-another-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!