2013-04-24 53 views
25

在WPF窗口中,我怎麼知道它是否被打開?如何知道WPF窗口是否打開

我的目標是當時只打開1個窗口實例。

因此,在父窗口我的僞代碼爲:

if (this.m_myWindow != null) 
{ 
    if (this.m_myWindow.ISOPENED) return; 
} 

this.m_myWindow = new MyWindow(); 
this.m_myWindow.Show(); 

編輯:

我發現,解決我最初的問題的解決方案。 window.ShowDialog();

它阻止用戶打開任何其他窗口,就像模態彈出窗口一樣。 使用此命令,不需要檢查窗口是否已經打開。

+1

感謝您的更新! window.ShowDialog()正是我需要的! – 2018-01-30 16:51:57

回答

60

WPF沒有在Application類開放Windows的集合,你可以做一個輔助方法檢查窗口是否打開。

下面是一個例子,它將檢查某個TypeWindow或某個名稱的Window是否打開,或者兩者都有。

public static bool IsWindowOpen<T>(string name = "") where T : Window 
{ 
    return string.IsNullOrEmpty(name) 
     ? Application.Current.Windows.OfType<T>().Any() 
     : Application.Current.Windows.OfType<T>().Any(w => w.Name.Equals(name)); 
} 

用法:

if (Helpers.IsWindowOpen<Window>("MyWindowName")) 
{ 
    // MyWindowName is open 
} 

if (Helpers.IsWindowOpen<MyCustomWindowType>()) 
{ 
    // There is a MyCustomWindowType window open 
} 

if (Helpers.IsWindowOpen<MyCustomWindowType>("CustomWindowName")) 
{ 
    // There is a MyCustomWindowType window named CustomWindowName open 
} 
+0

這是一個很好的解決方案!謝謝。但是因爲我找到了解決我的問題的另一種方法,所以我不會使用它。 – guilhermecgs 2013-04-25 12:46:46

+0

@guilhermecgs你找到解決問題的更簡單的方法嗎? – estebro 2014-07-31 15:19:46

+1

@estebro - 我使用了一個模式彈出。我的要求是避免用戶與其他打開的窗口進行交互。所以,一個模式彈出按照定義來做。 http://stackoverflow.com/questions/499294/how-do-make-modal-dialog-in-wpf – guilhermecgs 2014-07-31 17:52:21

8

您可以檢查是否m_myWindow==null,然後才創建並顯示窗口。當窗口關閉時,將變量設置回null。

if (this.m_myWindow == null) 
    { 
      this.m_myWindow = new MyWindow(); 
      this.m_myWindow.Closed += (sender, args) => this.m_myWindow = null;   
      this.m_myWindow.Show(); 
    } 
+0

@ofstream的類似解決方案 – guilhermecgs 2013-04-25 12:48:41

+0

只是出於興趣,這段代碼中是否存在內存泄漏,因爲我們從不刪除關閉的事件處理程序或將對象設置爲null刪除所有事件處理程序? – user1 2015-04-07 11:39:15

+0

@ user1我知道這是舊的,但是;是的,它確實會造成內存泄漏。我目前正試圖找到一種方法來解決托盤應用程序中的內存泄漏問題,該應用程序會生成不同的窗口並在變量上同步它們。 – JoshHetland 2015-04-14 03:48:50

1

把一個靜態布爾在你的班級,名爲_open或類似的東西。 在構造函數中,然後做到這一點:

if (_open) 
{ 
    throw new InvalidOperationException("Window already open"); 
} 
_open = true; 

,並在關閉事件:

_open = false; 
+3

它會工作,但我認爲這不是一個「乾淨」的解決方案。 – guilhermecgs 2013-04-25 12:47:24

0

是你搜索?

if (this.m_myWindow != null) 
{ 
    if (this.m_myWindow.IsActive) return; 
} 

this.m_myWindow = new MyWindow(); 
this.m_myWindow.Show(); 

如果你想有一個單身,你應該閱讀:How can we create a Singleton Instance for a Window?

+1

嗨,IsActive屬性不起作用。我認爲它只檢查窗口是否關注它(我不確定) – guilhermecgs 2013-04-25 12:41:42

2

如果你需要的,如果發現激活窗口,可以使用下面的代碼:

//activate a window found 
//T = Window 

Window wnd = Application.Current.Windows.OfType<T>().Where(w => w.Name.Equals(nome)).FirstOrDefault(); 
wnd.Activate(); 
0
void pencereac<T> (int Ops) where T : Window , new() 
    { 
     if (!Application.Current.Windows.OfType<T>().Any()) // Check is Not Open, Open it. 
     { 
      var wind = new T(); 
      switch (Ops) 
      { 
       case 1: 
        wind.ShowDialog(); 
        break; 
       case 0: 
        wind.Show(); 
        break; 
      } 
     } 
     else Application.Current.Windows.OfType<T>().First().Activate(); // Is Open Activate it. 
    } 

Kullanımı:

void Button_1_Click(object sender, RoutedEventArgs e) 
{ 
    pencereac<YourWindow>(1); 
} 
0

這是另一種使用LINQ實現此功能的方法。

using System.Linq; 

... 

public static bool IsOpen(this Window window) 
{ 
    return Application.Current.Windows.Cast<Window>().Any(x => x == window); 
} 

用法:

bool isOpen = myWindow.IsOpen(); 
相關問題