我的程序的用户报告打开新窗口的按钮导致它挂起2秒然后崩溃。虽然它没有在我测试过的任何计算机上崩溃。
即使打开像版权信息文本那样简单的窗口,也会让它崩溃。
他们的PC规格:
这是我打开一个窗口的方式:
这是从MainWindow按下的。
private Boolean IsMyWindowOpened = false;
private void buttonMyWindow_Click(object sender, RoutedEventArgs e)
{
// Detect which screen we're on
var allScreens = System.Windows.Forms.Screen.AllScreens.ToList();
var thisScreen = allScreens.SingleOrDefault(s => this.Left >= s.WorkingArea.Left && this.Left < s.WorkingArea.Right);
if (thisScreen == null) thisScreen = allScreens.First();
// Check if MyWindow is already open
if (IsMyWindowOpened) return;
// Start Window
mywindow = new MyWindow();
// Only allow 1 Window instance
mywindow.ContentRendered += delegate { IsMyWindowOpened = true; };
mywindow.Closed += delegate { IsMyWindowOpened = false; };
// Position Relative to MainWindow
// Keep from going off screen
mywindow.Left = Math.Max(this.Left - mywindow.Width - 12, thisScreen.WorkingArea.Left);
mywindow.Top = Math.Max(this.Top - 0, thisScreen.WorkingArea.Top);
// Open Window
mywindow.Show();
}
<Window x:Class="MyProgram.MyWindow"
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:System="clr-namespace:System;assembly=mscorlib"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
Loaded="Window_Loaded"
Height="200"
Width="450"
ResizeMode="CanMinimize"
UseLayoutRounding="True"
Icon="Resources/Images/Icons/icon.ico"
</Window>
public partial class MyWindow : Window
{
private MainWindow mainwindow;
public MyWindow(MainWindow mainwindow)
{
InitializeComponent();
this.mainwindow = mainwindow;
this.MinWidth = 450;
this.MinHeight = 200;
this.MaxWidth = 450;
this.MaxHeight = 200;
// Other Methods Here
}
// Other Methods Here
}
Alex.C是对的。我在System.Windows.Forms.Screen.AllScreens中遇到了类似的问题。它有时只返回null。所以用var thisScreen = Screen.FromControl(this)替换代码;应该解决这个问题
崩溃的最可能根本原因是:
用。替换你的过滤
var thisScreen = Screen.FromControl(this);
见下文
用。替换你的过滤
var thisScreen = Screen.FromControl(this);
可能的远射,但可能是相关的!
我遇到了Hyper-V,英特尔显示驱动程序和多显示器设置的组合问题,在安装Hyper-V时,显示设置中包含虚假显示器。在这种状态下,将额外的监视器与任何类型的电缆连接都不起作用(无信号)。它只会在Windows重启期间在额外的监视器上显示启动微调器,然后在所有尝试中都没有信号)。
它看起来像显示设置中的这些图片之一(即使没有连接任何额外的显示器):
找一台带有基于Intel主板和英特尔显示驱动程序的计算机,并尝试通过安装Hyper-V进入所述状态。如果偶然的当前版本的Hyper-V或英特尔显示驱动程序不再表现出奇怪的行为,请尝试查找版本。
这些问题似乎没有太大的可能导致您描述的崩溃。为了确保,您应该添加一些错误记录/检查以便将来更好地进行故障排除!