MVVM:IView 与数据绑定

发布于 2024-11-08 14:01:54 字数 1573 浏览 0 评论 0原文

我正在阅读有关 MVVM 的内容,以便在我的表示层视图中采用它。理想情况下,我希望对 WinForms、ASP.NET 和 SL 使用相同的方法。

我遇到了两种不同的方法,我想收集这些(也可能是其他)中的意见:

“带接口的视图”以及视图数据绑定到 ViewModel

“带接口的视图”

在这种方法中,我们有一个接口 IView包含: - 设置/获取典型字段值的属性 - 视图上发生的操作事件

其工作方式是通过将 IView 的具体实现注入到 ViewModel 中。然后 ViewModel 连接

事件。然后,它还通过属性“推送”和拉取字段值。它还通过

事件了解视图上发生的情况。可以通过 IView 属性激活和停用控件。

视图的具体实现在 WinForms 中很容易,不确定其他技术和 SL 中的“可混合性”。

public interface IMyView
{
  event EventHandler SomeActionClicked;

  Boolean CanEditField1 { get;set; }
  string Field1 { get;set; }
}

public class MyConcreteView: Form,IMyView
{
  public event EventHandler SomeActionClicked;

  public Boolean CanEditField1
  {
   get { return edtField1.Enabled; }
   set { edtField1.Enabled = value; }
  }
  public string Field1
  {
   get { return edtField1.Text; }
   set { edtField1.Text = value; }
  }

  private void btnAction_Click(object sender,EventArgs e)
  {
    SomeActionClicked(sender,e);
  }
}


public class ViewModel
{
  public ViewModel(IMyView view)
  {
    this.view = view;
    view.SomeActionClicked += SomeActionHandler;
  }

  private void SomeActionHandler(object sender,EventArgs e)
  {
    view.CanEditField = !view.CanEditField;  // Or whatever 'state' the ViewModel or Model is
    view.Field1 = DateTime.Now.ToString(...);
  }

  private IMyView view;
}

数据绑定视图

另一种方法是 ViewModel,它具有多个反映模型的属性(数据/字段值、UI 控件状态等)。

然后,视图使用数据绑定在视图上的 UI 控件中“呈现”字段值(模型) ViewModel 还

通过 ViewModel 中所

连接

的方法将数据绑定到的属性来控制 UI 控件状态。

I am reading about MVVM in order to adopt this across my presentation layers' views. Ideally I would like to use the same approach for WinForms, ASP.NET and SL.

I came across 2 distinct approaches and I would like to gather opinions in these (and possibly others):

The 'View with interface' and where the View is databound to the ViewModel

'View with interface'

In this approach we have an interface IView that contains:
- Set/Get properties for the typical field values
- Events for actions that happen on the View

The way this works is by a concrete implementation of the IView being injected into the ViewModel. The ViewModel then wires up the

events. It also then 'pushes' and pulls field values via the properties. It is also aware of what happens on the View by means of

the events. Controls can be activated and deactivated through the IView properties.

Concrete implementations of the view is easy in WinForms, not sure about the other technologies and the 'Blendability' in SL.

public interface IMyView
{
  event EventHandler SomeActionClicked;

  Boolean CanEditField1 { get;set; }
  string Field1 { get;set; }
}

public class MyConcreteView: Form,IMyView
{
  public event EventHandler SomeActionClicked;

  public Boolean CanEditField1
  {
   get { return edtField1.Enabled; }
   set { edtField1.Enabled = value; }
  }
  public string Field1
  {
   get { return edtField1.Text; }
   set { edtField1.Text = value; }
  }

  private void btnAction_Click(object sender,EventArgs e)
  {
    SomeActionClicked(sender,e);
  }
}


public class ViewModel
{
  public ViewModel(IMyView view)
  {
    this.view = view;
    view.SomeActionClicked += SomeActionHandler;
  }

  private void SomeActionHandler(object sender,EventArgs e)
  {
    view.CanEditField = !view.CanEditField;  // Or whatever 'state' the ViewModel or Model is
    view.Field1 = DateTime.Now.ToString(...);
  }

  private IMyView view;
}

Databound View

The other approach is a ViewModel that has several properties that reflect the Model (data/field values, UI control states, etc.

The View then uses databinding to 'present' the field values (Model) in the UI controls on the View. The ViewModel also controls

UI control states through properties being databound to. Actions in the View is fed to the ViewModel through methods in the

ViewModel that are hooked to.

Any (non-apparent) pros & cons to each of these methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥繁 2024-11-15 14:01:54

也许您实际上只需要视图作为 winforms 中属性的“呈现者”,而在 SL 中,所有接线都是通过 xaml 完成的,这将需要更少的样板。
因此,将视图模型保留在通用代码库中,而视图特定于 winforms。
对于 ASP.NET,这些视图模型事件连接起来并不那么方便,除非您使用丑陋的老式 Web 表单控件和回发。

我并没有真正看出这两种方法之间的区别,两者都适用于 SL。

保持简单。

Maybe you would actually only need the view as a "presenter" of the properties in winforms, while in SL all the wiring up is done trough the xaml, that would require less boiler plate.
So keep your view models in a common code base, while the view is specific to winforms.
For ASP.NET, those viewmodel events are not so sweet to hook up, unless you go for ugly oldschool webforms controls and postbacks.

I dont really see the difference between the two approaches, both would work with SL.

Keep it simple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文