Quantcast
Channel: User Liyun Zhang - MSFT - Stack Overflow
Viewing all articles
Browse latest Browse all 444

Answer by Liyun Zhang - MSFT for How to launch a Xamarin forms from Android Activity

$
0
0

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.

  1. Install the Xamarin.Forms nuget package in your project. I installed the newest version and updated the Xamarin.Google.Android.Material nuget package to the newest version.
  2. 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.

  1. Load the Xamarin.Forms.Application in the FormsAppCompatActivity:
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());        }    }}
  1. 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:

enter image description here


Viewing all articles
Browse latest Browse all 444

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>