WindowStartupLocation什么时候生效

When does WindowStartupLocation take effect

WPF 什么时候生效?this.WindowStartupLocation = WindowStartupLocation.CenterOwner;

我希望这是默认行为,但如果某些 window 布局信息存在(顶部、左侧...),我想在 Loaded 事件中设置它们 window。但这不能正常工作。

如果我注释掉 WindowStartupLocation,它会起作用。

也许以前有人解决过这个问题。谢谢!

WindowStartupLocation 生效并仅在启动时将 window 设置在中心,因此一旦设置了 window,它就不会对调整大小或移动 [=20] 产生任何其他影响=].

MSDN 说:

Gets or sets the position of the window when first shown.

另请参阅此 MSDN blog,它建议您如何将其连接为默认行为:

private void SizeChangedHandler(Object sender, SizeChangedEventArgs e)
{
    Rect workArea = SystemParameters.WorkArea;
    this.Left = (workArea.Width - this.ActualWidth) / 2;
    this.Top = (workArea.Height - this.ActualHeight) / 2;
}

private void LocationChangedHandler(Object sender, EventArgs e)
{
    Rect workArea = SystemParameters.WorkArea;
    this.Left = (workArea.Width - this.ActualWidth) / 2;
    this.Top = (workArea.Height - this.ActualHeight) / 2;
}