Kishor Bikram Oli
Kishor Bikram Oli

Reputation: 1828

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

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.

Upvotes: 9

Views: 6314

Answers (3)

smllstrwbrry
smllstrwbrry

Reputation: 1

I am new to this, but i did similar thing and I got the same mistake . This didn`t work for me:

btn = new Button { Text = $"{i}", HeightRequest = 50, WidthRequest = 50 };
flexLayout1.Children.Add(btn)
flexLayout2.Children.Add(btn)

But this worked:

flexLayout1.Children.Add(new Button { Text = $"{i}", HeightRequest = 50, WidthRequest = 50 });
flexLayout2.Children.Add(new Button { Text = $"{i}", HeightRequest = 50, WidthRequest = 50 });

Upvotes: 0

Arie
Arie

Reputation: 5373

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/

Upvotes: 3

Biswas Khayargoli
Biswas Khayargoli

Reputation: 1024

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.

Upvotes: 1

Related Questions