2012-07-28 255 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

相關問題