Tags » Mvvm

Data Binding Custom Controls in Forms

Xamarin.Forms is a new way to build mobile applications. Using the ideas of generalization and translation, it allows developers to build a single UI definition which is then translated into native controls for a platform by the rendering engine. 2,034 more words

Technical

My WPF/MVVM "must have" part 3/3 - On view model unit tests organization

One of the great advantage of the MVVM pattern is that it allows you to test the Graphic User Interface logic of the application through the view models. 1,641 more words

Programming

MVVM vs MVC vc MVP

Similarities

  • All three architectures are designed to separate view from model. Model handles domain and business logic, view handles presentation
  • Controller/Presenter/ViewModel handle communication with Model and View…
  • 148 more words
Vs.

MVVM and WPF: blink an image from View Model

MVVM pattern design is very powerful, but it is also very complex. This is the main feature of this approach, where UI and business logic are splitted: you will love this feature, but sometimes, you will hate it, because it makes harder all the simply UI operation (also the message box… you can use them, but if you want a 100% MVVM application, you can’t). 569 more words

WPF

AngularJS 1.4 - Declaring a Controller

I’ve been following a few tutorials that describe declaring a MainController. However the tutorials a pre AngularJS 1.3x and are no longer valid. Previous AngularJS allowed you to define a controller globally . 113 more words

Digital

MVVMBlocks .NET MVVM Framework

I have decided to release my MVVM framework for Windows Store Applications. MVVMBlocks is a very easy to use framework for implementing the MVVM pattern in your applications. 175 more words

C#

Android开发中的MVVM模式实践

MVC, MVP and MVM

一个典型的MVC模式如下图所示:

MVC模式在实践中其实并不适合大规模Android应用的开发,因为Android从的设计上来说,activity/fragment更像是View层的组件和controller的结合,因为单纯的XML布局不能完成非常复杂的交互逻辑。工作项目由于一开始(2012年左右)的设计是MVC的,activity承担了view和controller的角色,工作中一个Activity耦合了展现层的逻辑和controller的逻辑后达到了4K+的行数,一个非常恐怖的数字。所以必须要对之进行解耦重构,其中MVP和MVVM引起了我们的注意,MVP模式见下图:

MVP模式是从MVC派生出来的,但是它阻止了model和view的交流,使业务逻辑与展示解耦,MVP模式中,activity承担的是View的角色,Presenter和VIew互相依赖(为了解耦有的时候会使用接口和抽象类,这个其实有点MVVM的意思了),Presenter是View和Model的一个桥梁。Presenter和View是一一对应的,是View的一种抽象。MVP模式在解耦的层面做的不如MVVM彻底。关于MVP模式的讨论可以参见这篇文章.

MVVM模式是在MVP模式上进一步解耦而派生出来的,见下图:

MVVM和MVP模式的区别在于View对于ViewModel来说是透明的,ViewModel对于Model来说是透明的,ViewModel持有View的回调,Model持有ViewModel的回调,这样就实现View,ViewModel,Model的解耦。

更多关于MVC, MVP, MVVM模式的讨论可以看一下这篇文章.

Demo

以一个点击button发送网络请求,并对结果进行处理的demo为例。
Activity的代码如下:

public class MVVMDemoActivity extends ActionBarActivity
    implements IViewModelCallback {

    MVVMDemoViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvvmdemo);

        viewModel = new MVVMDemoViewModel(this);
        findViewById(R.id.sendrequest).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.action();
            }
        });
    }

    @Override
    public void onDataReceive() {
        //数据回来后进行视图的更新
    }

    @Override
    public void onError() {
        //数据请求失败的时候做一些处理
    }
}
… 90 more words
解耦