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

Kis*_*Oli 8 c# windows xaml windows-runtime windows-8.1

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

 <Application.Resources>
    <Button x:Key="MyButton"/>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

在 中MainPage.xaml.cs,我尝试以编程方式在我的网格中添加此按钮。

 Button btn = (Button)Application.Current.Resources["MyButton"];
 myGrid.Children.Add(btn);
Run Code Online (Sandbox Code Playgroud)

但它给出了这样的错误:

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

在 MainPage.xaml 中:

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

</Grid>
Run Code Online (Sandbox Code Playgroud)

我不知道我做错了什么。

谢谢。

Bis*_*oli 0

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