Xamarin.forms 如何在xamarin表单中获取意图活动结果

Xamarin.forms 如何在xamarin表单中获取意图活动结果,xamarin.forms,Xamarin.forms,我正在尝试使用依赖项服务来启动在我的xamarin表单应用程序中启用BT的意图。但我发现很难将结果返回到xamarin表单。是否有任何机制将activityresult通知返回表单应用程序。 在形式上, DependencyService.Get<IBluetoothHandler>().EnableBluetooth(); 如何将此活动结果生成表单 谢谢您可以在android项目的main活动中覆盖OnActivityResult方法。Forms只在整个应用程序中使用一个活动(幕

我正在尝试使用依赖项服务来启动在我的xamarin表单应用程序中启用BT的意图。但我发现很难将结果返回到xamarin表单。是否有任何机制将activityresult通知返回表单应用程序。 在形式上,

DependencyService.Get<IBluetoothHandler>().EnableBluetooth();
如何将此活动结果生成表单


谢谢

您可以在android项目的
main活动
中覆盖
OnActivityResult
方法。Forms只在整个应用程序中使用一个活动(幕后)。所以就用这个吧

要通知页面(或PCL/Forms项目中的另一个类),请使用Xamarin.Forms提供的

下面是一个例子:

您在MainActivity(android项目)中的方法:

然后,您的消息传递类:

public class ActivityResultMessage
{
    public static string Key = "arm";

    public int RequestCode { get; set; }

    public object ResultCode { get; set; }

    public object Data { get; set; }
}
然后在类中使用以下代码来处理结果:

// Subscribe to the onactivityresult-message
MessagingCenter.Subscribe<ActivityResultMessage>(this, ActivityResultMessage.Key, (sender) =>
{
    // do some magic =)
});
//订阅onactivityresult消息
MessagingCenter.Subscribe(此,ActivityResultMessage.Key,(发件人)=>
{
//施展魔法=)
});

请阅读有关messagincenter的内容(上面提供的链接),以了解其背后的内容。

在MainActivity中添加此内容

public static MainActivity Instance;
在OnCreate方法中添加:

 Instance = this;

当您执行某些操作时,活动可能会被销毁。您还应该向OnCreate添加相同的代码,并检查捆绑包内容试图覆盖OnActivityResult在我的主要活动中说“没有合适的方法来覆盖”。我使用的是默认的MainActivity“public class MainActivity:global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity”。知道怎么修吗?
public static MainActivity Instance;
 Instance = this;