如何(正确地)更新WPF应用程序的MVVM并购? [英] How to (correctly) update the M in MVVM of WPF application?

查看:119
本文介绍了如何(正确地)更新WPF应用程序的MVVM并购?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经通过refractoring MVVM为WPF应用程序使用了一系列爱德华·坦圭的问题,它可以是在他的脂肪模型,骨感的ViewModels哑意见,最好的办法MVVM?中,我有点糊涂了他的
中的大智能的ViewModels,哑视图和任何型号,最好的办法MVVM?

Having passed a series of Edward Tanguay's questions refractoring the usage of MVVM for WPF app which can be found in Linked sidebar of his Fat Models, skinny ViewModels and dumb Views, the best MVVM approach?, I am a little confused by his final WPF application in Big smart ViewModels, dumb Views, and any model, the best MVVM approach?

M(模型)是Customer类:

//model
public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime TimeOfMostRecentActivity { get; set; }

    public static Customer GetCurrentCustomer()
    {
        return new Customer 
                   { FirstName = "Jim"
                     , LastName = "Smith"
                     , TimeOfMostRecentActivity = DateTime.Now 
                   };
    }

}



返回当前用户。类,beause返回新创建的当前用户的副本...

which returns current user. Kind of, beause it returns duplicates of newly created "current" user...

但是,在存储和在需要的情况下更新M的数据?

But where is the M's data stored and updated in case of need?

假设,我想改变模型的当前用户的来根纳季?

Suppose, I want to change the model's current user's FirstName to "Gennady"?

我增加了一个按钮,通过此按钮单击事件处理程序更新模型:

I added a button for updating the model with this button click event handler:

private void button1_Click(object sender, RoutedEventArgs e)
{


} 

旨在改变其从模型的数据将在GUI中反映出来。

aiming to change the model's data from it which will be reflected in GUI.

我怎么能做到这一点,通过点击此按钮...对不起,通过将代码插入到此的button1_Click()

How can I do this, by clicking this button... sorry, by placing the code into this button1_Click()?

或者是有毛病我想办呢?结果
即可。如何正确地更新MVVM /将M?

Or it is something wrong with my wish to do it?
Then. how to correctly update/change M in MVVM ?

更新:结果
所有的答案似乎是指,我不应该做并购,但是,虚拟机的变化结果
虽然我专门问了一下的与引用的MV-VM实现

public CustomerViewModel()
{
    _timer = new Timer(CheckForChangesInModel, null, 0, 1000);
} 
private void CheckForChangesInModel(object state)
{
    Customer currentCustomer = CustomerViewModel.GetCurrentCustomer();
    MapFieldsFromModeltoViewModel(currentCustomer, this);
} 
public static void MapFieldsFromModeltoViewModel
     (Customer model, CustomerViewModel viewModel) 
{
    viewModel.FirstName = model.FirstName;
    viewModel.LastName = model.LastName;
    viewModel.TimeOfMostRecentActivity = model.TimeOfMostRecentActivity;
}



因此,例如,在执行该代码的from阿道夫·佩雷斯的回答变化,文本框的内容是从吉姆到根纳季变化只为 _timer =一个周期间隔一套新的定时器(CheckForChangesInModel,NULL,0,1000);

So, for example, upon implementing the code from Adolfo Perez's answer changes, the TextBox's content is changed from "Jim" to "Gennady" only for a period of interval set in _timer = new Timer(CheckForChangesInModel, null, 0, 1000);.

由我引用MV-VM在WPF的做法是这样的,它的M应更新,以便VM已经赶上了这些变化的,但不是虚拟机。

All logic of referenced by me M-V-VM in WPF approach is such that it is "M" should be updated, in order VM has caught up those changes, but not the "VM".

甚至,我不明白,如果在其中进行更改的 VM 他们怎么能反映在 M 如果在 VM 知道的 M ,但 - 不是相反 - 型号不知道视图模型)。

Even more, I cannot understand, if to make changes in VM how can they be reflected in M if the VM knows about M but - not vice versa - Model does not know about ViewModel).

推荐答案

在MVVM你应该避免代码隐藏。原因是,你想测试的类来结束,在这种情况下您的 VM 的是完全独立于您的 V 。你可以不涉及 V 运行在VM上的一套单元测试。你也可以勾不同类型的浏览,而不会影响你的业务逻辑。

In MVVM you should avoid code-behind. The reason is that you want to end up with testable classes, in this case your VM's that are completely independent from your V. You could run a set of unit tests on your VM without involving the V. You could also hook different types of Views without affecting your business logic.

您按钮将其命令属性绑定到一个的ICommand 属性暴露您的 VM 。此命令在 VM 将处理您的点击方法事件您指定。

Your button will bind its Command property to an ICommand property exposed in your VM. This Command in your VM will handle your click event in the method you specify.

在您查看:

<Button Content="Change FirstName" 
        Command="{Binding Path=ChangeFirstNameCommand"}/>

在您的视图模型:

//Define your command
public ICommand ChangeFirstNameCommand {get;set;}

//Initialize your command in Constructor perhaps
ChangeFirstNameCommand = new RelayCommand(OnChangeFirstName,CanChangeFirstName);

private void OnChangeFirstName()
{ 
    //Your FirstName TextBox in your V will be updated after you click the Button
    this.FirstName = "Gennady";
}

private bool CanChangeFirstName()
{
  //Add any validation to set whether your button is enabled or not. 
  // WPF internals take care of this.
  return true;
}



重要的是要记住,在此模式中您的是非常重要的V 知道您的 VM 和您的 VM 知道您的 M 而不是相反。

It is very important to keep in mind that in this pattern your V knows about your VM and your VM knows about your M but not the other way around.

在您的例子,如果你想改变你的模型名字属性,你woud必须做到以下几点:

In your example if you want to change your Model FirstName property you woud have to do the following:


  • 创建一个 VM 它实现了 INotifyPropertyChanged的

  • 暴露你的 M 姓属性在 VM 通知更改

  • 在您的XAML视图中创建一个文本框,并将其Text属性绑定到您的 VM .FirstName 。设置绑定模式=双向

  • Create a VM which implements INotifyPropertyChanged
  • Expose your M FirstName property in your VM notifying changes
  • Create a TextBox in your XAML View and bind its Text property to your VM.FirstName setting Binding Mode=TwoWay.

<TextBox Text=
     "{Binding Path=FirstName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>


当您在文本框中键入您的名字会直接填充在VM-M。此外,由于双向绑定,如果您修改您的名字财产VM 后,这种变化会自动在您的 V

As you type in the TextBox your FirstName will be directly populated in the VM-M. Also, thanks to the Two way binding, if you modify your FirstName property in your VM, that change will be reflected automatically in your V


  • 设置您的View.DataContext到您的 VM 。这是设置为你的所有数据绑定的背景下,除非您指定一个不同的结合来源。

  • 如果你想坚持一个DB的变化,然后在 VM 将于CRUD操作的护理

  • 注入一个服务类
  • Set your View.DataContext to your VM. This is what sets the Context for all your data bindings, unless you specify a different binding source.
  • If you want to persist changes in a DB then inject a service class in your VM which will take care of CRUD operations

看看这个简单的例子:

http://www.codeproject.com/Articles/126249/MVVM-Pattern-in -WPF-A-简单教程换绝对

这篇关于如何(正确地)更新WPF应用程序的MVVM并购?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆