The caliburn.micro of the MVVM of Xamarin

Source: Internet
Author: User

Convention

Caliburn.micro hereinafter referred to as CM
Xamarin.form hereinafter referred to as the XF

Summary
CM is currently released 3.0 beta version
https://github.com/Caliburn-Micro/Caliburn.Micro/tree/3.0.0
Has done a lot of support for Xamarin.


This article mainly explores how to use CM under the XF, and does not do any further research.

Example Address:
Https://github.com/gruan01/Xamarin-Example/tree/master/CMTest

Environment
On Nuget, there are already CM support packages for XF:
Caliburn.Micro.Xamarin.Forms 3.0.0-beta1
This package depends on:
Caliburn.Micro.Core (= 3.0.0-beta1)
Xamarin.Forms (≥1.4.2.6355)

But if you only install the above two dependencies, the compilation is not a pass. Will prompt:
Unable to load: Caliburn.Micro.Platform.Core

Currently this platform.core is not on Nuget and needs to download CM's 3.0 beta version itself, compiling it to get this DLL
The compiled DLL is available here:
Https://github.com/gruan01/Xamarin-Example/tree/master/CMTest/dll

Android Portal, Application

This is very simple, mainly to implement an IoC container simplecontainer in it, for specific reference:
Https://github.com/gruan01/Xamarin-Example/blob/master/CMTest/CMTest.Droid/Application.cs

WP Entrance

This is a little bit more complicated:

1, create a new Bootstrapper, inherit from Phonebootstrapperbase
What it does is basically the same thing that Applicaton does on Android, and it's about declaring an IoC, just a subclass of Simplecontainer:
Phonecontainer, this class is in Caliburn.Micro.Extensions.dll.
In addition to the Configure method and Android under the application is not the same, the others are the same.
Reference:
Https://github.com/gruan01/Xamarin-Example/blob/master/CMTest/CMTest.WinPhone/Bootstrapper.cs

2. Modify the App.xaml to look like this:

1 <Application2 x:class= "CMTEST.WINPHONE.APP"3 xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"4 xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"5 xmlns:local= "Clr-namespace:cmtest.winphone"6 >7 8 <application.resources>9 <Local:bootstrapperx:key= "Bootstrapper" />Ten </application.resources> One </Application>

3, modify the App.xaml.cs as follows:

1  Public Partial class App {23 public apps () {4this. InitializeComponent (); 5 }67 }

iOS Portal

N/

Apps for the XF project

Https://github.com/gruan01/Xamarin-Example/blob/master/CMTest/CMTest/App.cs

1, which needs to inherit from Formsapplicaton
2, requires a parameter of type Simplecontainer in the constructor.
3, with Simplecontainer registration related ViewModel for perrequest or Singleton,
of course, can not register, if not registered, in the use of Inavigationservice.for<xxx> ;. When Navigate, the relevant ViewModel is not instantiated, as the author interprets
:
https://github.com/Caliburn-Micro/Caliburn.Micro/issues/182# issuecomment-132356227

The navigation service uses the view model locator to find the appropriate view model, which is in turn uses IoC.

If you ' re not wanting to use dependency injection and all your view models has parameterless constructors then you can PU ll
Out all of the container code and the framework would just new up the view model.

If you want to use dependency injection so you can choose to use the Simplecontainer that comes with Caliburn or your
Container of choice instead.

The behavior of Simplecontainer is to return NULL if being asked for a instance it doesn ' t know about (hence the
Registration). Other containers has different behavior for this, it's up to you which your want to use.

Navigation Service uses the view model registered in the IoC, not registering is, of course, considered to be your own instantiation of the associated view model.

In fact, the purpose of my question is to add the default registration of the View Model function, it is not too. Of course, I did not have a deep understanding of CM, so only haha.

binding syntax

Using the CM, it must be a good way to bind it, the control x:name as long as the provided property or method name in the ViewModel is consistent, you can automatically bind, such as:
...
public string Username{get;set;}
...
<textblock x:name= "UserName"/>

The UserName is automatically bound to Text.

..
public void dosomthing () {
...
}
...
<button x:name= "dosomthing" content= "XXX"/>
The button event is automatically bound to the Dosomthing method.

However, the XF object cannot be obtained through x:name, so the above features cannot be used, so the binding can only be done in the following way:
1, data binding xxx= "{Binding XXX}"
2, event binding cm:message.attach= "XXX"

http://caliburnmicro.com/announcements/

There is no programmatic access to x:name in Xamarin.Forms, therefore the feature of Name based conventions won't be
Available, you'll need to use normal {Binding Username} and Cm:message.attach= "SignIn".

This, hopefully Xamarin will provide support in later versions.


Child view

If you've ever used CM, it's also great for another feature:
<contentcontrol x:name= "SUBVM"/>
As long as there is this SUBVM in the ViewModel, it will show the corresponding View.
This feature is also supported in the XF, but it is written like this:

<contentview cal:view.model= "{Binding VM}"/>

For details, see:
Https://github.com/gruan01/Xamarin-Example/blob/master/CMTest/CMTest/ViewModels/UserInfoViewModel.cs
Take a look at the effect of dynamically switching sub-views:

Do you want to use it for a single page application?

The caliburn.micro of the MVVM of Xamarin

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.