C# WPF 无框架窗体前端或后台设置居中都无效,请问各位大虾

前端: WindowStartupLocation="CenterScreen"
后台: WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;//屏幕居中设置失败,未解决
但是运行的时候还是不能居中显示,求大虾解答

检查窗体的height和width属性是否忘记设置,如果仍然未能解决问题,
可以在合适的位置调用如下函数:

private void CenterWindowOnScreen()
{
    double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
    double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
    double windowWidth = this.Width;
    double windowHeight = this.Height;
    this.Left = (screenWidth / 2) - (windowWidth / 2);
    this.Top = (screenHeight / 2) - (windowHeight / 2);
}

如果对您有帮助,请采纳答案好吗,谢谢!