4
votes

I'm trying to add a Xamarin.Forms page to an existing Xamarin Android app.

I'm struggling to figure out how to mix native Xamarin Android pages and Xamarin.Forms pages.

All the articles and examples I have seen describe either Forms or Xamarin apps and not a hybrid of the two.

The best idea I have read about so far is to add a native stub and replace the content with Xamarin.Forms content but I'm not sure how to achieve this.

Update

I'm using MvvmCross so my android view is inherited from MvxActivity. I have a StackLayout containing views using Xamarin.Forms. How do I get XF to convert to android code so I can use with SetContentView?

1

1 Answers

2
votes

When you create a new Xamarin.Forms project, for instance with a SharedProject approach, it will create the launcher project and initial class file that will be executed for you for each platform.

For instance on Android this is in MainActivity.cs. If you look inside this file you will notice that it inherits from AndroidActivity which in turn inherits from Activity, which is the typical class used on activities.

So you have two options available for you to create a mixed hybrid solution:-

1) Inherit from AndroidActivity in your platform-specifc stub-page should you want to display Xamarin.Forms content, or alternatively

2) Inherit from Activity, and do the same as usual.

If you want to display a Xamarin.Forms page from the stub-page you can then just use some boiler-plate code such as the following in the OnCreate method:-

 SetPage({some class that returns a Xamarin.Forms.Page object, i.e. ContentPage});

You can then use the normal navigation that is specific to Android to either display a Xamarin.Forms page or a platform-specific Activity page.