Introduction to the use of the Caliburn micro framework WP8 (I)

Source: Internet
Author: User
Introduction

The emergence of mvvm to WPF SilverlightProgramThe framework of the program is defined. mvvm can solve the coupling problem between the business layer and the UI Layer. More advantages will not be described here.

On the WP mobile phone end, many projects also adopt mvvm architecture. The common tool set is mvvm light toolkit. It is applied in many known WP projects, and most self-implemented mvvm architecture tools are also based on this. What we want to talk about today is Caliburn micro, the framework for mvvm architecture.

First, compare mvvm light toolkit and Caliburn micro:

mvvm light

Caliburn micro

communication between view and viewmodel

event: Use eventtocommand to bind the event in view to the Command defined in viewmodel through the icommand interface

attribute: binding to viewmodel corresponds to a property

event: you do not need to define the command in viewmodel

attribute in the method that directly associates events in view into viewmodel through action: automatic Binding Based on naming conventions

binding between view and viewmodel

bind datacontext to viewmodel in viewmodellocator in view

automatically finds the viewmodel corresponding to the view based on the Naming Convention (conventions) and binds it automatically

communication between viewmodels

Provides messenger for communication

provides eventaggregator for communication

navigation in viewmodel

Custom navigationservice

locate the view through viewmodel Based on the naming conventions and then perform the navigation

advantages

the concept is simple, clear, and easy to understand

automatically completes many tasks, greatly reducing the coding workload of developers

Insufficient

Compared with Caliburn mircoCodeMore

The learning cost is high, and many concepts are hard to grasp for the first time.

Both advantages and disadvantages are not analyzed too much. In my opinion, although cm learning costs are high, it provides more convenient development functions and is a good choice for the team.

Starting from Hello World

 

Let's first look at the basic usage of the CM framework from the simplest program.

After creating the WP8 program, you can use nuget to add Caliburn micro in references:

What we need to do next is to remove the content in APP. XAML. CS (yes, It is removed, which will be detailed later) and then App. XAML. CS looks like this.

Then we define a class in the root directory. The class name is random, such as bootstrapper, and then add the following content:

 Using  System. Collections. Generic;  Using  Caliburn. Micro;  Using  Caliburnpractise. viewmodels;  Namespace Caliburnpractise {  Public   Class  Bootstrapper: phonebootstrapper {  Private  Phonecontainer _ phonecontainer;  Protected   Override   Void  Configure () {_ phonecontainer = New  Phonecontainer (rootframe); _ phonecontainer. registerphoneservices ();}  Protected  Override   Object Getinstance (system. Type service, String  Key ){  Return  _ Phonecontainer. getinstance (Service, key );}  Protected   Override Ienumerable < Object > Getallinstances (system. Type Service ){  Return  _ Phonecontainer. getallinstances (service );}
Protected Override Void Buildup ( Object Instance) {_ phonecontainer. buildup (instance );}
}}

 

 

Analysis: Cm uses phonecontainer as a container to provide examples (the concept of dependency inject and container is not described here. If you are not clear about it, you can refer to the relevant information and do not explain it too much here)

Previously, we deleted the app. XAML. cschinese file because phonebootstrapper had encapsulated it, And phonebootstrapper had taken over these events.

Next we need to define bootstrapper in APP. XAML.

So the bootstrapper is added.

Next, add mainpageviewmodel in the root directory and define it in it.

Add textblock in mainpage. XAML and name it title

Add the following code to the configure of bootstrapper:

Then we run the program and we will see the magic. After the program is opened, the title in the UI is automatically bound.

CM automatically completes two tasks:

1. The mainpage is automatically bound to the datacontext according to the naming convention, which is found through phonecontainer.

Corresponding mainpage and then auto binding

2. According to the naming conventions, the attribute named title in viewmodel is found and automatically bound to the text of textbox.

Next, we will try to add a function in mainpageviewmodel:

Add a button named sayhello in the View:

Then run and click this button. It is a magical act. MessageBox pops up and sayhello is executed.

Analysis:

Through the above example, we can see that the program separates the UI code from the logic code, and we do not have a lot of tedious work. These functions are consistent with the internal Naming Convention mechanism of CM, automatic Binding helps us complete our work. Very powerful, isn't it?

Through the small example above, we can see the advantages of CM and its strength.Article.

First, provide some addresses for you to learn:

Official documentation: caliburnmicro.codeplex.com/documentation

Series of blogs: wp.qmatteoq.com/first-steps-with-caliburn-micro-in-windows-phone-8-the-first-project/

QQ chat group: 182659848 welcome to join us for discussion.

 

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.