简体   繁体   中英

how to pass Action type in bundle type to AppCompatActivity android xamarin

I have Action in MainActivity. I want to pass it to another ViewPagerActivity: AppCompatActivty. As here is:

 Action<ImageView> btnAction = PickSelected;

    ...
     private void PickSelected(ImageView img)
        {

        }

    ...
   var intentVP = new Android.Content.Intent(this, typeof(ViewPagerActivity));
   Bundle mb = new Bundle();
   mb.PutParcelable("bundle",btnAction); // here it give me the error 
   intentVP.PutExtra("MainBundle",mb);

   StartActivity(intentVP);

The error is next: "btnAction cannot be converted from System.Action to Android.OS.IParcelable". Is it possible pass Action type or there is another way?

I can't make comments so I will just answer instead.

From what I can see here, it looks like you are using an Action delegate and wanting to return a result based of what was picked in the "PickSelected" method.

The problem though is Action delegate return void, so you cannot do this.

Instead, you need to make the btnAction a Func and change the signature of PickSelected to the Android.OS.IParcelable (or whatever the type is required for that, I don't code for Android sadly) and in the method where you're passing the delegate, you need to invoke it and pass the ImageView object.

Func<ImageView, Android.OS.IParcelable> btnAction = PickSelected;
 private Android.OS.IParcelable PickSelected(ImageView img)
    {
        //// Insert logic here

        return Android.OS.IParcelable object;
    }
var intentVP = new Android.Content.Intent(this, typeof(ViewPagerActivity));
Bundle mb = new Bundle();
mb.PutParcelable("bundle",btnAction.Invoke(ImageViewObject)); 
StartActivity(intentVP);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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