English 中文(简体)
MVP, Winforms——活动Handler或代表
原标题:MVP, Winforms - EventHandlers or Delegates

我在使用消极观点的几种WinForms应用中采用了MVP模式。 我实施了一个包含财产和代表的界面,其形式是“行动与建议”;T >and Func< T > 以具体观点将“倡议”活动进行联网,并召回来。

我即将启动一个新项目,对网上模式的研究很少,包括这里模式的许多例子,并指出所有使用活动者都通知发言者。

我没有真正理解为什么在这种情况下会发生事件,因为我认为只有一个发言者。

我的问题是,为了保持这种一致性,这是的。 框架网利用事件,或出于我看不到的其他一些原因?

这里是我使用的模式的三边例子:

public interface IViewAbstraction
{
    public ModelData ModelData { set; }
    public Action<ModelData> ModelDataChangedCallback { set; }
}

public class SomeWinForm : Form, IViewAbstraction
{
    private Action<ModelData> modelDataChanged;
    private ModelData model;

    public ModelData ModelData
    {
        set { /* when this property changes, update UI */ }
    }

    public Action<ModelData> ModelDataChangedCallback
    {
        set { modelDataChanged = value; }
    }

    private void OnSomeWinformsEvent(EventArgs args)
    {
        if (modelDataChanged == null) throw new Exception();

        modelDataChanged(model);
    }
}

public class Presenter
{
    private readonly IViewAbstraction view;
    private readonly IDataLayerAbstraction dataLayer;

    public Presenter(IViewAbstraction view, IDataLayerAbstraction dataLayer)
    {
        this.dataLayer = dataLayer;
        this.view = view;
        this.view.ModelDataChangedCallback = OnModelChanged;
        this.view.ModelData = dataLayer.GetData();
    }

    private void OnModelChanged(ModelData data)
    {
        // validate and save data.
    }
}
最佳回答

你的格局与使用事件基本相同,有重大差别。 事件没有暴露出基本代表(ModelDataChangedCallback,你就是例子)。 这种做法是坏的,因为其他法典可以清楚地列出例如援引清单。 一项活动将有一名基本代表,可以加入或从中删除,但从该类范围之外从未清除过。

我不理解你关于没有多个订户的观点——这是不使用事件的理由。 活动只是一个阶层说“He! 事情已经发生,1至1幅带有播客物体的地图完全合理和正常。

你们也看不出这种比较奇怪的只看书写财产。

问题回答

暂无回答




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签