似乎未收到结果,显示为“ onActivityResult()”

时间:2019-04-02 18:53:22

标签: c# android visual-studio xamarin xamarin.android

我有两个活动,一个活动只有一个巨大的列表视图,另一个活动有一个plainText和按钮。

此“ newRoll”活动需要返回添加到ObservableCollection的字符串,我尚未实现listView更新的部分,但是使用logcat,我可以知道没有任何内容添加到保存所有项目的ObservableCollection中。我对意图的运作方式有误解吗?从我得到的结果来看,这应该运作吗?

主要活动:

    //upon clicking any item in the listView this runs:

    Intent NewRoll = new Intent(this, typeof(NewRoll));
    StartActivityForResult(NewRoll, requestCode);

protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            //just holds the string
                    holder = data.GetStringExtra("roll_name");
            //adds it to an observable collection
                    rolls.Add(holder);
            //me trying to get something back
                    Log.Info(rolls.Count.ToString(), "Roll Added");
                    Log.Info(holder, "Please?");
        }

NewRoll:

//on button click
//get whatever is in the plaintext
                newRoll = rollName.Text;
                data.PutExtra("roll_name",newRoll);
                SetResult(Result.Ok, data);
                Finish();

2 个答案:

答案 0 :(得分:0)

Xamarin上的文档指出Android.App.Activity.OnActivityResult的签名为

[Android.Runtime.Register("onActivityResult", "(IILandroid/content/Intent;)V", "GetOnActivityResult_IILandroid_content_Intent_Handler")]
protected virtual Void OnActivityResult (Int32 requestCode, [Android.Runtime.GeneratedEnum] Result resultCode, Intent data)

所以您必须重写它以接收活动结果,例如

protected override Void OnActivityResult (Int32 requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);

    // do your stuff
}

答案 1 :(得分:0)

protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        //just holds the string
                holder = data.GetStringExtra("roll_name");
        //adds it to an observable collection
                rolls.Add(holder);
        //me trying to get something back
                Log.Info(rolls.Count.ToString(), "Roll Added");
                Log.Info(holder, "Please?");
    }

我在这里看不到@Override注释。您确定要覆盖Activity基类方法onActivityResult吗?