2012-12-29 129 views
0

当我点击一个按钮打开窗口时,父窗口冻结。如果我再次最小化和最大化父窗口,它会再次正常工作。wpf - 窗口冻结

系统配置

  • Windows 7中,64位操作系统。
  • .NET Framework 3.5的SP1
  • Visual Studio速成2008

以下是简单的应用程序,我可以重现这个问题。

Window1.xaml

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <StackPanel> 
    <TextBox Width="200"/> 
    <Button Content="click" Click="Button_Click"/> 
    </StackPanel> 
</Window> 

Window1.xaml.cs

namespace WpfApplication1 { 
/// <summary> 
/// Interaction logic for Window1.xaml 
/// </summary> 
public partial class Window1 : Window { 
    public Window1() { 
     InitializeComponent(); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) { 
     (new Window2()).ShowDialog(); 
    } 
} 
} 

Window2.xaml

<Window x:Class="WpfApplication1.Window2" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window2" Height="300" Width="300" WindowStyle="ToolWindow"> 
<Grid> 

</Grid> 
</Window> 

它正常工作,如果我删除WindowStyle = “工具窗口”! !!!!!

的App.xaml

<Application x:Class="WpfApplication1.App" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
StartupUri="Window1.xaml"> 
<Application.Resources> 

</Application.Resources> 
</Application> 

回答

1

ShowDialog()呼叫阻止您的GUI线程。

请参阅this question了解不妨碍主线程的替代方法。

1

使用Show代替ShowDialog这在模态模式

1

如果使用的ShowDialog(打开一个窗口)尽量设置窗口1作为所有者的窗口2:

 Window2 w = new Window2 { Owner = this }; 
     w.ShowDialog();