Xamarin.Android MVP模式
一、简介
随着UI创建技术的功能日益增强,UI层也履行着越来越多的职责。为了更好地细分视图(View)与模型(Model)的功能,让View专注于处理数 据的可视化以及与用户的交互,同时让Model只关系数据的处理,基于MVC概念的MVP(Model-View-Presenter)模式应运而生。
在MVP模式里通常包含4个要素:
二、实例
1.新建一个项目,项目结构如下

2.做一个根据id读取数据的实例,界面如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入id"
android:inputType="number" />
<EditText
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="inter first name"
android:inputType="text" />
<EditText
android:id="@+id/last"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="inter last name"
android:inputType="text" />
<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="save" />
<Button
android:id="@+id/load"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="load" />
</LinearLayout>
3.建立bean
public class UserBean
{
private String mFirstName { get; set; }
private String mLastName { get; set; } public UserBean(String firstName, String lastName)
{
this.mFirstName = firstName;
this.mLastName = lastName;
}
}
4.建立model(处理业务逻辑),先写接口,后写实现
public interface IUserModel
{
void SetID(int id); void SetFirstName(String firstName); void SetLastName(String lastName); int GetID(); UserBean Load(int id);// 通过id读取user信息,返回一个UserBean
}
public class UserModel : IUserModel
{
public int GetID()
{
return ;
} public UserBean Load(int id)
{
// 查数据库或者联网获取id的userbean
return new UserBean("", "");
} public void SetFirstName(string firstName)
{ } public void SetID(int id)
{ } public void SetLastName(string lastName)
{ }
}
5.建立view(更新ui中view状态),这里列出需要操作当前view的方法,也就是接口。
public interface IUserView
{
int GetID(); String GetFristName(); String GetLastName(); void SetFirstName(String firstName); void SetLastName(String lastName);
}
6.建立presenter(主导器,通过ivew和imodel接口操作model和view),activity可以把所有逻辑给presenter处理
public class UserPresenter
{
private IUserView mUserView;
private IUserModel mUserModel; public UserPresenter(IUserView view)
{
mUserView = view;
mUserModel = new UserModel();
} public void SaveUser(int id, String firstName, String lastName)
{
mUserModel.SetID(id);
mUserModel.SetFirstName(firstName);
mUserModel.SetLastName(lastName);
} public void LoadUser(int id)
{
UserBean user = mUserModel.Load(id);
mUserView.SetFirstName(user.mFirstName); // 通过调用IUserView的方法来更新显示
mUserView.SetLastName(user.mLastName);
}
}
7.activity中实现iview接口,在其中操作view,实例化一个presenter变量。
public class MainActivity : Activity, IUserView, View.IOnClickListener
{
UserPresenter presenter;
EditText id, first, last;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle); // Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main); // Get our button from the layout resource,
// and attach an event to it
FindViewById<Button>(Resource.Id.save).SetOnClickListener(this);
FindViewById<Button>(Resource.Id.load).SetOnClickListener(this); ; id = FindViewById<EditText>(Resource.Id.id);
first = FindViewById<EditText>(Resource.Id.first);
last = FindViewById<EditText>(Resource.Id.last);
presenter = new UserPresenter(this);
} public int GetID()
{
return Convert.ToInt32(id.Text.ToString());
} public string GetFristName()
{
return first.Text.ToString();
} public string GetLastName()
{
return last.Text.ToString();
} public void SetFirstName(string firstName)
{
first.Text = firstName;
} public void SetLastName(string lastName)
{
last.Text = lastName;
} public void OnClick(View v)
{
switch (v.Id)
{
case Resource.Id.save:
presenter.SaveUser(GetID(), GetFristName(), GetLastName());
break;
case Resource.Id.load:
presenter.LoadUser(GetID());
break;
}
}
}
7.源码地址
https://github.com/huguodong/XamarinMVP
里面又更详细的实例
Xamarin.Android MVP模式的更多相关文章
- android MVP模式介绍与实战
android MVP模式介绍与实战 描述 MVP模式是什么?MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数 ...
- Android MVP模式
转自http://segmentfault.com/blogs,转载请注明出处Android MVP Pattern Android MVP模式\[1\]也不是什么新鲜的东西了,我在自己的项目里也普遍 ...
- Android MVP模式 简单易懂的介绍方式
主要学习这位大神的博客:简而易懂 Android MVP模式 简单易懂的介绍方式 https://segmentfault.com/a/1190000003927200
- Android MVP模式简单易懂的介绍方式 (三)
Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 讲完M和P,接下来就要讲V了.View ...
- Android MVP模式简单易懂的介绍方式 (二)
Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 上一篇文章我们介绍完了Model的创建 ...
- Android MVP模式简单易懂的介绍方式 (一)
Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 最近正在研究Android的MVP模式 ...
- Android MVP模式 谷歌官方代码解读
Google官方MVP Sample代码解读 关于Android程序的构架, 当前(2016.10)最流行的模式即为MVP模式, Google官方提供了Sample代码来展示这种模式的用法. Repo ...
- Android mvp模式、mvvm模式
MVC和MVP的区别2007年08月08日 星期三 上午 09:23 MVC和MVP到底有什么区别呢? 从这幅图可以看到,我们可以看到在MVC里,View是可以直接访问Model的!从而,View里会 ...
- Android MVP模式就是这么回事儿
MVP模式 概念就不说了,大家都懂.View层通过Persenter层相互通信,避免了View中大量的业务代码,而将其提取到Model中.其实简单的来说,就是通过接口回调,把业务分离出去.提高代码的可 ...
随机推荐
- Android编程: 环境搭建、基本知识
学习的内容两个方面:环境搭建.基本知识 ====环境搭建==== 1.下载 android studio(http://developer.android.com/sdk/index.html) 2. ...
- xml之Schema架构
1.什么是Schema架构 2.Schema文档结构 3.Schema元素类型 1>element元素 <!--简单数据:类型--> <xs:element name=&qu ...
- “我爱淘”冲刺阶段Scrum站立会议3
完成任务: 将搜索框的界面已经实现以及部署到整个框架中. 计划任务: 实现搜索功能,通过数据库的链接,实现用户可以查到自己需要的书籍的信息. 遇到问题: 1.数据库的操作,怎么实现查询功能: 2.Ac ...
- 使用cronolog切割tomcat catalina.out文件
今天查看之前配置的tomcat发现catalina.out文件已经增大到接近5G,过不了多久就会将所在分区撑爆. 搜集了一下,大部分都使用cronolog切割catalina.out文件.按照这个方式 ...
- 这个好像、也许、或许、大概、应该、Maybe真的可以算是传说中的Spring.Net了吧
这个好像.也许.或许.大概.应该.Maybe真的可以算是传说中的Spring.Net了吧 ...
- Using sql azure for Elmah
The MSDN docs contain the list of T-SQL that is either partially supported or not supported. For ex ...
- modernizr.js
1.判断浏览器是否支持 h5 if(Modernizr.canvas){ alert(123); }else{ alert(321); } 2.判断浏览器是否支持 canvas function su ...
- BZOJ2039 [2009国家集训队]employ人员雇佣
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=2039 鉴于一开始看题如果不仔细是看不懂题目的,还是说一下题目大意 [题目大意]:给定n个人 ...
- 【BZOJ】【1012】【JSOI2008】最大数maxnumber
线段树 ……现在再来看这题感觉好水啊,当年的大老虎现在也变成小花猫了,真是令人感动<_< /************************************************ ...
- CocoaPods 使用手册
CocoaPods 使用手册 CocoaPods 使用手册 ...