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

我想从Xamarin Android项目打开Xamarin表单页面。 在android项目中,我创建了太棒项目图像,我在这里调用事件从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(Resource.Id.ImgSmallC); imgSmallC.Click += (object sender, EventArgs args) => { StartActivity(typeof(MyPopupPage)); }; return base.OnCreateOptionsMenu(menu); } } 

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

System.ArgumentException:type参数名称:Type不是从java类型派生的。

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

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

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

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

基于Dependency Service的示例:

接口:

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

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