C#下WPF主窗体中加载别的Windows窗体时报: 无法将 Owner 属性设置为之前未显示的 Window的错误,代码:
public MainWindow()
{
InitializeComponent();
sceenAnimationWin = new SceenAnimationWin();
sceenAnimationWin.Owner = this;
sceenAnimationWin.Show();
}
错误的原因应该是MainWindow本身还未加载完成。解决方法是等窗体完全加载后,在初始化要加载的窗体。在windows的xaml文件中增加ContentRendered="WindowRendered",并在代码中加入WindowRendered处理方法,然后把MainWindow方法中的窗口初始化创建的代码移到WindowRendered中,问题解决。代码如下:
private void WindowRendered(object sender, EventArgs e)
{
sceenAnimationWin = new SceenAnimationWin();
sceenAnimationWin.Owner = this;
sceenAnimationWin.Show();
}