簡體   English   中英

如何提出一般事件?

[英]How to raise a generic event?

背景

問題標題可能有點誤導,但我不確定如何快速提出問題。 我正在構建一個winforms應用程序,並遵循MVP設計模式並具有被動視圖。 在我的應用程序的主視圖(窗體)中,有一個導航面板,其中包含按鈕,單擊這些按鈕將打開另一個視圖(窗體)。 我正在嘗試創建泛型按鈕:

// The type T represents the view (form) that should be opened when the button is clicked
NavigationButton<T>

主視圖的演示者在運行時分別創建每個按鈕:

// Code in Main Presenter - register each button
View.RegisterNavigationButton(new NavigationButton<IViewExample1>("Example 1")); // Pass in text to show on button
View.RegisterNavigationButton(new NavigationButton<IViewExample1>("Example 2"));

// Code in View
public void RegisterNavigationButton<T>(NavigationButton<T> button) where T : class, IView
{
    // Add button to flow layout panel
    _flpNavigation.Controls.Add(button);

    // Subscribe to click event
    button.Clicked += ButtonClicked<T>;
}

現在,如果這不是被動視圖,則單擊導航按鈕后,您可能會繼續直接從主視圖創建新視圖:

// This is the method subscribed to the click event as shown in the above code
private void ButtonClicked<T>(object sender, EventArgs e) where T : class, IView
{
    // The ApplicationController creates the new view (form) using an IoC container (Simple Injector)
    ApplicationController.ShowModelessForm<T>();
}

但是,這是一個被動視圖,因此它不應該創建新的視圖(窗體)...

我的每個視圖都實現一個接口,演示者通過該接口持有對其隨附視圖的引用。 視圖界面定義演示者可以訂閱的事件。 換句話說,只要接口中定義了方法,演示者就可以直接從視圖中調用方法,但是視圖必須引發事件以與演示者進行通信。

考慮到此設置,我將如何與演示者進行交流,以使其應該創建新視圖? 我的每個視圖都實現一個接口,演示者通過該接口持有對其隨附視圖的引用。 視圖界面定義演示者可以訂閱的事件。 給定我的通用設置,是否可以設置某種事件來與演示者進行交流以創建視圖?

** 編輯 **

我的障礙是我不知道如何定義要引發的事件以及如何引發事件。 假設我有兩個要打開的輔助視圖,這些視圖由以下視圖定義:IView1和IView2。 我是否必須在主視圖中定義兩個單獨的事件處理程序,每個輔助視圖一個? 然后,一旦單擊按鈕,如何引發適當的事件?

您決定需要什么事件處理程序。 查看按鈕的用途並引發適當的事件。 如果您有多個用途相同的按鈕,請讓它們引發相同的事件。 如果您有另一個與按鈕具有相同目的的動作,請再次引發相同的事件。

引發的事件與按鈕的用途相關,並且基本上不知道由於引發事件而可能發生的任何事情。

該事件可以是更多的文字“單擊詳細信息按鈕”,也可以是更抽象的“請求的詳細數據”。

關於如何引發事件的主題, https://msdn.microsoft.com/zh-cn/library/edzehd2t(v = vs.110).aspx有一個引發事件的相當標准的技術示例。 (下面改寫)

class Counter
{
    public event EventHandler DetailsButtonClicked;

    protected virtual void OnDetailsButtonClicked(EventArgs e)
    {
        if (DetailsButtonClicked != null)
        {
            DetailsButtonClicked(this, e);
        }
    }

    // provide remaining implementation for the class
}

在演示者中,訂閱事件並執行操作,例如打開新視圖。


有關引發事件時傳遞消息的更多信息

您可以在調用事件時使用通用EventHandler傳遞消息。 我只是鼓勵您評估您提供的代碼的可讀性和輕松重構的能力。

例如,避免發送表示程序含義的字符串消息。 而是發送一個枚舉或常量值。

public void TryIt()
{
    var z = new Counter();
    z.DetailsButtonClicked += Z_DetailsButtonClicked;

    z.OnDetailsButtonClicked("Greetings Earthlings");
}

private void Z_DetailsButtonClicked(object sender, CustomEventArgs e)
{
    Debug.Print(e.Message);
}

public class CustomEventArgs : EventArgs
{
    public CustomEventArgs(string message) { this.Message = message; }
    public string Message { get; set; }
}

class Counter
{
    public event EventHandler<CustomEventArgs> DetailsButtonClicked;

    public virtual void OnDetailsButtonClicked(string message)
    {
        if (DetailsButtonClicked != null)
        {
            DetailsButtonClicked(this, new CustomEventArgs(message));
        }
    }

    // provide remaining implementation for the class
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM