Xamarin.android MVP Mode

Source: Internet
Author: User

First, Introduction

As UI creation technology becomes more and more powerful, the UI layer is performing more and more responsibilities. In order to better subdivide the view and model functions, let view focus on processing data visualization and interaction with the user, while the model only relational data processing, based on the MVC concept of the MVP (Model-view-presenter) model emerged.

In MVP mode, there are usually 4 elements:

View: responsible for drawing UI elements, interacting with the user (embodied as activity in Android); View interface: an interface that requires a view implementation, the view interacts with the presenter through the view interface to reduce coupling and facilitate unit testing; Model: responsible for storing, retrieving, manipulating data (sometimes also implementing a Model interface to reduce coupling); Presenter: as the intermediate link between view and model, handles the responsible logic for interacting with the user. the so-called MVP, that is (model-processing business logic (mainly data read and write, or with background communication (in fact, read and write data), view-processing UI control, presenter-leader, Operation model and view)Second, examples

1. Create a new project with the following project structure

2. Do an instance of reading data based on ID, the interface is as follows

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns: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">    <EditTextAndroid:id= "@+id/id"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Input id"Android:inputtype= "Number" />    <EditTextAndroid:id= "@+id/first"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Inter First Name"Android:inputtype= "text" />    <EditTextAndroid:id= "@+id/last"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Inter Last Name"Android:inputtype= "text" />    <ButtonAndroid:id= "@+id/save"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Save" />    <ButtonAndroid:id= "@+id/load"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Load" /></LinearLayout>
View Code

3. Create a Bean

 public  class   UserBean { private  String mfirstname {get ; set          private  String mlastname {get ; Span style= "color: #0000ff;"                  >set  ;}  public   UserBean (String FirstName, String lastName { this . Mfirstname = FirstName;         this . Mlastname = LastName; }    }

4. Establish model (processing business logic), write interface, write implementation

     Public Interface Iusermodel    {        void SetID (int  ID);         void setfirstname (String firstName);         void setlastname (String lastName);         int GetID ();        UserBean Load (int id); // read user information by ID, return a userbean    }
  Public classUsermodel:iusermodel { Public intGetID () {return 0; }         PublicUserBean Load (intID) {//Check the database or the Internet to get the ID Userbean            return NewUserBean (" One"," A"); }         Public voidSetfirstname (stringfirstName) {        }         Public voidSetID (intID) {} Public voidSetlastname (stringlastName) {        }    }

5. Create a view (update the view state in the UI), which lists the methods that need to manipulate the current view, that is, the interface.

     Public Interface Iuserview    {        int  GetID ();        String getfristname ();        String getlastname ();         void setfirstname (String firstName);         void setlastname (String lastName);    }

6. Establish presenter (the main device, operate the model and view via the Ivew and Imodel interfaces), activity can give all logic to presenter processing

  Public classUserpresenter {PrivateIuserview Muserview; PrivateIusermodel Musermodel;  PublicUserpresenter (Iuserview view) {Muserview=view; Musermodel=NewUsermodel (); }         Public voidSaveuser (intID, String firstName, String lastName)            {Musermodel.setid (ID);            Musermodel.setfirstname (FirstName);        Musermodel.setlastname (LastName); }         Public voidLoaduser (intID) {UserBean user=musermodel.load (ID); Muserview.setfirstname (User.mfirstname); //update the display by calling the Iuserview methodMuserview.setlastname (user.mlastname); }    }

Implement the IView interface in 7.activity, where you manipulate view to instantiate a presenter variable.

  Public classmainactivity:activity, Iuserview, View.ionclicklistener {Userpresenter presenter;        EditText ID, first, last; protected Override voidOnCreate (Bundle bundle) {Base.            OnCreate (bundle); //Set Our view from the "main" layout resourceSetcontentview (Resource.Layout.Main); //Get Our buttons from the layout resource,//And attach an event to itFindviewbyid<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=NewUserpresenter ( This); }         Public intGetID () {returnConvert.ToInt32 (ID.        Text.tostring ()); }         Public stringGetfristname () {returnFirst .        Text.tostring (); }         Public stringGetlastname () {returnLast .        Text.tostring (); }         Public voidSetfirstname (stringfirstName) {first. Text=FirstName; }         Public voidSetlastname (stringlastName) {Last. Text=LastName; }         Public voidOnClick (View v) {Switch(v.id) { CaseResource.Id.save:presenter.                    Saveuser (GetID (), Getfristname (), Getlastname ());  Break;  CaseResource.Id.load:presenter.                    Loaduser (GetID ());  Break; }        }    }

7. Source Address

Https://github.com/huguodong/XamarinMVP

And a more detailed example.

Xamarin.android MVP Mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.