源码地址:https://github.com/l2999019/DemoAppgit
能够Star一下,随意 - -github
嗯..前面3篇就是基础内容..后面就开始逐渐要加深了,进阶篇开始了.ide
今天咱们讲讲Xamarin中的MVVM双向绑定,嗯..须要有必定的MVVM基础.,具体什么是MVVM - -,请百度,我就很少讲了函数
效果以下:post
这个时间的功能很简单,就是一个时间的动态显示.学习
咱们首先建立一个基础的页面以下:this
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:DemoApp.MVVMDemo.ViewModel" x:Class="DemoApp.MVVMDemo.MVVMPageDemo"> <ContentPage.Content> <StackLayout > <Label Text="{Binding DateTime,StringFormat='{0:F}'}"> <Label.BindingContext> <local:TimeViewModel></local:TimeViewModel> </Label.BindingContext> </Label> </StackLayout> </ContentPage.Content> </ContentPage>
你们能够发现,咱们此次多了一些内容.spa
首先,咱们会发现ContentPage的xmlns定义中多了一个local的定义.这个很重要,他是用来让咱们在xaml中引用其余程序集中的类,相似于Using的做用.双向绑定
剩下的BindingContext和Bingding关键字,后面咱们慢慢讲
接下来,咱们建立一个ViewModel的类以下:
public class TimeViewModel : INotifyPropertyChanged { //定义一个时间类型 DateTime dateTime; //实现接口的事件属性 public event PropertyChangedEventHandler PropertyChanged; //建立构造函数,定义一个定时执行程序 public TimeViewModel() { this.DateTime = DateTime.Now; //定义定时执行程序,1秒刷新一下时间属性 Device.StartTimer(TimeSpan.FromSeconds(1), () => { this.DateTime = DateTime.Now; return true; }); } //定义时间属性,建立SetGet方法,在Set中使用PropertyChanged事件,来更新这个时间 public DateTime DateTime { set { if (dateTime != value) { dateTime = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("DateTime")); } } } get { return dateTime; } } }
咱们继承了INotifyPropertyChanged,从类名就能够看出来,这个是关于实现属性变动事件的一个接口.
他包含一个PropertyChanged,属性变动事件,咱们须要在每一个属性变动的时候(也就是Set中),调用它
在具体的开发过程当中,若是你须要使用MVVM那么你全部的ViewModel都应该继承它.
不少解释我都写在了注释里面,请仔细看注释
而后咱们回到Xaml中的BindingContext,它的做用就一目了然了,给这个Xaml控件,绑定一个上下文对象,也就是你定义的ViewModel,来方便你绑定其中的属性
<Label Text="{Binding DateTime,StringFormat='{0:F}'}"> 这句的意思就是,绑定其中的DateTime属性,并格式化显示.
咱们在构造函数中启动的定时程序,就会一直更新DateTime,对应的,页面上也会一直随着变动.这样咱们就实现了一个基础的MVVM
效果如图:
经过上面的小栗子,咱们学习了一下基本的绑定关系和绑定方法.
那么下面就来一个比较复杂,比较难的例子.效果是这样的,如图:
咱们建立三个数值,他们与控件Slider来绑定,并控制.更新值的同时,求和.获得NumSun的值.
在界面中,咱们有一个清空的Button来清除这个ViewModel中的值.
首先,咱们建立xaml代码以下:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:DemoApp.MVVMDemo.ViewModel" x:Class="DemoApp.MVVMDemo.MVVMDemoPage2"> <ContentPage.BindingContext> <local:AddNumViewModel></local:AddNumViewModel> </ContentPage.BindingContext> <ContentPage.Content> <StackLayout> <Label Text="{Binding NumSun,Mode=TwoWay,StringFormat='总数NumSun={0:F2}'}" /> <Label Text="{Binding Num1, StringFormat='Num1 = {0:F2}'}" /> <Slider Value="{Binding Num1,Mode=TwoWay}" /> <Label Text="{Binding Num2, StringFormat='Num2 = {0:F2}'}" /> <Slider Value="{Binding Num2,Mode=TwoWay}" /> <Label Text="{Binding Num3, StringFormat='Num3 = {0:F2}'}" /> <Slider Value="{Binding Num3,Mode=TwoWay}" /> <Button Text="清空" Command="{Binding CleanCommand}" /> </StackLayout> </ContentPage.Content> </ContentPage>
而后建立咱们的ViewModel代码以下:
public class AddNumViewModel : INotifyPropertyChanged { //定义属性值 double num1, num2, num3,numSun; public event PropertyChangedEventHandler PropertyChanged; //定义清空的命令 public ICommand CleanCommand { protected set; get; } public AddNumViewModel() { //实现清空 this.CleanCommand = new Command((key) => { this.NumSun = 0; this.Num1 = 0; this.Num2 = 0; this.Num3 = 0; }); } /// <summary> /// 统一的属性变动事件判断方法 /// </summary> /// <param name="propertyName"></param> protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public double Num1 { set { if (num1 != value) { num1 = value; OnPropertyChanged("Num1"); SetNewSunNum(); } } get { return num1; } } public double Num2 { set { if (num2 != value) { num2 = value; OnPropertyChanged("Num2"); SetNewSunNum(); } } get { return num2; } } public double Num3 { set { if (num3 != value) { num3 = value; OnPropertyChanged("Num3"); SetNewSunNum(); } } get { return num3; } } public double NumSun { set { if (numSun != value) { numSun = value; OnPropertyChanged("NumSun"); } } get { return numSun; } } /// <summary> /// 把数值加起来的方法(业务逻辑) /// </summary> void SetNewSunNum() { this.NumSun = this.Num1 + this.Num2 + this.Num3; } }
很简单,咱们建立了Num1,Num2,Num3和NumSun四个属性.实现了一个SetNewSunNum的方法,来求和.
而后就一一对应的在xaml中绑定了相关的属性.全部的Slider绑定中都有个Mode=TwoWay,意思就是,这个属性为双向绑定,在控件中变动它的同时,也会在ViewModel中变动.
而后咱们在来看看清空按钮的命令绑定.
先解释一下,为何会有命令绑定这个东西,由于咱们使用双向绑定的时候,页面的点击事件,并不能直接调用到ViewModel,因此就衍生了一个叫命令绑定的东西.来和咱们控件的各类事件相关联.
咱们回到代码,会发现,在AddNumViewModel中,咱们定义了一个继承自 ICommand的CleanCommand 的命令,并在构造函数中实现了它
在咱们的xaml中,buttom绑定了这个事件 <Button Text="清空" Command="{Binding CleanCommand}" />
这样,就能够直接调用到ViewModel了,固然你的命令也能够传递参数,以下:
<Button Text="清空" Command="{Binding CleanCommand}" CommandParameter="aaa" />
aaa就是你传递的参数.
接收也很简单,稍微改一下.CleanCommand 以下:
这个key就是你传递进来的参数了..
今天主要学习了Xamarin中的MVVM双向绑定和命令绑定,
须要双向绑定的类,须要继承INotifyPropertyChanged,须要绑定的命令,须要继承:ICommand
最后,列一下可使用命令绑定的控件.
Button
MenuItem
ToolbarItem
SearchBar
TextCell(因此也包含
ImageCell
)
ListView
TapGestureRecognizer
除了SearchBar和
ListView这两个控件
以外,这些控件均可以使用Command
和CommandParameter
嗯..,SearchBar
定义SearchCommand
和SearchCommandParameter
属性,而ListView
定义一个RefreshCommand
属性的类型ICommand
。
其实都是同样的..名字换了一下..
嗯..没啥好说的..持续更新中..