Xamarin.Android OnActivityResult未被片段内部调用

时间:2014-09-15 17:55:44

标签: android xamarin xamarin.android xamarin-studio

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

我调用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);

        }

4 个答案:

答案 0 :(得分:2)

嗯..我创建了一个样本,它对我来说效果很好。我看到的唯一区别是,您的覆盖看起来不正确。它应该是公共覆盖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
}

答案 1 :(得分:0)

检查请求代码是否与为startActivityForResult(intent,0)提供的相同,这里0是请求代码..例如,如果你把999放在这里0 ..然后在onActivity结果方法中检查requestcode == 999.它是用于检查意图是否与要求的相同,除了那个......

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 0 && resultCode == RESULT_OK) { // ADD THIS
    // Get Data and do something with it 
    }
}

答案 2 :(得分:0)

尝试将resultCode数据类型更改为int,而不是Result,并将覆盖的函数public更改。

e.g。

public override void OnActivityResult(int requestCode, int resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
}

答案 3 :(得分:0)

针对使用 Android.Support.V4.App 片段的用户。您可以在片段中覆盖 OnActivityResult 方法,但是如果将它与 FragmentActivity 结合使用,它将永远不会被调用! 将被调用的是 FragmentActivity OnActivityResult 。可以像下面这样实现:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
}