MVVMbasics Logo

mobilemotion.eu Logo


Back to Class Reference home page


MVVMbasics.Viewmodels
Class BaseViewmodel

MVVMbasics.Models.BaseModel
  extended by MVVMbasics.Viewmodels.BaseViewmodel
 

 
public abstract class BaseViewmodel : BaseModel

Base viewmodel class all actual viewmodels can be derived from.


Property Summary
public ViewState ViewState { get; private set; }
Flag that indicates whether the View which is registered to this Viewmodel is active (shown on the screen) or not. Can be used to decide whether a message box shall be shown or not, for example.
public ServiceLocator ServiceLocator { get; set; }
ServiceLocator to be used to retrieve service instances during Viewmodel logic. Is set by the View when instantiating the Viewmodel, only if it was defined in the BaseApplication.
Should be used instead of directly referencing Application.Current.ServiceLocator, since the Application might not in all cases be accesible (e.g., in multi-platform Apps the Viewmodel might be located within a Portable Class Library that can not access the Application classes.

Deprecated. Use App.Services or any 3rd party IoC container instead.
 
Constructor Summary
public  BaseViewmodel()
Tries to set the CommandAutobinding flag depending on whether an MvvmCommandAutobinding attribute is registered, and automatically binds Commands to methods if applicable.
 
Method Summary
virtual void  OnServiceLocatorAvailable()
Method that is called when the App's ServiceLocator is set for the first time. Usually this happens shortly after the constructor.
Deprecated. Use App.Services or any 3rd party IoC container instead.
virtual void  OnNavigatedTo(ParameterList uriParameters, ParameterList parameters, ViewState viewState)
Method that is called when navigating to a page / window.
virtual void  CancelNavigatingFrom(ViewState viewState)
Method that is called when the page / window is about to be closed, in order to return back to previously shown page / window.
virtual void  OnNavigatedFrom(ViewState viewState)
Method that is called when a page / window is closed.
protected void  RunOnMainThread(Action action)
Helper method that checks if some actions needs to be invoked on the main thread, and calls the dispatcher to do so.
protected void  Set<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
Setter method that sets the private member field and fires the PropertyChanged event using the Dispatcher.
protected void  Set<T>(Func<bool>> before, ref T field, T value, [CallerMemberName] string propertyName = "")
Setter method that sets the private member field, fires the PropertyChanged event using the Dispatcher and calls a specified method to decide whether to continue updating the member field.
protected void  Set<T>(ref T field, T value, Action after, [CallerMemberName] string propertyName = "")
Setter method that sets the private member field, fires the PropertyChanged event using the Dispatcher and calls a specified method after updating the member field.
protected void  Set<T>(Func<bool>> before, ref T field, T value, Action after, [CallerMemberName] string propertyName = "")
Setter method that sets the private member field, fires the PropertyChanged event using the Dispatcher and calls two specified methods: One before updating the member field to decide whether to continue, one after updating the member field.
 
Constructor Detail

BaseViewmodel

public BaseViewmodel()

Tries to set the CommandAutobinding flag depending on whether an MvvmCommandAutobinding attribute is registered, and automatically binds Commands to methods if applicable.

Method Detail

OnServiceLocatorAvailable

public virtual void OnServiceLocatorAvailable()

Method that is called when the App's ServiceLocator is set for the first time. Usually this happens shortly after the constructor.
Subclasses can override this method to be notified when the ServiceLocator is available, and then do all the tasks that would occur in the constructor but are dependent on the ServiceLocator.

Deprecated. Use App.Services or any 3rd party IoC container instead.


OnNavigatedTo

public virtual void OnServiceLocatorAvailable(ParameterList uriParameters, ParameterList parameters, ViewState viewState)

Method that is called when navigating to a page / window.
Subclasses can override this method to be notified when the page / window they represent is loaded and shown on the screen. In addition, all parameters that were passed to the page / window can be easily retrieved and processed inside this method.

Parameters:

parameters - List of parameters that were passed from the calling page / window.
uriParameters - List of parameters that were contained within the URI used to navigate to the current page / window.
viewState - Indicates the lifecycle state the View is about to reach.


CancelNavigatingFrom

public virtual void CancelNavigatingFrom(ViewState viewState)

Method that is called when the page / window is about to be closed, in order to return back to previously shown page / window.
Subclasses can override this method and return TRUE in order to cancel the closing and the navigation process.

Parameters:

viewState - Indicates the lifecycle state the View is about to reach.


OnNavigatedFrom

public virtual void OnNavigatedFrom(ViewState viewState)

Method that is called when a page / window is closed.
Subclasses can override this method to be notified when the page / window they represent is closed.

Parameters:

viewState - Indicates the lifecycle state the View is about to reach.


RunOnMainThread

protected void RunOnMainThread(Action action)

Helper method that checks if some actions needs to be invoked on the main thread, and calls the dispatcher to do so.

Parameters:

action - Action to be invoked using the dispatcher if necessary.