2012-01-05 94 views
4

我见过这么多的样本,为了打开关闭的窗口,我应该在关闭事件中隐藏窗口,但这对我来说并不公平,如果我在工作中间关闭窗口并再次打开显示内容的窗口我离开的地方,因为我以前隐藏窗口。那么在关闭或隐藏窗口后,我怎么才能重新启动我的窗口。如何重新打开关闭的窗口?

当前我打电话给winload方法,它是在调用隐藏窗口的显示方法后显示新窗口。

private PurgeData data=new PurgeData(); 

private void MenuPurgeData_Click(object sender, RoutedEventArgs e) 
     { 

      try 
      { 
       if (PurgeData == null) 
       { 
        PurgeData = new PurgeData(); 
        PurgeData.Show(); 
       } 
       else 
       { 
        PurgeData.WinLoad(); 
        PurgeData.Show();      
       } 
      } 

谢谢, @nagaraju。

回答

1

这真的取决于你的应用程序的结构。由于您没有维护状态,因此只需要保存已关闭的实际窗口类型即可。根据您使用的窗口类型,您可以为其分配一个ID(例如,在其Tag属性中),以便稍后可以识别它。然后您可以在Closing事件期间推送此ID。然后,如果用户重新打开它,请弹出堆栈以获取ID并检查对应的窗口。然后您可以重新打开该窗口。

当然,Stack只是一种数据结构,它可能适用于您,也可能不适合您。使用堆栈意味着用户可以重新打开他们关闭的所有过去的窗口,但也许您可能只想要一个窗口。

编辑 - 基本代码:

//create an enum to make it easy to recognise the type of window that was open 
enum WindowType { Profile, Settings }; 

//create a stack to hold the list of past windows 
Stack<WindowType> pastWindows = new Stack<WindowType>(); 

//give the window type a particular id 
profileWindow.Tag = WindowType.Profile; 

//open the window 
profileWindow.Show(); 

//in the closing event, if you want the user to be able to reopen this window, push it to the stack 
protected override void OnClosing(CancelEventArgs e) 
{ 
     pastWindows.Push(WindowType.Profile); //or whatever type it was 
     base.OnClosing(e);  
} 

//to reopen the last window 
void ReopenLastWindow() 
{ 
    WindowType lastType = pastWindows.Pop(); 
    switch(lastType) 
    { 
     case WindowType.Profile: 
      profileWindow.Show(); 
      break; 
     case WindowType.Settings: 
      settingsWindow.Show(); 
      break; 
     default: 
      break; 
    } 
} 
+0

如果你不是我的,你能否提供示例代码片段。 – nag 2012-01-05 12:20:31

+0

我已添加代码。它完全没有经过测试,并直接输入到StackOverflow中,所以在某些属性/方法上可能存在编译器错误,但它应该给你一个总体思路。 – keyboardP 2012-01-05 12:30:50

+0

非常感谢。 – nag 2012-01-05 12:33:49

8

如果你想要隐藏/显示的行为,你应该不会在窗口上调用window.close()的所有,但通过调用Window.Hide隐藏它( )。如果用户已经关闭但不可避免地关闭,则可以尝试以下操作。在窗口内覆盖OnClosing并将e.Cancelled设置为true,然后调用.Hide()。这应该允许窗口隐藏/显示,即使用户关闭窗口。

// Disclaimer, untested! 
protected override void OnClosing(CancelEventArgs e)  
{   
    e.Cancel = true; // cancels the window close  
    this.Hide();  // Programmatically hides the window 
} 

编辑:

好吧,现在我读过你的问题正确;-) 因此,如何能我刚开始我的窗口关闭后或隐藏窗口。

当您使用上述重新显示窗口时,它当然会与以前隐藏的实例相同,因此将具有相同的数据和状态。如果你想要全新的内容,你需要创建一个新的Window()并调用Window.Show()。如果您像上面一样隐藏/显示,那么您将在隐藏之前将窗口恢复到完全相同的状态。

您是否在WPF应用程序中使用了MVVM模式?如果是这样,你可以尝试以下。通过将视图模型中的所有数据绑定到视图(即:窗口后面的代码中没有业务逻辑或数据),您可以调用ViewModel上的方法在显示窗口时重置所有数据。您的绑定将刷新并且窗口状态将被重置。请注意,只有在您正确遵循MVVM并将主窗体上的所有元素绑定到ViewModel属性(包括子控件)后,这才会起作用。

最好的问候,

+0

目前我打电话时调用Load方法打开隐藏窗口时调用window_load方法,然后它显示给我像新窗口。 – nag 2012-01-05 12:23:20

+0

@nag,这听起来不像是正确的做事方式。你不应该专门调用Window_Load。你应该从外部代码调用的唯一方法是new()(创建一个新实例),Close()(关闭并且不再重新打开),Show()(在新的或之前的Hide()隐藏()隐藏显示的窗口。最好的问候, – 2012-01-05 12:25:25

+0

是啊!为了显示新窗口,我调用一个显示窗口为新鲜的方法,这是在调用隐藏窗口的.show()方法之前调用的。这种方法是对还是错告诉我? – nag 2012-01-05 12:31:41