C# 为什么它不起作用?StartupLocation=WPF中的中心所有者
我想将我的子窗口定位到父窗口的中心。但它在WPF中不起作用 我设置C# 为什么它不起作用?StartupLocation=WPF中的中心所有者,c#,wpf,C#,Wpf,我想将我的子窗口定位到父窗口的中心。但它在WPF中不起作用 我设置StartupLocation=CenterOwner。但它不起作用 MainWindow.xaml.cs public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Task.Run(async () => { await
StartupLocation=CenterOwner
。但它不起作用
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Task.Run(async () =>
{
await Task.Delay(1000);
Dispatcher.Invoke(() => new TestWindow().ShowDialog());
});
}
}
public partial class TestWindow : Window
{
public TestWindow()
{
InitializeComponent();
}
}
MainWindow.xaml
<Window x:Class="WpfApp29.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp29"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
<Window x:Class="WpfApp29.TestWindow"
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"
xmlns:local="clr-namespace:WpfApp29"
WindowStartupLocation="CenterOwner"
mc:Ignorable="d"
Title="TestWindow" Height="300" Width="300">
<Grid>
</Grid>
</Window>
TestWindow.xaml
<Window x:Class="WpfApp29.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp29"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
<Window x:Class="WpfApp29.TestWindow"
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"
xmlns:local="clr-namespace:WpfApp29"
WindowStartupLocation="CenterOwner"
mc:Ignorable="d"
Title="TestWindow" Height="300" Width="300">
<Grid>
</Grid>
</Window>
我在
main窗口中调用ShowDialog
函数。但是Testwindow
未定位到CenterOwner
(MainWindow
)您必须将测试窗口的所有者设置为MainWindow,如下所示:
Task.Run(async () =>
{
await Task.Delay(1000);
Dispatcher.Invoke(() =>
{
TestWindow testWindow = new TestWindow();
testWindow.Owner = this;
testWindow.ShowDialog();
});
});
否则,测试窗口的所有者为空,测试窗口将在“随机”位置打开。您必须将测试窗口的所有者设置为主窗口,如:
Task.Run(async () =>
{
await Task.Delay(1000);
Dispatcher.Invoke(() =>
{
TestWindow testWindow = new TestWindow();
testWindow.Owner = this;
testWindow.ShowDialog();
});
});
否则,测试窗口的所有者为空,测试窗口将在“随机”位置打开。工作正常。谢谢,工作顺利。谢谢您。