2016-10-01 99 views
0

有人請爲我說明如何設置一個這樣的邏輯:兩個窗口之間的wpf事件

我有一個WPF控件。當按下按鈕時,它執行兩個可能的事情之一。答:它檢查是否已加載不同的WPF窗口。如果是,它會觸發該窗口的Print方法。 B.它檢查是否加載了不同的WPF窗口。如果不是,則實例化該窗口,然後觸發其Print方法。

我很難理解兩個WPF控件/ Windows之間的事件系統。對我來說這是一個相對較新的東西,所以如果有人通過這個來向我走,我會很感激。

Ps。這不是一項家庭作業,而是我的一項新愛好。如果它是一個完全noob問題,然後只是指向我的資源,所以我可以教育自己。

乾杯!

+0

你提的問題是過於寬泛。你需要提供一個很好的[mcve],清楚地說明你的場景。我會指出你似乎錯誤地看待問題。也就是說,打印命令應該屬於知道如何打印的視圖模型,並且您不必導航事件系統就可以在視圖模型上調用要打印的命令。 WPF中的用戶界面和事件路由用於與視圖進行交互,後者又與視圖模型進行交互。視圖本身不應該實現與用戶交互直接相關的功能。 –

+0

如何使用觀察者 - 訂戶設計模式爲您的情況。我認爲這將是一個很好的解決方案這 –

回答

1

首先,您將檢查打開的新Window是您需要的方式是什麼?

你可以通過比較其Handle或他們的Type(:窗口公共類MyWindowWithPrintMethod)做到這一點。

可以有多種方式來做到這一點。

我建議我簡單的方法,專注於WPF方式,以最簡單的方式解決您的目的。

MyWindowWithPrintMethod obj_MyWindowWithPrintMethod; 
    private void btnNewWindow_Click(object sender, RoutedEventArgs e) 
    { 
     obj_MyWindowWithPrintMethod = new MyWindowWithPrintMethod(); 
     obj_MyWindowWithPrintMethod.Show(); 
    } 

    private void btnCheckNewWindow_Click(object sender, RoutedEventArgs e) 
    { 
     WindowInteropHelper tgtWindow = new WindowInteropHelper(obj_MyWindowWithPrintMethod); 

     foreach (Window w in Application.Current.Windows) 
     { 
      // Compare Handle 
      WindowInteropHelper wih = new WindowInteropHelper(w); 
      if (wih.Handle == tgtWindow.Handle) 
      { 
      ((MyWindowWithPrintMethod)w).Print(); 
      } 

      // Compare Type 
      if (w.GetType() == typeof(MyWindowWithPrintMethod)) 
      { 
      ((MyWindowWithPrintMethod)w).Print(); 
      } 
     } 
    } 

MyWindowWithPrintMethod.cs

 public class MyWindowWithPrintMethod : Window 
    { 
     public void Print() 
     { 
      MessageBox.Show("Print invoked !"); 
     } 
    } 
0

answer從這個question約2個窗事件可能幫助:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     this.Loaded += new RoutedEventHandler(MainWindow_Loaded); 
    } 

    void MainWindow_Loaded(object sender, RoutedEventArgs e) 
    { 
     Child childWindow = new Child(); 
     childWindow.MyEvent += new EventHandler(childWindow_MyEvent); 

     childWindow.ShowDialog(); 
    } 

    void childWindow_MyEvent(object sender, EventArgs e) 
    { 
     // handle event 
     MessageBox.Show("Handle"); 
    } 
} 

子窗口

public partial class Child : Window 
{ 
    // define event 
    public event EventHandler MyEvent; 

    protected void OnMyEvent() 
    { 
     if (this.MyEvent != null) 
      this.MyEvent(this, EventArgs.Empty); 
    } 

    public Child() 
    { 
     InitializeComponent(); 

     this.Loaded += new RoutedEventHandler(Child_Loaded); 
    } 

    void Child_Loaded(object sender, RoutedEventArgs e) 
    { 
     // call event 
     this.OnMyEvent(); 
    } 
} 

上面的代碼顯示瞭如何從一個窗口中設置一個事件到另一個。但是,您可能只想在其他窗口中調用方法。對於example

public void AddNewUser() 
{ 
    Window2 window = new Window2(); 
    if (window.ShowDialog() == true) 
    { 
     // Update DataGrid 
     RefreshDataGrid(); 
    } 
} 

如果你下定決心堅持的事件,那麼你應該在WPF routed events閱讀起來。

+0

如何使用觀察者 - 訂戶設計模式爲您的情況。我認爲這將是一個很好的解決方案 –