Skip to content

Instantly share code, notes, and snippets.

@rid00z
Last active January 24, 2016 08:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rid00z/b6afddffcf961c437cb7 to your computer and use it in GitHub Desktop.
Save rid00z/b6afddffcf961c437cb7 to your computer and use it in GitHub Desktop.
A mini Mvvm for Xamarin.Forms
public abstract class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public virtual void ReverseInit(object value) { }
protected void PushViewModel<T> () where T : BaseViewModel
{
PushViewModel<T> (null);
}
public static Page ResolveViewModel<T>(Dictionary<string, string> data)
where T : BaseViewModel
{
var viewModel = IOC.Current.Resolve<T>();
return ResolveViewModel<T>(data, viewModel);
}
public static Page ResolveViewModel<T>(Dictionary<string, string> data, BaseViewModel viewModel)
where T : BaseViewModel
{
var name = typeof(T).Name.Replace ("Model", string.Empty);
var pageType = Type.GetType ("App.Pages." + name);
var page = (Page)IOC.Current.Resolve (pageType);
page.BindingContext = viewModel;
var initMethod = TinyIoC.TypeExtensions.GetMethod (typeof(T), "Init");
if (initMethod != null) {
if (initMethod.GetParameters ().Length > 0)
{
if (data == null)
data = new Dictionary<string, string> ();
initMethod.Invoke (viewModel, new object[] { data });
}
else
initMethod.Invoke (viewModel, null);
}
var vmProperty = TinyIoC.TypeExtensions.GetProperty(pageType, "ViewModel");
if (vmProperty != null)
vmProperty.SetValue (page, viewModel);
var vmPageBindingContext = TinyIoC.TypeExtensions.GetProperty(pageType, "BindingContext");
if (vmPageBindingContext != null)
vmPageBindingContext.SetValue (page, viewModel);
var initMethodPage = TinyIoC.TypeExtensions.GetMethod (pageType, "Init");
if (initMethodPage != null)
initMethodPage.Invoke (page, null);
return page;
}
protected void PushViewModel<T> (Dictionary<string, string> data) where T : BaseViewModel
{
BaseViewModel viewModel = IOC.Current.Resolve<T>();;
var page = ResolveViewModel<T> (data, viewModel);
viewModel.PreviousViewModel = this;
ITabbedNavigation tabbedNav = IOC.Current.Resolve<ITabbedNavigation> ();
tabbedNav.PushView (viewModel, page);
}
protected void PopViewModel()
{
ITabbedNavigation tabbedNav = IOC.Current.Resolve<ITabbedNavigation> ();
tabbedNav.PopView ();
}
protected void PopViewModel(object data)
{
if (PreviousViewModel != null && data != null) {
var initMethod = PreviousViewModel.GetType().GetRuntimeMethod ("ReverseInit", new Type[] { PreviousViewModel.GetType() });
if (initMethod != null) {
initMethod.Invoke (PreviousViewModel, new object[] { data });
}
}
ITabbedNavigation tabbedNav = IOC.Current.Resolve<ITabbedNavigation> ();
tabbedNav.PopView ();
}
protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment