James Montemagno
James Montemagno

Live, Love, Bike, and Code.



MvxUIRefreshControl for #MvvmCross

James MontemagnoJames Montemagno

Implementing MvxTableViews and MvxCollectionViews are extremely simple. From binding to accessory or cell clicks, to implementing advanced logic for custom cells they just plain work in MvvmCross. However, today I ran into one issue that needed to be solved, an Mvx binding for UIRefreshControl. <br./></br.>

In iOS6+ land adding pull to refresh is EXTREMELY simple. You can read all about it, but basically you need to just implement 1 method and you can even set a custom string and style it. It is beautiful, however there was no way to bind anything in MvvmCross. So I wanted to create a simple binding to accomplish a few goals:

1.) Execute an ICommand when someone pulls down to refresh

2.) Bind a bool to begin and end the refreshing spinner

3.) Bind a string to set the AttributedTitle (what is displayed to the user) when they pull or when it refreshes.

So I created a very very simple UIRefreshControl, which you can find on Gist RIGHT HERE

The code is as simple to implement as:

var refreshControl = new MvxUIRefreshControl();
this.RefreshControl = refreshControl;

var set = this.CreateBindingSet<MyView, MyViewModel>();
set.Bind(refreshControl).For(r => r.IsRefreshing).To(vm => vm.IsBusy);
set.Bind(refreshControl).For(r => r.RefreshCommand).To(vm => vm.ReloadCommand);
set.Bind(refreshControl).For(r => r.Message).To(vm => vm.BusyMessage);

I hope this helps

Live, Love, Bike, and Code