Friday 15 July 2011

xamarin - How to do validation using mvvmcross -



xamarin - How to do validation using mvvmcross -

i have started learning write mobile apps using xamarin , mvvmcross. have found quite easy pick basics due great back upwards including n+1 days of mvvmcross videos on youtube (huge stuart lodge).

however struggling valudation data. i'm hoping on stackoverflow can point me in direction of useful blogs or tutorials on performing validation using mvvmcross. want able validate info entered , update view indicating issue.

i need first principles don't know don't know (if makes sense). need best practice follow.

data validation can displayed in ui in different ways. example, can show message box or show label.

suppose want have label reddish text somewhere in ui show error.

i assume have 'save' button or similar in ui.

you can bind button savecommand in view-model.

in implementation of savecommand, can check if info valid , set error string property.

you can have label's text bound error property. moreover, bind label's visibility status (error != null).

public class settingsviewmodel : mvxviewmodel { string firstname; public string firstname { { homecoming this.firstname; } set { if(this.firstname != value) { this.firstname = value; this.raisepropertychanged(()=> this.firstname); this.error = null; // reset error } } } public string error { get; private set; } public icommand savecommand { { homecoming new mvxcommand(this.save); } } void save() { // reset error this.error = null; if(string.isnullorempty(this.firstname)) { this.error = "first name empty"; } if(string.isnulloremtpy(this.error)) { // no error, save settings... } else { this.raisepropertychanged(()=> this.error); } } }

validation xamarin mvvmcross

No comments:

Post a Comment