First of all, I don't suggest you use the xaml in the xamarin.android project. It should be used in the cross platform project.
In the xamarin.android project, if you want show the contentpage, you can use refer to the xamarin.forms project and use the FormsAppCompatActivity
to render the contentpage.
- Install the
Xamarin.Forms
nuget package in your project. I installed the newest version and updated theXamarin.Google.Android.Material
nuget package to the newest version. - Create
Xamarin.Forms.Application
in the project
The App.xaml:
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="App3.App"><Application.Resources></Application.Resources></Application>
The App.cs:
using Xamarin.Forms.Xaml;namespace App3{ [XamlCompilation(XamlCompilationOptions.Compile)] public partial class App : Xamarin.Forms.Application { public App() { InitializeComponent(); MainPage = new Page1(); } }}
Note: When I created the app.cs, it told me that the MainPage and resource can't be found. Close and reopen the project will resolve this.
- Load the
Xamarin.Forms.Application
in theFormsAppCompatActivity
:
using Xamarin.Forms.Platform.Android;namespace App3{ [Activity(Label = "Activity1",Theme = "@style/MainTheme.Base")] public class Activity1 : FormsAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); } }}
- Go to the contentpage in the MainActivity:
Button button = FindViewById<Button>(Resource.Id.button1);button.Click += delegate{ var intent = new Intent(this, typeof(Activity1)); StartActivity(intent); };
And my project's structure: