【发布时间】:2018-10-10 10:20:14
【问题描述】:
在针对 10240 的 UWP 应用中;我知道当您尝试添加已在 UI 中绘制的元素时,您会收到:
元素已经是另一个元素的子元素。'
但是我在页面的资源中有边框,通过代码添加,收到同样的异常。
<Page
x:Class="App13.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<Border Width="100" Height="100" Background="Red" x:Name="Border" x:Key="Border" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Height="100" Width="100" Content="Add" Tapped="Button_Tapped"/>
</Grid>
using Windows.UI.Xaml.Input;
namespace App13
{
public sealed partial class MainPage : Page
{
public MainPage() => InitializeComponent();
private void Button_Tapped(object sender, TappedRoutedEventArgs e)
{
var border = Resources["Border"] as Border;
if (!(Content is Panel panel)) return;
panel.Children.Add(border);
}
}
}
如果元素在资源字典中,但尚未渲染,它怎么可能是另一个元素的子元素?
【问题讨论】:
-
异常没有显示太多。只有那个例外。我检查了要添加边框的父级的子级,除了触发点击事件的按钮之外什么都没有
-
我查了一下,边框对象中的Parent设置为null
-
这是一个小例子。实际上,我在资源字典中有一些带有 x:bind 的用户控件。我有一个非常具体的问题,这可以帮助我