Using the ModelHandler class

The model handler helps map the UI components directly to ConsumedResult objects. The model handler is designed to create a two way bind between UI components and a model. Individual UI components can be mapped to each individual field and the list error messages for a field. This class is provided with the ConsumeAPI.UI.Simple nuget. It can be extended to configure how the different UI components need to be mapped the fields. The Xamarin.Forms.Consume.Simple provides a model handler that works out of the box with xamarin forms. This class contains a Link() function that can map Xamarin.Form's Entry fields to the fields of a model and maps a StackLayout for the error messages of the field.

To use this concept, you will first need to create a new model handler object (lets say mapped to Person class).

var handler = new ModelHandler()

Now this can be mapped to existing fields. Here is an example using the ModelHandler provided by Xamarin.Forms.Consume.Simple

    handler.Link(p => p.FirstName, FNameTextField, FNameErrStackLayout);
    handler.Link(p => p.LastName, LNameTextField, LNameErrStackLayout);

Once all fields are mapped, handler.Model can be used to get or set the model. The Submit() functions can access and handle the ConsumedResult objects.

    await handler.SubmitAsync(
            async model => RunSomething(model),    // lamda that returns the ConsumedResult object. 
                                                  // This is mosty used to make a http call via the Consumer
            s => DoThisOnSuccess(s)               // The function to run on success. S is the data returned by http

In the above function, if the http request fails with a 400, it automatically populates the errors in the stack layouts.