我只是在我的WPF申请中被一些奇怪的行为绊倒了。
我试图确保,某些窗口将显示在与其“父窗口”相同的屏幕上。
为此,我使用了Window属性WindowStartupLocation = CenterOwner,当直接从Visual 运行时,它的功能是预期的。
奇怪的是,当我手动运行完全相同的.exe (...\Project\bin\Debug\Project.exe) “”时,“子窗口”总是从我的主屏幕开始,不管我是否移动了“父窗口”。
所以当我将“父窗口”移到第二个屏幕,打开“子窗口”时,它仍然会显示在我的主屏幕上,而不是像预期的那样显示在我的第二个屏幕上。
因此,我的问题是,如果我“手工”运行或直接从VisualStudio.运行.exe,我就会得到不同的行为。
我使用的ExtensionMethods:
public static class ExtensionMethods {
//I created these Extensions, to easily show Windows on the same screen, as a give Window.
public static bool? ShowDialog(this Window child, Window owner) {
child.WindowStartupLocation = WindowStartupLocation.CenterOwner;
child.Owner = owner;
return child.ShowDialog();
}
public static void Show(this Window child, Window owner) {
child.WindowStartupLocation = WindowStartupLocation.CenterOwner;
child.Owner = owner;
child.Show();
}
}我如何打开一个新的“儿童窗口”
public void test(Window pw) {
ChildWindow cw = new ChildWindow();
cw.ShowDialog(pw); //Edited typo from "cw.ShowDialog(w);"
}编辑:
我刚刚创建了一个新项目,其中只有两个Windows和扩展方法,以便在干净的环境中试用它。
在这个过程中,我发现只有当第二个窗口最大化时,问题才会发生。无论是在xaml中设置WindowState = Maximized时,还是通过子窗口构造函数上的代码设置时.
不过,直接在Visual中,行为与预期的一样,但在直接运行.exe时却不是这样。
我的“新鲜”项目的整个代码:
MainWindow XAML:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Open Child" Click="btn"/>
</Grid>
</Window>MainWindow cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3 {
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
public void btn(object sender, RoutedEventArgs e) {
test(this);
}
public void test(Window pw) {
ChildWindow cw = new ChildWindow();
cw.ShowDialog(pw);
}
}
public static class ExtensionMethods {
//I created these Extensions, to easily show Windows on the same screen, as a give Window.
public static bool? ShowDialog(this Window child, Window owner) {
child.WindowStartupLocation = WindowStartupLocation.CenterOwner;
child.Owner = owner;
return child.ShowDialog();
}
public static void Show(this Window child, Window owner) {
child.WindowStartupLocation = WindowStartupLocation.CenterOwner;
child.Owner = owner;
child.Show();
}
}
}ChildWindow XAML:
<Window x:Class="WpfApplication3.ChildWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ChildWindow" Height="300" Width="300"
WindowState="Maximized">
<Grid>
<TextBlock Text="CHILD"/>
</Grid>
</Window>ChildWindow cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApplication3 {
/// <summary>
/// Interaktionslogik für ChildWindow.xaml
/// </summary>
public partial class ChildWindow : Window {
public ChildWindow() {
InitializeComponent();
}
}
}发布于 2019-05-15 09:43:51
尽管如此,我仍然不明白为什么会发生这种行为,我仍然在周围找到了一个工作。
这个问题似乎腐蚀到了“儿童窗口”的最大限度。当WindowState没有改变时,无论是在VS还是在直接执行时,行为都是预期的。
我的工作是,最大化窗口,在加载窗口之后,因为它就出现在正确的屏幕上。
子窗口构造器中的示例代码:
public ChildWindow() {
InitializeComponent();
this.Loaded += delegate(object ds, RoutedEventArgs de) {
((Window)ds).WindowState = System.Windows.WindowState.Maximized;
};
}尽管如此,如果有人无意中发现了这个问题,an还有一些关于为什么会发生这个问题的进一步信息,可以随意分享。
对我来说,这是一个小的不习惯,但它是有效的,所以,我将采取目前的做法。
编辑:
对于所有感兴趣的人,为了更少的不便,我稍微修改了一下我的ExtensionMethod:
public static bool? ShowDialog(this Window child, Window owner) {
child.WindowStartupLocation = WindowStartupLocation.CenterOwner;
child.Owner = owner;
//Detects, if the Window should be Maximized. If so set the State to
//Normal instead and add an Eventhandler to Maximize the Window after beeing loaded.
if (child.WindowState == WindowState.Maximized) {
child.WindowState = WindowState.Normal;
child.Loaded += delegate(object ds, RoutedEventArgs de) {
((Window)ds).WindowState = System.Windows.WindowState.Maximized;
};
}
return child.ShowDialog();
}这样,您就可以像往常一样在XMAL中设置WindowState,而不需要自己显式地在加载的事件中设置。
https://stackoverflow.com/questions/56145243
复制相似问题