c# - Where to bind the ViewModel to the View? -


i work on project(6 pages) target on windows phone 7.5 , above. don't pure mvvm model, since handle navigation event , querystrings such pain, , decide learn 1 one, , finish project first :p

since new new mvvm, decide not use mvvm light, writes boilerplate codes myself. when finish coding each part, face problem.

where bind viewmodel efficient. read lot , conclude points below.

  1. create model instance in app.xaml.cs property, , make binding in codebehind of view.(this how official data bound app example does.) when facing lots of view (6 pages etc), may problem.

    public partial class app : application {     private static mainviewmodel viewmodel = null;      /// <summary>     /// static viewmodel used views bind against.     /// </summary>     /// <returns>the mainviewmodel object.</returns>     public static mainviewmodel viewmodel     {                 {             // delay creation of view model until necessary             if (viewmodel == null)                 viewmodel = new mainviewmodel();              return viewmodel;         }     } } 
  2. make new instance of viewmodel global variable in code behind of view. can use through out view. (as above, don't pure mvvm)

  3. in code behind, in constructor, bindings once.

    public mainpage() {     initializecomponent();      myviewmodel vm = new myviewmodel();     datacontext = vm; } 

i read mvvm light use viewmodellocator central station. avoid use mvvm light, bind viewmodel guys think best?

mvvm pattern , it's ok not use pure mvvm. (and in end comes down preference) think option 3 ideal.

this true in windows phone due view first approach. dislike idea of cluttering app code view models. view code behind think more natural location setup , bind viewmodel. speaking view model should 1 instance of view. wouldn't example want have detail pages different items sharing 1 detail view model show same data. while view model can used more 1 type of view typically wouldn't used multiple instances of views.

the benefit can see of putting in app code access view model instance anywhere. vm "a" needs tell vm "b" refresh. isn't common in mobile platform though i've needed more in drag , drop scenarios or child windows. news if need functionality still don't need make static view models can implement own or pull in messaging library mvvmlight.

i have 4 apps in wp marketplace , i've experimented view models in code behind suggest , mvvm light , caliburn micro. hope helps.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -