Directory
- Icleanup and idisposable
- Attribute Change Handling: propertychanged and broadcast
- Judge a member
Returned directory
Icleanup and idisposable
Viewmodelbase in mvvm light simultaneously executes the icleanup interface and the idisposable interface. The former is from the mvvm Light Class Library itself, and the latter is the product of. net. The two are designed to accomplish the same type of events, and idisposable dispose is not recommended (marked with the obsolete feature). Of course, you can still rewrite the dispose method.
Why does icleanup still execute idisposable? Mr. Laurent Bugnion once explained this problem in stackoverflow. Address:
Http://stackoverflow.com/questions/2963151/cleanup-vs-disposebool-in-mvvm-light
Simply put, only idisposable is initially available,. the purpose of idisposable in net usually indicates that it can be recycled after execution. However, the resource clearing logic in viewmodel does not necessarily mean that viewmodel is waiting for GC destruction immediately, it may only be data storage, closing file streams, and other operations. Therefore, icleanup was born.
Viewmodelbase to execute icleanup is to directly define a public virtual cleanup method. Cancel any messages registered with the current object on the default messenger. Such as source code:
Messenger. Default. unregister (this );
The dispose method is the Execution Mode of the dispose method in. net. The public dispose method calls the protected dispose method and the disposing parameter is true.
Returned directory
Attribute Change Handling: propertychanged and broadcast
Viewmodelbase executes inotifypropertychanged at the same time, so it defines the propertychanged event. Define the raisepropertychanged (onpropertychanged) method to execute the event. The parameter is the attribute name. Raisepropertychanged encapsulates the property name in propertychangedeventargs (in system. componentmodel) and then executes the propertychanged event. This. Net event definition routine does not need to be explained.
Viewmodelbase also defines another raisepropertychanged method. The parameters of this method are:
(String propertyname, t oldvalue, t newvalue, bool broadcast)
If broadcast is false, the execution of the previous raisepropertychanged is the same. If broadcast is true, this attribute changes the event.Not onlyThe normal raisepropertychanged method will be called, and another broadcast method will be called to spread the event (or message) of this attribute change through the message mode unique to mvvm light.
The broadcast method first encapsulates the information in the propertychangedmessage <t> type. This type is inherited from propertychangedmessagebase. A non-generic execution belongs to the basic message type in messagebase: mvvm light.
The broadcast method then checks whether the current viewmodelbase has been bound to an imessenger. A constructor of viewmodelbase passes in a custom imessenger type. If not bound, use the default Messenger (via the messenger. Default attribute ). Finally, use the send method of imessenger to send messages.
In addition, viewmodelbase defines the messengerinstance attribute (the inheritance class is visible and can be rewritten, And the type is imessenger). You can modify the default imessenger In the bound viewmodelbase.
You can select the raisepropertychanged method as needed when defining your own property members. When mvvminpc code snippet of mvvm light is used, the following complete code is directly generated:
/// <Summary>
/// The <see CREF = "myproperty"/> property's name.
/// </Summary>
Public const string mypropertypropertyname = "myproperty ";
Private bool _ myproperty = false;
/// <Summary>
/// Gets the myproperty property.
/// Todo update documentation:
/// Changes to that property's value raise the propertychanged event.
/// This property's value is broadcasted by the messenger's default instance when it C
/// </Summary>
Public bool myproperty
{
Get
{
Return _ myproperty;
}
Set
{
If (_ myproperty = value)
{
Return;
}
VaR oldvalue = _ myproperty;
_ Myproperty = value;
// Remove one of the two CILS below
Throw new notimplementedexception ();
// Update bindings, no broadcast
Raisepropertychanged (mypropertypropertyname );
// Update bindings and broadcast change using galasoft. mvvmlight. Messenging
Raisepropertychanged (mypropertypropertyname, oldvalue, value, true );
}
}
In the following example, the user chooses which method to call. Of course, do not forget to delete throw new notimplementedexception.
Update:
For more information about Messenger, refer to another article:
Mvvm light: imessenger of the Message System
Returned directory
Judge a member
Viewmodelbase also provides many types of members.
For example, the veritypropertyname method has the conditional ("debug") feature, so it is not compiled in the release mode. It can be used to determine whether the current viewmodel has a property with the specified name. If no, an argumentexception exception is thrown.
There are also static isindesignmodestatic attributes and non-static isindesignmode attributes to determine whether the execution environment is a design mode (such as Visual Studio designer or blend ). You can execute unnecessary code in different modes. For example, you can quickly generate some data for demonstration in the design mode.