Xamarin.Android OnActivityResult 未在 Fragment 内调用 [英] Xamarin.Android OnActivityResult not being called inside a Fragment

查看:28
本文介绍了Xamarin.Android OnActivityResult 未在 Fragment 内调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在接受从相机拍摄的照片后,似乎没有调用 OnActivityResult.

It appears as if OnActivityResult does not get called after accepting the picture taken from the camera.

我调用 StartActivityForResult() 是错误的吗?还是我遗漏了什么.

 public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
        {
            var m_View = inflater.Inflate (Resource.Layout.Feed, null);

            btnCamera = m_View.FindViewById<Button> (Resource.Id.btnCamera);
            btnGallery = m_View.FindViewById<Button> (Resource.Id.btnGallery);

            ivPicture = m_View.FindViewById<ImageView> (Resource.Id.imageView1);

            btnCamera.Click += (sender, e) => {
                //launch gallery
                //allow photo editing/saving

                var mediaPicker = new MediaPicker (Activity);
                if (!mediaPicker.IsCameraAvailable){
//                      Console.WriteLine ("No Camera!");
                }else {
                    Intent intent = new Intent(MediaStore.ActionImageCapture);
                    StartActivityForResult(intent, 0);
                }
            };

            btnGallery.Click += (sender, e) => {
                //launch gallery
                //allow photo editing/saving
                var imageIntent = new Intent ();
                imageIntent.SetType ("image/*");
                imageIntent.SetAction (Intent.ActionGetContent);
                StartActivityForResult (
                    Intent.CreateChooser (imageIntent, "Select photo"), 0);

            };

            return m_View;
        }

        protected virtual void OnActivityResult (int requestCode, Result resultCode, Intent data)
        {
            System.Console.WriteLine ("WOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
            Uri contentUri = data.Data;
            ivPicture.SetImageURI (data.Data);

        }

推荐答案

嗯.. 我创建了一个示例,它对我来说效果很好.我看到的唯一区别是您的 override 看起来不正确.它应该是 public override void OnActivityResult()

Hmm.. I created a sample and it worked just fine for me. The only difference I see is that it looks like your override is not correct. It should be public override void OnActivityResult()

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var rootView = inflater.Inflate(Resource.Layout.MainFragment, container, false);
    var button = rootView.FindViewById<Button>(Resource.Id.button_camera);
    button.Click += (sender, args) =>
    {
        var intent = new Intent(MediaStore.ActionImageCapture);
        StartActivityForResult(intent, 0);
    };
    return rootView;
}

public override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    Log.Debug("TestFragment", "Got result");

    // do what you want with the result here
}

这篇关于Xamarin.Android OnActivityResult 未在 Fragment 内调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆