Xamarin 如何从 android 项目打开 xamarin 表单页面? [英] Xamarin how to open xamarin forms page from android project?

查看:35
本文介绍了Xamarin 如何从 android 项目打开 xamarin 表单页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Xamarin Android 项目打开 Xamarin 表单页面.在 android 项目中,我创建了工具栏项目图像,我在其中调用事件以从 Xamarin 表单项目中打开页面.

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.

这是我的 MainActivity.cs 工具栏图片项实现:

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

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

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:类型参数名称:类型不是派生的来自 java 类型.

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

推荐答案

你不能使用基于 Xamarin.FormPage 作为 Android Activity,它们是两个完全不同的东西.

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

您可以从 Xamarin.Android 项目访问 Xamarin.Forms Application 单例,并将其用于 PushModelAsyncPushAsync

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

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

基于依赖服务的示例:

界面:

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

基于 Xamarin.Form 的代码:

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 依赖实现:

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

这篇关于Xamarin 如何从 android 项目打开 xamarin 表单页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆