Thursday, September 19, 2013

The other day I spoke a little about some of the features included in Csla.Axml.Android.  Another feature it gives you is an easy way to pass a copy of a CSLA object between activities without doing a lot of work.

As you may know when moving from one activity to another in android you cannot simply pass an object by reference.  To get around this you can store a reference of your object in a static variable but global type variables are generally considered anti-patterns.  A better approach is to serialize the object and receive a copy of it in the target activity.  This can be done a variety of ways such as implementing Java.IO.ISerializable, json serialization, etc.  Doing this can lead to varying levels of work on your part.

To serialize and send a copy on a CSLA object from an Activity that inherits from the Csla ActivityBase, use the SeirializeModelForParameter method:

var projectResourceListActivity = new Intent(this, typeof(ProjectResourceList));
projectResourceListActivity.PutExtra(Constants.EditParameter, this.SerilizeModelForParameter());
StartActivity(projectResourceListActivity);


This method takes whatever is in the current view model's model property and serializes it using the mobile formatter.  If you need to send along a different object you can simply pass a reference to it as a parameter in the SerializeModelForParameter(objectIWantToSerialize) command instead.

To reconstitute the object on the target activity you can use a different method on the activity, DeserializeFromParameter.  A good place to do that might be in the activity's OnCreate event:

Library.ProjectEdit projectEdit;
var serializedProjectEdit = Intent.GetByteArrayExtra(Constants.EditParameter);
if (serializedProjectEdit != null)
{
    projectEdit = (Library.ProjectEdit)this.DeserializeFromParameter(serializedProjectEdit);
}

That's all their is too it!

Monday, September 16, 2013

CSLA Xamarin Android

As some of you may have heard, Rocky Lhotka, creator of the CSLA framework has been beset by some rather serious medical issues.  We've known each other for quite some time and it's really quite disconcerting to see someone who has always had boundless professional energy to be out of action for a while.  I'm sure it's quite a bit more disconcerting to him as he's a person that is used to being constantly active and working.

He was about to release a new version of CSLA that included a working port to Xamarin Android.  I had been working on that on and off for quite some time.  The version that is currently in the CSLA GitHub fork is currently has the core portions working in the master branch.  That can be found here:

Official CSLA Github Fork

The version in my fork has some updated UX helpers to extend the default activity and add a level of data binding.  Currently the data binding is still rudimentary but effective.  This can be found here:

Latest CSLA Xamarin Android Source

The UX helpers can be found in the Csla.Axml dll.  The following are some of the things that can be done with it:

Like other CSLA UX helpers there is a view model base that you can use to reshape your CSLA models in a way that is appropriate for a particular view.  These ViewModels expose out an underlying Model property that refers to a CSLA model and allows you to extend and add references to other models as needed.  The syntax to create such a class is as so:

public class ProjectResourceEdit : Csla.Axml.ViewModel<Library.ProjectResourceEdit>

This will strongly type the Library.ProjectResourceEdit class to the generic parameter of the ViewModel.  As expected any calls to ProjectResourceEdit.Model will be of type Library.ProjectResourceEdit.  A wide range of the capabilities you find with the CSLA ViewModel for other UX technologies will also be available with the CSLA Xamarin Android ViewModel.

Another abstract class you can use from the Csla.Axml dll is the ActionBase.  This will strongly type your action to a CSLA view model you have created as above.  It will also give you limited data binding abilities.  To use the ActivityBase simply inherit from it instead of the standard Activity class.  Additionally, provide type information for the ViewModel and associated Model you would like to use.

public class ProjectResourceEdit : ActivityBase<ViewModels.ProjectResourceEdit, Library.ProjectResourceEdit>

Currently activation of the data binding can be done manually in the model, though I have future plans of allowing it to be set directly in the axml.  To bind up a control simply use the following code:

this.Bindings.Add(Resource.Id.txtResourceAssigned, "Text", this.viewModel.Model, "Assigned");

This code assigns the Text property of the TextView with the id of txtResourceAssigned to the associated model's Assigned property.  This property will be initially displayed in the text view and updated back to the Assigned property when the txtResourceAssigned TextView looses focus. Additionally bindings can be created with references to custom functions that can coerce/convert values back and forth from your model/view model to a control on the axml layout.

When moving from a view to move to another control such as clicking a button the binding is sometimes not updated as the original control still has focus.  This is particularly problematic for save buttons.  To force bindings to be updated use the following method in the click event for your save button:

this.Bindings.UpdateSourceForLastView();

Finally,  when getting back a new reference to your model from the data portal you may need to clear your bindings and update them to point to the new instance of your model object.  The bindings can be cleared simply by making the following call:

this.Bindings.RemoveAll();

That's it for today.  In the next post I will discuss how the UX helpers can assist you in passing your model from one activity to another as you cannot pass models by reference between views.