我正在平板电脑模式下为Surface pro开发一个wpf应用程序。我在不同的计算机上编码,一切正常。在表面上测试应用程序时,会发生一些奇怪的事情:
在应用程序期间,将打开第二个窗口,以便修改某些设置。该窗口包含一个按钮以关闭窗口(参见下面的代码)。在表面上单击此按钮时,整个应用程序将关闭(包括主窗口)。在我编写代码的计算机上,这种情况从未发生过,所以在我看来,问题在于硬件。
感谢您的帮助!
正如评论中所讨论的那样,我已经尝试了几件事情,问题仍然存在。我现在删除了大部分应用程序代码,只剩下基本功能,问题仍然没有消失。我将发布以下代码:
<Application x:Class="TestNamespace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestNamespace"
Startup="ApplicationStartup">
<Application.Resources>
<Style TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
</Application.Resources>
</Application>
public partial class App : Application
{
public MainWindow window = new MainWindow();
public Monitoring monitoring;
private void ApplicationStartup(object sender, StartupEventArgs e)
{
window.Show();
monitoring = new Monitoring();
window.Content = monitoring.gui;
this.DispatcherUnhandledException += App_DispatcherUnhandledException;
this.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
}
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine("App1: There has been an unhandled exception");
throw new NotImplementedException();
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine("App2: There has been an unhandled exception");
throw new NotImplementedException();
}
}
<UserControl x:Class="TestNamespace.GUI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:oxy="http://oxyplot.org/wpf"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:mi="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:local="clr-namespace:TestNamespace">
<Grid Background="Black">
<Button HorizontalAlignment="Center" VerticalAlignment="Center"
Width="300" Height="300"
Content="NewWindow" Click="SettingsButton_Click"/>
</Grid>
</UserControl>
public partial class GUI : UserControl
{
private Monitoring monitoring;
public GUI(Monitoring monitoring)
{
this.monitoring = monitoring;
InitializeComponent();
this.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine(" GUI: There has been an unhandled exception");
throw new NotImplementedException();
}
private void SettingsButton_Click(object sender, RoutedEventArgs e)
{
monitoring.settingsWindow = new testWindow();
monitoring.settingsWindow.Show();
}
}
public class Monitoring
{
private App currentApp = (App)Application.Current;
public GUI gui;
public Window settingsWindow;
public Monitoring()
{
gui = new GUI(this);
}
}
<Window x:Class="TestNamespace.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:TestNamespace"
mc:Ignorable="d"
Title="testWindow"
ResizeMode="NoResize" WindowState="Maximized" WindowStyle="None">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="245,200,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
</Window>
public partial class testWindow : Window
{
public testWindow()
{
InitializeComponent();
this.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine("TestWindow: There has been an unhandled exception");
throw new NotImplementedException();
}
private void button_Click(object sender, RoutedEventArgs e)
{
e.Handled = true;
this.Close();
}
}
<Window x:Class="TestNamespace.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestNamespace"
mc:Ignorable="d"
ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowState="Maximized" WindowStyle="None">
<Grid Background="White">
<TextBox x:Name="textBox" HorizontalAlignment="Center" Height="47" TextWrapping="Wrap" Text="LOADING..." VerticalAlignment="Center" Width="198" FontWeight="Bold" FontSize="36" BorderBrush="{x:Null}" />
</Grid>
</Window>
partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
这是Minimal,Complete和Verifiable示例的完整代码。此代码仍会在平板电脑上产生错误。谢谢你的帮助!
答案 0 :(得分:0)
我终于设法解决了这个问题!我没有创建和显示新窗口,而是创建了一个新的UserControl(TestWindow)并更改了mainWindow的内容。关闭TestWindow后,普通GUI作为MainWindow的Content加载,TestWindow设置为null。
这可能不是最佳解决方案,但它确实有效。
编辑1
几周后,我又一次偶然发现了同样的问题。我发现Erti-Chris Eelmaa写的solution最终解决了这个问题。我需要更改Application.ShutdownMode
,以便应用程序仅在显式调用Application.Shutdown();
时关闭。
Application.ShutdownMode = ShutdownMode.OnExplicitShutdown;
我希望这有帮助!