如何在 xamarin android 中将数据从 Fragment 传递到 Activity?

How to pass data from Afragment to Bactivity on xamarin android?

Afragment.cs

var data=new Intent(this,typeof(Bactivity));
    data.PutStringArrayListExtra("ByGenres",TrackModel.Track);
        data.PutExtra("position",e.Position);
        StartActivity(data);

  • Error CS0119: Expression denotes a type', where avariable', value' ormethod group' was expected
  • Error CS1502: The best overloaded method match for `Android.Content.Intent.Intent(Android.Content.Context, System.Type)' has some invalid arguments (CS1502)

无法将数据传递给 Bacitivity。

在片段中你必须使用 Activity 而不是 this var data=new Intent(this.Activity, typeof(Bactivity));

var str = Newtonsoft.Json.JsonConvert.SerializeObject(obj); 
... 
T obj = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str);

如果它是一个大对象,后者更好,因为它更快。

使用序列化函数将对象转换为字符串,然后 activity 使用反序列化函数转换回对象。

var track=Newtonsoft.Json.JsonConvert.SerializeObject(items.tracks); 
var data=new Intent(this.Activity,typeof(AudioPlayer)); 
data.PutExtra("listdata",track);
data.PutExtra("position",e.Position);
StartActivity(data);