Skip to content

Instantly share code, notes, and snippets.

@jamesmontemagno
Created August 23, 2013 19:36
Show Gist options
  • Save jamesmontemagno/6323125 to your computer and use it in GitHub Desktop.
Save jamesmontemagno/6323125 to your computer and use it in GitHub Desktop.
MvvmCross Implementation of UIRefreshControl that you can use inside of TableViews or CollectionViews
using System;
using MonoTouch.UIKit;
using System.Windows.Input;
namespace com.refractored.mvxbindings
{
/// <summary>
/// Mvx user interface refresh control.
/// </summary>
public class MvxUIRefreshControl : UIRefreshControl
{
/// <summary>
/// Initializes a new instance of the <see cref="MvxUIRefreshControl"/> class.
/// </summary>
public MvxUIRefreshControl()
{
this.ValueChanged += (object sender, EventArgs e) =>
{
var command = RefreshCommand;
if(command == null)
return;
command.Execute(null);
};
}
private string _message;
/// <summary>
/// Gets or sets the message to display
/// </summary>
/// <value>The message.</value>
public string Message
{
get { return _message;}
set { _message = value ?? string.Empty;
this.AttributedTitle = new MonoTouch.Foundation.NSAttributedString(_message);}
}
private bool _isRefreshing;
/// <summary>
/// Gets or sets a value indicating whether this instance is refreshing.
/// </summary>
/// <value><c>true</c> if this instance is refreshing; otherwise, <c>false</c>.</value>
public bool IsRefreshing
{
get { return _isRefreshing;}
set
{
_isRefreshing = value;
if (_isRefreshing)
BeginRefreshing();
else
EndRefreshing();
}
}
/// <summary>
/// Gets or sets the refresh command.
/// </summary>
/// <value>The refresh command.</value>
public ICommand RefreshCommand { get; set;}
}
}
In your ViewDidLoad() simply do this:
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);
set.Apply();
In this instance MyViewModel has the following:
private bool m_IsBusy;
public bool IsBusy
{
get { return m_IsBusy; }
set { m_IsBusy = value; RaisePropertyChanged(() => IsBusy);
BusyMessage = m_IsBusy ? "Refreshing..." : string.Empty; }
}
private string m_BusyMessage = string.Empty;
/// <summary>
/// Gets or sets the subtitle for the base model
/// </summary>
public string BusyMessage
{
get {return m_BusyMessage; }
set { m_BusyMessage = value; RaisePropertyChanged(() => BusyMessage); }
}
private MvxCommand m_ReloadCommand;
public ICommand ReloadCommand
{
get
{
return m_ReloadCommand ?? (m_ReloadCommand = new MvxCommand(ExecuteReloadCommand));
}
}
private void ExecuteReloadCommand()
{
IsBusy = true;
//do stuff
IsBusy = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment