Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
794 views
in Technique[技术] by (71.8m points)

c# - Xamarin how to open xamarin forms page from android project?

I want to open Xamarin forms page from Xamarin Android project. On android project I created toolabar item image, where I am calling event to open page from Xamarin forms project.

Here is my MainActivity.cs toolabar image item implementation:

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
      private IMenu CurrentMenu { get; set; }
      private ImageView imgSmallC { get; set; }

      public override bool OnCreateOptionsMenu(IMenu menu)
            {
               ActionBar.DisplayOptions = ActionBarDisplayOptions.HomeAsUp | ActionBarDisplayOptions.ShowCustom | ActionBarDisplayOptions.ShowTitle | ActionBarDisplayOptions.ShowHome;  
                LayoutInflater inflater = (LayoutInflater)ActionBar.ThemedContext.GetSystemService(LayoutInflaterService);
                View customActionBarView = inflater.Inflate(Resource.Layout.actionbar_custom_view_done, null);  

                imgSmallC = (ImageView)customActionBarView.FindViewById<ImageView>(Resource.Id.ImgSmallC);

                imgSmallC.Click += (object sender, EventArgs args) =>
                {
                    StartActivity(typeof(MyPopupPage));
                };
                return base.OnCreateOptionsMenu(menu);
            }
}

In StartActivity I am calling MyPopupPage.xaml page from Xamarin forms project, but unfortunately when I am debugging project and I click on toolbar image I get such a error:

System.ArgumentException: type Parameter name: Type is not derived from a java type.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can not use a Xamarin.Form based Page as an Android Activity, they are two completely different things.

You can access the Xamarin.Forms Application singleton from the Xamarin.Android project and use that to PushModelAsync or PushAsync

Example (using full Namespace):

    await Xamarin.Forms.Application.Current.MainPage.Navigation.PushModalAsync(new PushPageFromNative.MyPage());

A Dependency Service-based Example:

Interface:

using System;
namespace PushPageFromNative
{
    public interface IShowForm
    {
        void PushPage();
    }
}

Xamarin.Form-based code:

var pushFormBtn = new Button
{
    Text = "Push Form",
    VerticalOptions = LayoutOptions.CenterAndExpand,
    HorizontalOptions = LayoutOptions.CenterAndExpand,
};
pushFormBtn.Clicked += (sender, e) =>
{
        DependencyService.Get<IShowForm>().PushPage();
};

Xamarin.Android Dependancy Implementation:

async public void PushPage()
{
    // Do some Android specific things... and then push a new Forms' Page
    await Xamarin.Forms.Application.Current.MainPage.Navigation.PushModalAsync(new PushPageFromNative.MyPage());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...