Xamarin如何从android项目打开Xamarin表单页面?

我想从Xamarin Android项目打开Xamarin表单页面.在android项目上,我创建了toolabar项目图片,我在其中调用event从Xamarin表单项目打开页面.

这是我的MainActivity.cs toolabar图片项目实现:

    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);
            }
}

在StartActivity中,我从Xamarin表单项目调用MyPopupPage.xaml页面,但是不幸的是,当我调试项目并单击工具栏图像时,出现这样的错误:

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

解决方法:

您不能将基于Xamarin.Form的Page用作Android Activity,因为它们是完全不同的两件事.

您可以从Xamarin.Android项目访问Xamarin.Forms应用程序单例,并将其用于PushModelAsync或PushAsync

示例(使用完整的命名空间):

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

基于依赖服务的示例:

接口:

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

基于Xamarin.Form的代码:

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

Xamarin.Android依赖实现:

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());
}
上一篇:c#-Task.Run()有什么区别,在Xamarin表单上调用异步void AAA()?


下一篇:Ca-Xamarin Forms从PCL请求互联网许可Android 6