Getting Started with Mvvmlight Framework (iv)

Source: Internet
Author: User

This article focuses on ViewModelBase, demonstrates The use of the Set and raisePropertyChanged methods, and discusses the release of resources for the Cleanup method.

icleanup

interface. Implementing the ViewModel of this interface requires releasing resources in the cleanup method, especially the-= event

observableobject

The class implements the INotifyPropertyChanged interface, which defines a notification-based object base class for ViewModelBase to inherit

viewmodelbase

Inherit from observableobject,   Icleanup. The base class for the ViewModel that will be used under the Mvvmlight framework. Set and raisepropertychanged are mainly provided for external use. Also in the cleanup method, all Mvvmlight Messenger (defined in GalaSoft.MvvmLight.Messaging namespace) of the instance is unregister

above is the form given in the first article, viewmodelbase mvvmlight all You viewmodelmvvmlight rela Ycommand

Let's take a look at the use of the most basic Set and raisepropertychanged methods :

        Private stringtitle;  Public stringTitle {Get{returntitle;} Set{Set (reftitle, value); }        }        Private stringtext;  Public stringText {Get{returntext;} Set{text=value; raisePropertyChanged ("Text"); raisePropertyChanged ("Titleandtext"); }        }         Public stringTitleandtext {Get            {                returnTitle +text; }        }

The Set method automatically notifies you when a property is assigned a value raisepropertychanged . Of course you can also call the raisepropertychanged method manually.

The source code for MVVM light follows the reusable logic extraction package, reducing the amount of work we do when we move bricks:

        protected BOOLSet<t>(            reft field, T newvalue=default(T),BOOLBroadcast =false, [Callermembername]stringPropertyName =NULL)        {            if(equalitycomparer<t>. Default.equals (field, NewValue)) {return false; }#if! Portableraisepropertychanging (PropertyName);#endif            varOldValue =field; Field=NewValue; raisePropertyChanged (PropertyName, OldValue, field, broadcast);return true; }

Cleanuppage onnavig Atedfrom-= event,unregister off mvvmlight messenger

Base is called in the subclass ViewModel that inherits ViewModelBase . Cleanup () Automatically releases the current ViewModel registered Messenger

the Cleanup method in ViewModelBase:

         Public Virtual void Cleanup ()        {            Messengerinstance.unregister (this);        }

  So the general ViewModel Onnavigatedfrom method looks like this:

         Public void Onnavigatedfrom (object  obj)        {            base. Cleanup ();             this. Xxxxevent-= xxxxhandler;        }

  What what, you say ViewModel There is no onnavigatedfrom method ? Not really, but we have implemented the in Avigable interface for ViewModel that need to handle the navigation event :

     Public Interface inavigable    {        void onnavigatedfrom (object  obj);         void Onnavigatedto (object  obj);    }

  Then,override needs to handle the corresponding method of the Page of the navigation event , call the navigatedfrom in ViewModel, Navigatedto method, pass the parameters, put the processing logic into the ViewModel :

     Public Sealed Partial classMainpage:page { PublicMainPage () { This.        InitializeComponent (); }        protected Override voidonnavigatedto (NavigationEventArgs e) {Base.            Onnavigatedto (e); varnavigable = DataContext asinavigable; Navigable.        Onnavigatedto (E.parameter); }        protected Override voidOnnavigatedfrom (NavigationEventArgs e) {Base.            Onnavigatedfrom (e); varnavigable = DataContext asinavigable; Navigable.        Onnavigatedfrom (E.parameter); }    }

See here, is not the release of resources or something is also a very simple thing? But the year of the Sao! too young Too simple, sometimes naive! Is that just enough? We need to go back to cleanupviewmodelonnavigatedfromviewmodel there is no , or what to do if you don't need a navigation event. For example, a mainpage corresponds to mainviewmodelcontactviewmodel

     Public class Mainviewmodel:viewmodelbase, Inavigable    {        ObservableCollectiongetset;}

  The contactViewModel is just the encapsulation of the contact data Object, and there is no navigation event. At this time, if you do not need contactlist resident Memory, the method of MAinviewmodel onnavigatedfrom will grow like this:

 public  void  Onnavigatedfrom (object   base  
    
     .            Cleanup ();            
     this . Xxxxevent-= Xxxxhandler;  foreach  (var  contact in   ContactList) {contact.            Cleanup ();        } contactlist.clear (); }

No mistake oh, still inherit viewmodelbase 's contactvIewmodel to release the internal resources, but the call to clean up is externally referenced ContactList 's Mainviewmodel to launch.

This article discusses the inheritance use of ViewModelBase, and introduces a little bit of experience I use, including how to use navigation events and release resources. Also hope to give new inspiration, the old drivers pat.

In addition , the Mvvmlight framework may be paused (no one is looking at it anyway ...). Because I'm going to start doing Win10 's Universal App , digging a click!

Getting Started with Mvvmlight Framework (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.