简体   繁体   English

Xamarin.forms 混合 ContentPage 和 Android Activity

[英]Xamarin.forms mixing ContentPage and Android Activity

How can I mix both pages together如何将两个页面混合在一起

I want to do something platform specific (Android) OnActivityResult but I use a ContentPage in Xamarin.Forms我想做一些特定于平台的 (Android) OnActivityResult但我在 Xamarin.Forms 中使用 ContentPage

How do I know in my .Droid project which activity it is?我如何知道我的 .Droid 项目是哪个activity

Lets say I have a page called CalendarPage : ContentPage .假设我有一个名为CalendarPage : ContentPage的页面CalendarPage : ContentPage Where do I register it in my droid project so I can catch the ActivityResult in that Activity(ContentPage)?我在哪里可以在我的 droid 项目中注册它,以便我可以在该 Activity(ContentPage) 中捕获ActivityResult

I have no clue since I'm new to Xamarin.Forms and learning it.我不知道,因为我是 Xamarin.Forms 的新手并正在学习它。

Xamarin forms runs on one activity, which is most like your main activity. Xamarin 表单在一项活动上运行,这与您的主要活动最相似。

There are two sample projects that show you how to communicate between native and form parts of the code, which can be found here有两个示例项目向您展示了如何在代码的本机部分和表单部分之间进行通信,可以在此处找到

  1. https://github.com/xamarin/xamarin-forms-samples/tree/master/Forms2Native https://github.com/xamarin/xamarin-forms-samples/tree/master/Forms2Native
  2. https://github.com/xamarin/xamarin-forms-samples/tree/master/Native2Forms https://github.com/xamarin/xamarin-forms-samples/tree/master/Native2Forms

However, to answer your question, you would do something like the following但是,要回答您的问题,您将执行以下操作

 private const int MyRequestCode = 101;

 //Start activity for result
 var contactPickerIntent = new Intent(Intent.ActionPick, Android.Provider.ContactsContract.Contacts.ContentUri);
 context.StartActivityForResult(contactPickerIntent, MyRequestCode);

and then in your main activity (the activity that initializes your xamarin forms application (using global::Xamarin.Forms.Forms.Init(this, bundle); )然后在您的主要活动中(初始化您的 xamarin 表单应用程序的活动(使用global::Xamarin.Forms.Forms.Init(this, bundle);

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    if (requestCode == MyRequestCode && resultCode == Result.Ok)
    {
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM