Xamarin Forms MVVM实现效果说明
实体对象定义Model2 和Model均可 实现响应效果
public class BaseModel : INotifyPropertyChanged
{
private bool _selected;
public bool Selected
{
get { return _selected; } set
{
if (Selected != value)
{
_selected = value;
OnPropertyChanged();
OnPropertyChanged("StatusFormat");
}
}
} public string StatusFormat
{
get
{
return Selected ? "选中了" : "没选中";
}
} public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} public event PropertyChangedEventHandler PropertyChanged;
} public class BaseModel2 : BindableObject
{
public static readonly BindableProperty SelectedProperty =
BindableProperty.Create("Selected",
typeof(bool),
typeof(bool),
false );
public bool Selected
{
get
{
return (bool)this.GetValue(SelectedProperty);
}
set
{
if (Selected != value)
{
this.SetValue(SelectedProperty, value);
//通知下另外的依赖属性跟着变
OnPropertyChanged("StatusFormat");
}
}
} public string StatusFormat
{
get
{
return Selected ? "选中了" : "没选中";
}
}
}
public partial class MainPage : ContentPage
{
/// <summary>
/// 我实现了2个mvvm方式 BaseModel 和 BaseModel2 都可以实现响应
/// </summary>
private ObservableCollection<BaseModel2> Models;
public int Tick { get; set; }
public MainPage()
{
InitializeComponent();
Models = new ObservableCollection<BaseModel2>();
lst.ItemsSource = Models;
this.BindingContext = this;
} private void Button_Clicked(object sender, EventArgs e)
{
Tick++;
this.OnPropertyChanged("Tick");
if (Models.Count==)
{
Models.Add(new BaseModel2 { Selected = false });
Models.Add(new BaseModel2 { Selected = true });
Models.Add(new BaseModel2 { Selected = true });
Models.Add(new BaseModel2 { Selected = false });
} Models[new Random().Next(Models.Count)].Selected = !Models[new Random().Next(Models.Count)].Selected; }
}
<?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:App5Mvvm"
x:Class="App5Mvvm.MainPage"> <StackLayout>
<!-- Place new controls here -->
<Label Text="Xamarin MVVM哪些事"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<Label Text="{Binding Tick, StringFormat='{0}次点击'}}"></Label>
<Button Clicked="Button_Clicked" Text="点我点我"></Button>
<ListView x:Name="lst">
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell>
<StackLayout Orientation="Horizontal">
<BoxView WidthRequest="" Color="Red" IsVisible="{Binding Selected}"></BoxView>
<Label Text="{Binding StatusFormat}"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout> </ContentPage>
Xamarin Forms MVVM实现效果说明的更多相关文章
- Xamarin Forms 进度条控件
本文翻译:http://xamlnative.com/2016/04/14/xamarin-forms-a-simple-circular-progress-control/ 里面都是胡说的,如果看不 ...
- Xamarin.Forms 自定义控件(呈现器和效果)
Xamarin.Forms 使用目标平台的本机控件呈现用户界面,从而让 Xamarin.Forms 应用程序为每个平台保留了相应的界面外观.凭借效果,无需进行自定义呈现器实现,即可自定义每个平台上的本 ...
- 从零开始学Xamarin.Forms(一) 概述
原文:从零开始学Xamarin.Forms(一) 概述 Xamarin 读 "ˈzæmərin",是一个基于开源项目mono的能够使用C#开发的收费的跨平台(iOS.And ...
- Xamarin.Forms 开发资源集合(复制)
复制:https://www.cnblogs.com/mschen/p/10199997.html 收集整理了下 Xamarin.Forms 的学习参考资料,分享给大家,稍后会不断补充: UI样式 S ...
- Xamarin.Forms 开发资源集合
收集整理了下 Xamarin.Forms 的学习参考资料,分享给大家,稍后会不断补充: UI样式 Snppts: Xamarin Forms UI Snippets. Prebuilt Templat ...
- 使用MvvmCross框架实现Xamarin.Forms的汉堡菜单布局
注:本文是英文写的,偷懒自动翻译过来了,原文地址:Implementing MasterDetail layout in Xamarin.Forms by MvvmCross 欢迎大家关注我的公众号: ...
- 从零開始学Xamarin.Forms(一) 概述
Xamarin 读 "ˈzæmərin",是一个基于开源项目mono的可以使用C#开发的收费的跨平台(iOS.Android.Windows Phone.Mac)解决方式. ...
- Xamarin.Forms之XAML
官网参考 XAML基础知识 XAML(eXtensible Application Markup Language)可扩展应用程序标记语言,允许开发者在Xamarin.Forms应用中采用标记而不是代 ...
- Xamarin.Forms 入门
介绍 Xamarin.Forms是一个开源UI框架,Xamarin.Forms允许开发人员从单个共享代码库构建Android,iOS和Windows应用程序. Xamarin.Forms允许开发人员使 ...
随机推荐
- boost-字符编码转换:使用conv
Windows下的字符集转换可以使用WideCharToMultiByte/ MultiByteToWideChar,Linux下字符集转换可以使用iconv()函数,下面为使用boost的conv来 ...
- DOS的几个常用命令
1.rem:注释 DOS中的注释,其后面的内容会被自动忽略.双冒号(::)也有相同的效果 相当于R语言和Python中的# 2.set:设置变量 set var = 1 将1赋值给变量var 打印出来 ...
- android-基础编程-Dialog
Dialog是一种常见的控件. 设置对话框一般步骤如下: 1.实例化dialog 由于AlertDialog的构造函数的关系,不能直接实例化,需要利用Builder来实例化,如 AlertDialog ...
- (多线程dp)Matrix (hdu 2686)
http://acm.hdu.edu.cn/showproblem.php?pid=2686 Problem Description Yifenfei very like play a num ...
- (01背包 第k优解) Bone Collector II(hdu 2639)
http://acm.hdu.edu.cn/showproblem.php?pid=2639 Problem Description The title of this problem i ...
- (CodeForces 548B 暴力) Mike and Fun
http://codeforces.com/problemset/problem/548/B Mike and some bears are playing a game just for fun. ...
- POJ2456--Aggressive cows(Binary Search) unsolved
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- 对工程测量大师App的评价
我是测绘工程专业的学生,去年由于同学的推荐我开始使用工程测量大师这款App. 工程测量大师是一款非常方便的测量计算软件,用户将测量的数据输入后,软件就可以帮助你得到精确的计算结果.操作非常简单,使用起 ...
- SpringMVC随笔之——@responsebody【引用snowolf博文】
首先说RESTful 风格是什么 :(RESTful 风格:把请求参数变成请求路径的一种风格.) OK,一句话总结完毕 @responsebody表示该方法的返回结果直接写入HTTP response ...
- JVM的參數
博客:https://www.cnblogs.com/redcreen/archive/2011/05/04/2037057.html#CMSInitiatingOccupancyFraction_v ...