Xamarin 的 MVVM 之 Caliburn.Micro
约定
Caliburn.Micro 以下简称 CM
Xamarin.Form 以下简称 XF
摘要
CM 当前已释出 3.0 beta 版
https://github.com/Caliburn-Micro/Caliburn.Micro/tree/3.0.0
对 Xamarin 做了很多支持.
本文主要探索一下, XF 下如何使用 CM, 其它方面不做深入研究.
示例地址:
https://github.com/gruan01/Xamarin-Example/tree/master/CMTest
环境
在 Nuget 上, 已经有 CM 对 XF 的支持包:
Caliburn.Micro.Xamarin.Forms 3.0.0-beta1
这个包依赖于:
Caliburn.Micro.Core (= 3.0.0-beta1)
Xamarin.Forms (≥ 1.4.2.6355)
但是如果你只安装了上面这两个依赖, 编译是通不过的. 会提示:
无法加载: Caliburn.Micro.Platform.Core
当前这个 Platform.Core 在 Nuget 上是没有的, 需要自行下载 CM 的 3.0 beta 版, 编译,从而得到这个 dll
这里提供已编译的 DLL:
https://github.com/gruan01/Xamarin-Example/tree/master/CMTest/dll
Android 入口, Application
这个很简单, 主要是要在它里面实现一个 IoC 容器 SimpleContainer, 具体参考:
https://github.com/gruan01/Xamarin-Example/blob/master/CMTest/CMTest.Droid/Application.cs
WP 入口
这个稍微复杂一点:
1, 新建一个 Bootstrapper , 继承自 PhoneBootStrapperBase
它里面做的事情和 Android 下面的 Applicaton 做的事情基本一致, 也是要声明一个 IoC, 只不过是 SimpleContainer 的子类:
PhoneContainer, 这个类在 Caliburn.Micro.Extensions.dll 里.
除 Configure方法 和 Android 下的 Application 不一样外,其它都一样.
参考:
https://github.com/gruan01/Xamarin-Example/blob/master/CMTest/CMTest.WinPhone/Bootstrapper.cs
2, 修改 App.xaml 为以下的样子:
<Application x:Class="CMTest.WinPhone.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:CMTest.WinPhone" > <Application.Resources> <local:Bootstrapper x:Key="Bootstrapper" /> </Application.Resources> </Application>
3, 修改 App.xaml.cs 为如下:
public partial class App {
public App() {
this.InitializeComponent();
}
}
IOS入口
暂缺
XF 项目的 App
https://github.com/gruan01/Xamarin-Example/blob/master/CMTest/CMTest/App.cs
1,需要继承自 FormsApplicaton
2,构造函数中需要加上 SimpleContainer 类型的参数.
3,用 SimpleContainer 注册相关的 ViewModel 为 PerRequest 或 Singleton,
当然也可以不注册, 如果不注册的话, 在使用 INavigationService.For<xxx>.Navigate 的时候,相关 ViewModel 不会被实例, 作者是这样解释
的:
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, this in turn uses IoC.
If you're not wanting to use dependency injection and all your view models have parameterless constructors then you can pull
out all of the container code and the framework will just new up the view model.If you want to use dependency injection then 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 when being asked for a instance it doesn't know about (hence the
registration). Other containers have different behavior for this, it's up to you which you want to use.
Navigation Service 使用 IoC 中注册的 View Model ,不注册当然是视为你要自己实例化相关 View Model.
其实我提这个问题的目的是想如果加上默认注册 View Model 的功能, 那就在好不过了. 当然了, 我并没有深入了解 CM, 所以只能哈哈了.
绑定语法
使用过 CM 的, 对它提供的绑定方式一定很赞, 控件的 x:Name 只要和 ViewModel 中的提供的 属性 或 方法名一致, 就可以自动绑定, 如:
...
public string UserName{get;set;}
...
<TextBlock x:Name="UserName" />
会自动绑定 UserName 到 Text 上.
..
public void DoSomthing(){
...
}
...
<Button x:Name="DoSomthing" Content="XXX" />
会自动绑定 button 事件到 DoSomthing 方法上.
但是 XF 的对象不能通过 x:Name 获取到, 所以无法使用上面的特性, 要实现绑定, 只能用这下面的方式:
1, 数据绑定 XXX="{Binding xxx}"
2, 事件绑定 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 will not be
available, you will need to use normal {Binding Username} and cm:Message.Attach="SignIn".
这一点, 希望 Xamarin 能在后期的版本中提供支持.
子视图
如果你使用过 CM ,那一定对另外一个功能也很赞:
<ContentControl x:Name="SubVM" />
只要 ViewModel 中存在这个 SubVM , 就会把它对应的 View 给展示出来.
在 XF 中, 也支持这个功能, 不过要这样写:
<ContentView cal:View.Model="{Binding VM}" />
具体参见:
https://github.com/gruan01/Xamarin-Example/blob/master/CMTest/CMTest/ViewModels/UserInfoViewModel.cs
看一下动态切换子视图的效果:

怎么样?是不是想拿它来做个单页应用?
Xamarin 的 MVVM 之 Caliburn.Micro的更多相关文章
- WPF MVVM(Caliburn.Micro) 数据验证
书接前文 前文中仅是WPF验证中的一种,我们暂且称之为View端的验证(因为其验证规是写在Xaml文件中的). 还有一种我们称之为Model端验证,Model通过继承IDataErrorInfo接口来 ...
- WPF +MVVM(Caliburn.Micro)项目框架
最近做了一个软件,这个软件不是网站,但是与HTML,AJAX等技术密切相关,也不是只有单纯的数据库增删改查,还涉及到线程协调,比较复杂的文本处理…… 这样的软件,用OA,ERP的框架显然是不合适的,因 ...
- 从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器
从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器 之前时间一直在使用Caliburn.Micro这种应用了MVVM模式的WPF框架做开发,是时候总结一下了. Calibu ...
- MVVM Caliburn.Micro学习记录
wpf中MVVM一直用的自己写的框架,最近试了试Caliburn.Micro. Caliburn.Micro可以通过x:name来进行属性和事件绑定. 比如 <Button x:Name=&qu ...
- Caliburn.Micro(MVVM框架)
一.首启窗体设置 1. 创建一个新的WPF应用程序并添加NuGet包:Caliburn.Micro 2. 删除项目自带的主窗口文件MainWindow.xaml 3. 在App.xaml项目文件中,删 ...
- Caliburn.Micro学习笔记(一)----引导类和命名匹配规则
Caliburn.Micro学习笔记目录 用了几天时间看了一下开源框架Caliburn.Micro 这是他源码的地址http://caliburnmicro.codeplex.com/ 文档也写的很详 ...
- Caliburn.Micro学习笔记(四)----IHandle<T>实现多语言功能
Caliburn.Micro学习笔记目录 说一下IHandle<T>实现多语言功能 因为Caliburn.Micro是基于MvvM的UI与codebehind分离, binding可以是双 ...
- Caliburn.Micro 关闭父窗体打开子窗体
比如我们在做登录的时候需要关闭父窗体打开子窗体.使用Caliburn.Micro它的时候我们关闭登录窗口的时候主页面也会关闭. 解决方法就是在登录页面的CS里面写 IndexView iv = new ...
- AvalonDock 2.0+Caliburn.Micro+MahApps.Metro实现Metro风格插件式系统(一)
随着IOS7由之前UI的拟物化设计变为如今的扁平化设计,也许扁平化的时代要来了,当然我们是不是该吐槽一下,苹果什么时候也开始跟风了,自GOOGLE和微软界面扁平化过后,苹果也加入了这一队伍. Aval ...
随机推荐
- Javascript中replace()的一些用法总结
关于定义 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 关于语法 stringObject.replace(regexp/substr,repla ...
- internet connection sharing has been disabled by the network administrator
Start > Run > gpedit.msc Locate; Computer Configuration/Administrative Templates/Network/Netwo ...
- 第四篇 :微信公众平台开发实战Java版之完成消息接受与相应以及消息的处理
温馨提示: 这篇文章是依赖前几篇的文章的. 第一篇:微信公众平台开发实战之了解微信公众平台基础知识以及资料准备 第二篇 :微信公众平台开发实战之开启开发者模式,接入微信公众平台开发 第三篇 :微信公众 ...
- 第一篇:微信公众平台开发实战Java版之了解微信公众平台基础知识以及资料准备
相信很多人或多或少听说了微信公众平台的火热.但是开发还是有一点门槛,鉴于挺多朋友问我怎么开发,问多了,自己平时也进行以下总结.所以下面给大家分享一下我的经验: 微信公众号是什么? 官网的介绍:再小的个 ...
- PPT制作教程:如何制作ppt
PowerPoint(PPT)是专门用于制作演示文稿(俗称幻灯片).广泛运用于各种会议.产品演示.学校教学等.学会如何制作ppt,成为提升工作效 率的好帮手.PPT包含有很多的功能,我们可以根据个人喜 ...
- JavaScript“尽快失败”的原则
我第一次听说编码原则中有"尽快失败"这一条时,觉得很奇怪,为什么代码要失败?应该成功才对呀.但事实上,当代码在遇到错误的时候应该尽快的终止.为了检测各种状态,我们需要频繁的创建if ...
- Java实现事件机制
java中的事件机制的参与者有3种角色: 1.event object:事件状态对象,用于listener的相应的方法之中,作为参数,一般存在与listerner的方法之中 2.event sourc ...
- CentOS安装Hypernetes相关问题解法
1.手动编译hyper缺少libdevmapper.h git clone -b v2_02_103 https://git.fedorahosted.org/git/lvm2.git /usr/lo ...
- 图的遍历之深度优先搜索(DFS)
深度优先搜索(depth-first search)是对先序遍历(preorder traversal)的推广.”深度优先搜索“,顾名思义就是尽可能深的搜索一个图.想象你是身处一个迷宫的入口,迷宫中的 ...
- 边工作边刷题:70天一遍leetcode: day 71
Longest Substring with At Most Two Distinct Characters # Given a string, find the length of the long ...