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