按钮点击Xamarin Android来调用Xamarin.Forms.Page?

时间:2017-09-05 11:31:13

标签: xamarin xamarin.android xamarin.forms

我需要通过Page中的一个活动来调用Xamarin.Forms Xamarin.Android

我曾使用[Java.Interop.Export("btnOne1Click")]来调用Xamarin.Forms Page

如果我使用App.Current.MainPage = new RootPage();,代码会正常运行但不会导航到Page

我不想拨打[Java.Interop.Export("btnOne1Click")]上的MainActivity

[Activity(Label = "FailureActivity")]
public class FailureActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        RequestWindowFeature(WindowFeatures.NoTitle);

        SetContentView(Resource.Layout.Failure);

        Button button = FindViewById<Button>(Resource.Id.txt_go_onjobs);
    }

    [Java.Interop.Export("btnOne1Click")]
    public void btnOne1Click(Android.Views.View v)
    {
        try
        {
            Xamarin.Forms.Forms.Context.StartActivity(typeof(RootPage));
        }
        catch(Exception ex)
        { }
    }
}

1 个答案:

答案 0 :(得分:0)

Xamarin表单在一个活动上运行,这与您的主要活动最相似。

有两个示例项目向您展示如何在代码的原生部分和表单部分之间进行通信,可在此处找到:

  1. https://github.com/xamarin/xamarin-forms-samples/tree/master/Forms2Native
  2. https://github.com/xamarin/xamarin-forms-samples/tree/master/Native2Forms
  3. 但是,要回答您的问题,您可以执行以下操作:

    button = FindViewById<Button> (Resource.Id.txt_go_onjobs);
    button.Click += (sender, e) => {
        // this is your Xamarin.Forms screen
        StartActivity(typeof(FormsActivity));
    };
    

    现在,在FormsActivity致电:

    [Activity (Label = "FormsActivity")]            
    public class FormsActivity : Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
    
            Xamarin.Forms.Forms.Init (this, bundle);
            LoadApplication (new App ());
        }
    }