隨着UI建立技術的功能日益加強,UI層也履行着愈來愈多的職責。爲了更好地細分視圖(View)與模型(Model)的功能,讓View專一於處理數 據的可視化以及與用戶的交互,同時讓Model只關係數據的處理,基於MVC概念的MVP(Model-View-Presenter)模式應運而生。android
在MVP模式裏一般包含4個要素:git
1.新建一個項目,項目結構以下github
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.創建beaniview
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(處理業務邏輯),先寫接口,後寫實現ide
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 0; } public UserBean Load(int id) { // 查數據庫或者聯網獲取id的userbean return new UserBean("11", "22"); } 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變量。ui
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.源碼地址this
https://github.com/huguodong/XamarinMVP
裏面又更詳細的實例