Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么它不起作用?StartupLocation=WPF中的中心所有者_C#_Wpf - Fatal编程技术网

C# 为什么它不起作用?StartupLocation=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

我想将我的子窗口定位到父窗口的中心。但它在WPF中不起作用

我设置
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();
    });
});

否则,测试窗口的所有者为空,测试窗口将在“随机”位置打开。

工作正常。谢谢,工作顺利。谢谢您。