2012-07-28 256 views

回答

5

你最好的选择是创建在窗口B中的属性,您将创建的窗口传递给。像这样的东西。我有一个名为MainWindow的窗口和另一个名为Window2的窗口。

主窗口

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     Window2 secondForm; 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      secondForm = new Window2(); 
      secondForm.setCreatingForm =this; 
      secondForm.Show(); 
     } 
    } 
} 

窗口2

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for Window2.xaml 
    /// </summary> 
    public partial class Window2 : Window 
    { 
     Window creatingForm; 

     public Window2() 
     { 
      InitializeComponent(); 
     } 

     public Window setCreatingForm 
     { 
      get { return creatingForm; } 
      set { creatingForm = value; } 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      if (creatingForm != null) 
       creatingForm.Close(); 

     } 

    } 
} 

在respose您的意见,关闭,是由另一种形式创建的窗口是调用Close容易创建形式的方法:

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     Window2 secondForm; 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      if (secondForm == null) 
      { 
       secondForm = new Window2(); 
       secondForm.Show(); 
      } 
      else 
       secondForm.Activate(); 
     } 

     private void button2_Click(object sender, RoutedEventArgs e) 
     { 
      if (secondForm != null) 
      { 
       secondForm.Close(); 
       secondForm = new Window2(); 
       //How ever you are passing information to the secondWindow 
       secondForm.Show(); 
      } 

     } 
    } 
} 

+0

: - Good Work。其实我想从MainWindow关闭Window2。提前致谢。 – 2012-07-28 05:34:36

+0

@AnoopMohan通过创建表单的表单来封闭表单要容易得多,您只需保留对创建表单的引用并在其上调用Close方法即可。 – 2012-07-28 05:47:59

+0

:谢谢,先生。 。现在我可以关闭窗口。但我需要做更多的事情..我想关闭窗口并在同一事件中打开具有不同值的同一个窗口。对不起,打扰您。 。提前致谢。 。 – 2012-07-28 05:59:52

1

它是非常简单使人公共类和方法,这样

class Helper 
{ 
public static void CloseWindow(Window x) 
    { 
     Assembly currentAssembly = Assembly.GetExecutingAssembly(); 
     // int count = Application.Current.Windows; 
     foreach (Window w in Application.Current.Windows) 
     { 
      //Form f = Application.OpenForms[i]; 
      if (w.GetType().Assembly == currentAssembly && w==x) 
      { 
       w.Close(); 
      } 
     } 
    } 
} 

现在请从您希望这样的关闭窗口此功能。

Helper.CloseWindow(win);//win is object of window which you want to close. 

希望这有助于。

2

这是从任何其他窗口关闭任何窗口的方法。你可以通过给你的窗口一些独特的标识符来修改它,以便在多个实例中工作,然后在foreach循环中搜索。

public static class Helper 
{ 
    public static void CloseWindowOfWhichThereIsOnlyOne<T>() 
    { 
     Assembly currentAssembly = Assembly.GetExecutingAssembly(); 
     foreach (Window w in Application.Current.Windows) 
     { 
      if (w.GetType().Assembly == currentAssembly && w is T) 
      { 
       w.Close(); 
       break; 
      } 
     } 
    } 
} 

或者具有唯一标识符“忽悠”:

public static void CloseWIndowUsingIdentifier(string windowTag) 
    { 
     Assembly currentAssembly = Assembly.GetExecutingAssembly(); 
     foreach (Window w in Application.Current.Windows) 
     { 
      if (w.GetType().Assembly == currentAssembly && w.Tag.Equals(windowTag)) 
      { 
       w.Close(); 
       break; 
      } 
     } 
    } 

我喜欢这个比建议的解决方案更好,因为你不需要乱用你的窗户,除了给他们独特的标签。我只用这个小项目,没有风险,因为我不会失去10-12个窗口的踪迹!

的其他建议的解决方案是一个有点傻(我没有50因缘就此作出评论),你可以只调用win.close()如果你已经有对象的引用......

0
 foreach (Window w in Application.Current.Windows) 
     { 
      if (w.Name != "Main_Window_wind") 
      { 
       w.Visibility = System.Windows.Visibility.Hidden; 
      } 
     } 
//name is the x:Name="Main_Window_wind" in xaml 

您现在可以关闭为隐伏所有窗口,但不关闭命名Main_Window_wind,你可以添加另一个窗口中与这个封闭若:!w.Name =“Main_Window_wind” & & w.Name =“AnyOther_Window_wind” & & ...

更快的方法是这样的:

 for (int intCounter = App.Current.Windows.Count - 1; intCounter > -1; intCounter--) 
     { 

      if (App.Current.Windows[intCounter].Name != "Main_Window_wind") 
       App.Current.Windows[intCounter].Visibility = System.Windows.Visibility.Hidden; 
     } 
+2

请在解决问题的原因和方式上添加一些关于解决方案的意见 – 2015-05-11 12:23:43

相关问题