Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

dornerworks/Mvx.Forms.PageWrapper

Repository files navigation

Mvx.Forms.PageWrapper

Xamarin.Forms Page wrappers for MvvmCross the Android Fragment/Activity and the iOS ViewController

Inspired by Alex Dunn's blog posts:

Introduction

Wraps a Xamarin.Forms Page inside a MvvmCross view. This allows for "native" navigation via MvvmCross. The result is that you can easily navigate between a native view and a embedded Xamarin.Forms page.

How To Use

  1. Create your Xamarin.Forms page by subclassing MvxEmbeddedContentPage.

2a. In your Android project, create a MvxFormsActivity or MvxFormsFragment to wrap your Xamarin.Forms page.

public class MyFormsFragment : MvxFormsFragment<MyPage, MyViewModel>
{
  protected override void OnCreate(Bundle savedInstanceState)
  {
    base.OnCreate(savedInstanceState);
    Page.BackgroundColor = Color.Red;
    ...
  }
}

2b. In your iOS project, create a MvxFormsViewController to wrap your Xamarin.Forms page.

public class MyFormsViewController : FormsViewController<MyPage, MyViewModel>
{
  public override void ViewDidLoad()
  {
    base.ViewDidLoad();
    Page.BackgroundColor = Color.Red;
    ...
  }
}
  1. You can navigate to the ViewModel as your normall would in MvvmCross. The Xamarin.Forms page will be embedded inside the activity/fragment. Binding works as expected in both the native view and content page.

Customization

You can override the CreatePage method to handle the construction of the page.

protected override MyPage CreatePage()
{
  return new MyPage(paramter1, parameter2)
  {
    ViewModel = ViewModel
  };
}

Android

You can override the Frame that the MvxEmbeddedContentPage is embedded into.

protected override UIView FragmentLayoutId()
{
  return Resource.Id.some_other_frame;
}

iOS

You can override the UIView that the MvxEmbeddedContentPage is embedded into.

protected override UIView PageContainerView()
{
  return _someOtherView;
}

Known Limitations

  • Toolbars defined in Xamarin.Forms are ignored.