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中.其实简单的来说,就是通过接口回调,把业务分离出去.提高代码的可 ...
随机推荐
- 4.FPGA芯片管脚解释
用户I/O:不用解释了. 配置管脚: MSEL[1:0] 用于选择配置模式,比如AS.PS等. DATA0 FPGA串行数据输入,连接到配置器件的串行数据输出管脚. DCLK FPGA串行时钟输出 ...
- Log4Net学习【三】
Log4Net配置详解 配置方式一 在相应的应用程序的配置文件中配置,(WinForm对应的是*.exe.config,WebForm对应的是*.config),本实例是Web应用程序,以Web.co ...
- 设计模式 -- 单例模式(Java&&PHP)
所谓单例模式,简单来说,就是在整个应用中保证只有一个类的实例存在.就像是Java Web中的application,也就是提供了一个全局变量,用处相当广泛,比如保存全局数据,实现全局性的操作等. 能够 ...
- android 开发怎么让程序生成的图片文件不会被系统扫描到
我们在写应用的时候,可能会保存很多图片,大的小的,仅仅是我们的应用中会用到,处于种种原因不希望用户看到,我是觉着如果被用户看到了,就失去了我的应用的那一层神秘的面纱,用户是米有闲情逸致去打开你一层层的 ...
- NSIS中判断.net framework 是否安装
在网上找了几个脚本,原理都是读取注册表进行判断,都可以用. 1.CheckDotNetFramework /* * Name: CheckDotNetFramework.nsh * Version: ...
- Windows下配置使用MemCached
工具: memcached-1.2.6-win32-bin.zip MemCached服务端程序(for win) Memcached Manager win下的Mem ...
- SGU 185 Two shortest 最短路+最大流
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21068 Yesterday Vasya and Petya qua ...
- lua与 object-C 通信
IOS中如何调用LUA,以及LUA如何调用IOS中的功能 下面将讲解一下如何在iOS里调用Lua函数,以及Lua函数如何调用iOS本地函数. 转载请注明出处.原文出处 http://www.cnblo ...
- ios读取通讯录信息
ios读取通讯录信息 (2012-05-22 14:07:11) 标签: ios读取通讯录 it iphone如许app读取通讯录信息,读取通讯录信息时需要加载AddressBookUI 和Add ...
- JavaScript之Cookie讲解
什么是 Cookie “cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 来创建和取回 cookie ...