MVVM Light框架點滴(MVVM模式)

2021-09-06 03:26:25 字數 2509 閱讀 1989

mvvm light框架是針對wpf和silverlight開發提供的乙個mvvm模式的實現,以下簡要總結一下該框架。

[以下基於silverlights4]

目前的框架就兩個庫檔案

galasoft.mvvmlight庫

viewmodelbase:view model的基礎類,mvvm中vm實現

messenger:用於viewmodel和view之間傳遞的訊息,注意系統的galasoft.mvvmlight.messaging命名空間下已經預定義了一些常使用的訊息處理類,
如dialogmessage、notificationmessageaction、notificationmessagewithcallback等
command:命令,relaycommand,用語和介面元素的響應處理繫結

galasoft.mvvmlight.extras庫

eventtocommand:使用xaml 繫結command,expression4中使用

dispatcherhelper:處理多執行緒

galasoft.mvvmlight.binaries.v3.zip

解壓到系統的program files目錄

galasoft.mvvmlight.templates.v3.vs10.zip

將其中的itemtemplates projecttemplates下的silverlight內容拷貝到對應的

c:\users\administrator\documents\visual studio 2010\templates下

此時new project是可以選擇silverlight下的mvvm light模板,不過這個模板也是比較簡單的

一般專案使用方法:

1、 新增galasoft.mvvmlight.sl4.dll galasoft.mvvmlight.extras.sl4.dll引用

xmlns:vm="clr-namespace:mvvmlight1.viewmodel"

3、 新增viewmodel、view

此時:viewmodel中:加入command實現、傳送訊息和view 傳遞資訊

view中:       繫結資料、繫結command、註冊訊息、icleanup實現viewmodel清理註冊訊息清理、介面和互動設計
至於view和viewmodel之間的初始化關聯可以使用viewmodellocator.cs的形式建立和繫結【這個形式還是有些麻煩】,當然也可以使用mef等ioc實現自動注入
view中繫結viewmodel可以使用

訊息註冊

一般在view中註冊以響應viewmodel中訊息,當然也可以在別處註冊

messenger.default.register(this, onreadonlyissuemessage);
messenger.default.register

(this, true, m => backgroundbrush = m);

訊息傳送

messenger.default.send(new commandmessage(notifications.notifyshutdown));

command定義

public relaycommand shutdowncommand
public mainviewmodel()
else
}
removefilecommand = new relaycommand(g => this.onremovefilecommand(g), g => g != null);

command繫結

command=""
commandparameter=""

icleanup

mef

public partial class allissues : usercontrol, icleanup
}
[import(viewmodeltypes.allissuesviewmodel)]
public object viewmodel
}
public void cleanup()

dispatcherhelper

源**中galasoft.mvvmlight.test (sl).csproj
testdispatcherhelper.cs有詳細內容

viewmodel

從viewmodelbase繼承實現檢視對應的類


MVVM Light框架點滴(MVVM模式)

mvvm light框架是針對wpf和silverlight開發提供的乙個mvvm模式的實現,以下簡要總結一下該框架。以下基於silverlights4 目前的框架就兩個庫檔案 galasoft.mvvmlight庫 viewmodelbase view model的基礎類,mvvm中vm實現 me...

Vue 實現mvvm框架

observe obj 訂閱 key 資料 if this.binding key let binding this.binding key 重寫getter,setter object.defineproperty obj,key,set newval proxydata data,vm set ...

Android MVP和MVC和MVVM模式區別

1 mvp模式 比較常用的模式 1.資料模型 model 業務邏輯和實體模型,用來操作實際的資料,包含bean和model的抽象介面來降低耦合。2.檢視 view 就是android中的檢視,需要建立乙個view的抽象介面view inte ce。通過實現view的介面來實現view與present...