Caliburn. Micro tips

Source: Internet
Author: User

Recently, WP7 found a very interesting framework: Caliburn. micro. After playing for a few days, I encountered some problems, and I forgot to summarize them. For details about Caliburn. Micro (CM), refer
Here I will use a simple example (Caliburn. Micro. hellowp7) in the CM package to introduce the problems encountered.

1.Here the http://caliburnmicro.codeplex.com/releases/view/67451 downloads the CM package, unzip

2.Open the Caliburn. Micro. HelloWP7 project under the samples directory.

3.First, you must pay attention to the reference assembly. For wp,

The following Assembly needs to be referenced:

Caliburn. Micro

Caliburn. Micro. Extension

System. Windows. Interactivity

The three dll files can be found in \ bin \ WP7 \ Release.

4.We found that HelloWP7 has a class: HelloWP7Bootstrapper

This class is used to guide or initialize windows phone projects and initialize Caliburn configuration information, such as contract and ViewModel.

5.With HelloWP7Bootstrapper, you must add HelloWP7Bootstrapper to App. xaml application resources.

<Application. Resources>
<Local: HelloWP7Bootstrapper x: Key = "bootstrapper"/>
</Application. Resources>

6.As we know above, HelloWP7Bootstapper is used to initialize WP. Therefore, we need to clear other initialization code in App. xaml. cs with only the following code:

Namespace Caliburn. Micro. HelloWP7 {
Using System. Windows;

Publicpartialclass App: Application {
Public App (){
InitializeComponent ();
}
}
}

The official website explains what to clear this code: Important Note About App. xaml. cs
If you create your WP7 application using a standard Visual Studio template, the generated App. xaml. cs file will have a lot of code in it. the purpose of this code is to set up the root frame for the application and make sure everything gets initialized properly. of course, that's what the bootstrapper's job is too (and in fact it does a few things better than the out-of-the-box code in addition to processing CM ). so, you don't need both. when using CM's PhoneBootstrapper, be sure to clear out all the code from the App. xaml. cs file should t for the call to InitializeComponent in the constructor.

7.The above steps have basically been used to build a CM environment. How does CM separate the interface xaml code from the business logic? In the HelloWP7 project, we can see that MainPage has another class file MainPageViewModel. cs, which is used to process the MainPage business logic. How is it associated with MainPage? Let's look back at the config function of the HelloWP7Bootstrapper class. We can see the following code:

Container = new PhoneContainer (RootFrame );
Container. RegisterPhoneServices ();
Container. PerRequest <MainPageViewModel> ();

The first and second lines of code are required and cannot be deleted !! Container. perrequest <mainpageviewmodel> () indicates that mianpage is registered as a service to process each request. Can mainpage and mainpageviewmodel be connected in this way? A little worse

8.To handle improper business logic in viewmodel, you must register a viewmodel in bootstrapper and specify naming rules. The naming rule for mainpage is mainpage + viewmodel, that is, mainpageviewmodel. However, for other newly added pages, the new page must end with a view, such as loginview. XAML, registview. in XAML, the VM class name must be loginviewmodel. CS, registviewmodel. CS. Note the difference between mainpage and other pages. Otherwise, nullreference is abnormal during navigate.

9.Use viewmodel to process page events. CM uses the name attribute of the element to associate the relationship between the foreground and viewmodel. For example, if XAML has a button for X: Name = "btnlogin", add a common btnlogin function to viewmodel, you can process the button logic in the btnlogin function. For selectedchange events, you must use the message mechanism in cm, for example, we need to assign the selectedchanged value of ListBox in the view to the viewmodel for processing. we must add the following attribute to ListBox: Cal: Message. attach = "[event selectionchanged] = [Action selectionchanged ($ source)]" It is easy to understand that event specifies the event to be bound, and Action specifies the event to be processed by viewmodel. That is:

ItemTemplate = "{StaticResource NewsListItemTemplate }"
ItemsSource = "{Binding Items }"
Cal: Message. Attach = "[Event SelectionChanged] = [Action SelectionChanged ($ source)]"
ItemContainerStyle = "{StaticResource ListBoxItemStyle }"
HorizontalContentAlignment = "Stretch"
ScrollViewer. VerticalScrollBarVisibility = "Disabled"/>

For viewmodel, you only need to add a public function selectionchanged:

Publicvoid SelectionChanged (object sender)
{
Var listBox = (ListBox) sender;
If (ListBox. selectedindex =-1)
Return;
// Do whatever you want...
}

10.Binding ListBox data cm through viewmodel provides a class bindablecollection for automatically binding UI thread elements. The specific usage is to declare a public member variable in viewmodel.

Public bindablecollection <News> items {Get; private set ;}

This variable is used to save all your data, initialize it in the constructor, and add data to it in a specific place.

If the value in items is used in the page ListBox, the method is very simple. You only need to specify the itemsource of ListBox as items.

<ListBox Height = "Auto" name = "listbox1" itemssource = "{binding items}">
<ListBox. itemtemplate>
<Datatemplate>
<Textblock text = "{binding title}"/>
</Datatemplate>
</ListBox. itemtemplate>
</ListBox>

11.In the mainpageviewmodel of hellowp, we can see that the constructor of CM is as follows:

Public mainpageviewmodel (inavigationservice navigationservice)
{
This. navigationservice = navigationservice;
}

Here, inavigationservice is the interface that cm provides for navigation. CM 1.1 supports page jump through viewmodel, so it is very convenient to use. For specific code, refer to the gotopagetwo () method in hellowp.

12.How do I obtain interface controls in viewmodel?

Cm provides a Class Screen to obtain the current interface. Therefore, we only need to inherit the screen in viewmodel and get the view instance through the getview () method.

Mainpage view = (mainpage) getview (null); you can get the page control through view.

Now, let's summarize it. Enjoy it :-)

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.