简体   繁体   English

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

[英]Xamarin how to open xamarin forms page from android project?

I want to open Xamarin forms page from Xamarin Android project. 我想从Xamarin Android项目打开Xamarin表单页面。 On android project I created toolabar item image, where I am calling event to open page from Xamarin forms project. 在android项目中,我创建了太棒项目图像,我在这里调用事件从Xamarin表单项目打开页面。

Here is my MainActivity.cs toolabar image item implementation: 这是我的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);
            }
}

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: 在StartActivity中,我从Xamarin表单项目调用MyPopupPage.xaml页面,但不幸的是,当我调试项目时,我点击工具栏图像时出现这样的错误:

System.ArgumentException: type Parameter name: Type is not derived from a java type. System.ArgumentException:type参数名称:Type不是从java类型派生的。

You can not use a Xamarin.Form based Page as an Android Activity , they are two completely different things . 你不能使用基于Xamarin.FormPage作为Android Activity ,它们是两个完全不同的东西

You can access the Xamarin.Forms Application singleton from the Xamarin.Android project and use that to PushModelAsync or PushAsync 您可以从Xamarin.Android项目访问Xamarin.Forms Application单例并将其用于PushModelAsyncPushAsync

Example (using full Namespace): 示例(使用完整的命名空间):

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

A Dependency Service -based Example: 基于Dependency Service的示例:

Interface: 接口:

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

Xamarin.Form -based code: 基于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 Dependancy Implementation: Xamarin.Android Dependancy实施:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以 xamarin 形式从客户端项目(IOS/Android)将一个内容页面导航到另一个内容页面? - How to navigate one Content page to another Content page from client project (IOS/Android) in xamarin forms? Xamarin 在 Android 项目中打开内容页面 - Xamarin Open Content Page In Project Android 如何通过onclick侦听器的Xamarin Droid活动打开Xamarin表单页面 - How to open a Xamarin Forms page from a Xamarin Droid Activity from a onclick listener 将项目从Xamarin.Android传输到Xamarin.Forms - Transfer project from Xamarin.Android to Xamarin.Forms 如何从 Xamarin 项目中的跨平台页面打开和关闭 Android 活动 - How to open and close Android activity from cross-platform page in Xamarin project 在Xamarin.forms项目中使用Xamarin Android dll - Use Xamarin Android dll in Xamarin forms project hybridWebView 页面从 Xamarin.Forms 中的 Javascript 打开另一个 ContentPage - hybridWebView page open another ContentPage from Javascript in Xamarin.Forms 如何从Xamarin.Forms中的可移植类库项目调用位于Android项目内的方法? - How to invoke a method located inside the Android project from the Portable Class Library Project in Xamarin.Forms? 如何从Xamarin Forms项目中调用Android和iOS项目中可用的方法? - How to call the method available in Android and iOS project from Xamarin Forms project? 如何在xamarin表单中选择listview项目时打开另一个页面? - How to open another page when a listview item is selected in xamarin forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM