WP7 Application Development Notes (14) Use Caliburn micro to simplify mvvm

Source: Internet
Author: User
Tags manual writing
Document directory
  • 1. Simplified binding
  • 2. Automatic view-viewmodel ing
  • 3. Dependency Injection
  • 4. Auxiliary Interfaces

In the previous article, I briefly introduced the mvvm mode. I learned that mvvm needs to write many custom commands and actions, and datacontext needs to be set for each class. Operations and code are repeated. To reduce the amount of code and unify standards, the mvvm framework must be introduced to improve efficiency.

 

Open-source mvvm frameworks include:
Prism: Provided by Microsoft and used together with MEF/unity for dependency injection. It supports combined commands and can be expanded. There are detailed tutorials and drills on msdn.
Mvvm light Toolkit: Templates for projects and items with Visual Studio and expression blend. For more information, see here. For more information, see the use of Vs and expression blend.
Caliburn micro: A compact but powerful framework that supports simplified binding and injection to implement multiple UI modes to solve practical problems.

Simple mvvm Toolkit: Provides templates for vs projects and items, dependency injection, deep copy, and attribute associations between models and view models.
Catel: Templates containing projects and items, user controls, and enterprise class libraries. Supports dynamic view model injection and delayed loading and verification of view models. It also supports WP7-specific view model services.

 

Here I am going to use Caliburn. Micro, which supports wp7.1 well. It also includes Caliburn. Micro. Extensions specially designed for WP.

Open Source Project address:

Http://caliburnmicro.codeplex.com/

 

Some features of the Caliburn. Micro framework

Caliburn. Micro uses many naming conventions to implement and simplify code, and provides many mechanisms to reduce development work.

 

1. Simplified binding

Caliburn. Micro has designed a binding convention for binding conventions to simplify the binding code:

You can directly set the X: name with the same name (case sensitive) as the binding to automatically bind it.

1) simplify data binding:
<TextBox Text="{Binding Path=CustomerName}" />

In Caliburn. Micro, you can write it as follows:

<TextBox x:Name=”CustomerName” />

Achieve the same data binding effect, and the default is two-way binding.

 

2) Simplify command binding:

Command binding is also supported and simpler:

The main code used to bind commands:

public ICommand HelloCommand{ get; set; }

HelloCommand = new InvokeCommand(OnHello);

public void OnHello(){

MessageBox.Show(“Hello world”)

}

<Button Command="{Binding HelloCommand}" />

 

It can be written in Caliburn. Micro. It can be directly the same as the method name, and other commands can be used.

<Button x:Name=”OnHello” />

 

3) Simplified action binding (event binding ):

The action is bound to Caliburn. Micro to implement its own actionmessage. The original binding method is as follows:

<Button Content="Button"> 
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<local:InvokeAction Command="{Binding HelloCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>

Write the following method and set methodname to the corresponding method name.

<Button Content="Button">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="OnHello" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>

You can also set the parameter of the method. You can use $ eventargs to automatically input the eventarg object.

<cal:Parameter Value="$eventArgs" /> 

 

2. Automatic view-viewmodel ing

You do not need to write code such as datacontext = new mainpageviewmodel ();. Caliburn. Micro can automatically complete this ing,

You only need to follow the naming conventions:

View: {customname} View

Name For viewmodel: {customname} viewmodel

For example, helloview is automatically mapped and bound to helloviewmodel.

 

Note that the namespace is also required:

Two namespaces can be correctly identified:

  • The view and viewmodel are in the same namespace.
  • View is in the Views namespace and viewmodel is in the viewmodels namespace.

 

3. Dependency Injection

 

Because Caliburn. Micro uses the IOC container internally, dependency injection is also a very easy function, so we will not detail it here.

Note that Caliburn. Micro uses a lightweight phonecontainer container in WP.

 

 

4. Auxiliary Interfaces

In the mvvm mode, the interaction between viewmodels is complicated. Caliburn. Micro provides many injection interfaces.

Use viewmodel.

 

1) inavigationservice navigation service interface:

Page navigation is an important feature of WP development. The inavigationservice interface implements the encapsulated navigation function and can be injected and used in viewmodel.

First, constructor injection:

public MainPageViewModel(INavigationService navigationService…) 
{
this.navigationService = navigationService;

}

 

Then you can directly navigate to the viewmodel without having to know the relative address of the View:

navigationService.UriFor<ConfigViewModel>().Navigate();

Of course, you can also move back:

navigationService.GoBack();

 

2) tombstone mechanism and viewmodel status storage and interrupt recovery

I would like to give it a compliment. This feature is very powerful!

 

The tombstone mechanism is a program running rule in the mobile phone operating system of Microsoft Windows Phone 7. To put it simply, when a task on a mobile phone is forced to be interrupted (if a call is sent), the system records the status of the current application (like recording the event on the tombstone ), then stop the program. When the program needs to be restored, the program is restored to the State before the interruption according to the content on the tombstone. Such a mechanism is the "Tombstone mechanism ".

 

However, manual writing is very troublesome to restore the interrupted state. Caliburn. Micro provides a simpler implementation and greatly improves the efficiency.

Istoragehandler and abstract class storagehandler <t> can save and restore the viewmodel status. They can be stored in State or isolatedstoragesettings,

It is easy to use and can work automatically.

 

First, let's look at a simple viewmodel example:

public class MainPageViewModel:Screen 
{
private string hello;

public string Hello
{
get { return hello; }
set
{
hello = value;
NotifyOfPropertyChange(() => Hello);
}
}

}

 

Defines the mainpageviewmodelstorage Class Based on mainpageviewmodel and inherits the storagehandler <mainpageviewmodel>

Do not reference after definition is complete,Caliburn. Micro will work automatically.

 

public class MainPageViewModelStorage : StorageHandler<MainPageViewModel> 
{

public override void Configure()
{
Id(p => p.DisplayName);

Property(model => model.Hello)
.InPhoneState()
.RestoreAfterActivation();
}
}

 

First, you must specify the ID as the unique identifier of viewmodel. Here ID (P => P. displayname) is used ).

Set the storage mode and recovery events of each property.

 

The storage methods include inphonestate and inappsettings.

The recovery events include restoreafteractivation, restoreafterviewload, and restoreafterviewready.

This setting method is a bit like the map ing of Nhibernate.

 

3) iwindowmanager form Management

You can open a custom dialog or Popup.

You only need to know the viewmodel and do not need to know the view. For example:

windowManager.ShowDialog(new ConfigViewModel);

 

4) Message notifications between ieventaggregator forms

The observer mode and intermediary mode are implemented. ieventaggregator acts as the intermediary to send a message object from one viewmodel to the other viewmodel that subscribes to the message.

It is rarely used in WP, but ieventaggregator implements UI thread switching, which is sometimes more convenient.

For more information, see:

Http://caliburnmicro.codeplex.com/wikipage? Title = the % 20 event % 20 aggregator & referringtitle = documentation

 

 

For more information, see:

Learn the sample code provided by Caliburn. Micro v1.3 RTW \ samples:

In particular:

Caliburn. Micro. hellowp71

Caliburn. Micro. hellowindowmanagerwp71

Read the document:

Documentation: detailed documentation:

Http://caliburnmicro.codeplex.com/documentation

Working with Windows Phone 7 V1.1 :( Caliburn. Micro is introduced in the WP Project)

Http://caliburnmicro.codeplex.com/wikipage? Title = working % 20 with % 20 WINDOWS % 20 phone % 207% 20v1. 1 & referringtitle = documentation

 

Some Caliburn. Micro tutorials in the garden:

Http://www.cnblogs.com/Zhouyongh/tag/Caliburn/

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.