如何在活动之间传递对象 [英] How to pass Objects between activities

查看:54
本文介绍了如何在活动之间传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个feed对象,其中填充了反序列化的json数据.我正在尝试在2个xamarin android活动之间传递feed对象.

I have a feed object, that is filled with deserialized json data. I am trying to pass a feed object between 2 xamarin android activities.

我该怎么做?

我知道putseriazable,但是我不确定如何实现.

I am aware of putseriazable but i'm not sure how to implement this.

public class Feed
{
    public class Author
    {
        public int id { get; set; }
        public string slug { get; set; }
        public string name { get; set; }
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string nickname { get; set; }
        public string url { get; set; }
        public string description { get; set; }
    }       

    public class CustomFields
    {
        public List<string> sno_headline { get; set; }
        public List<string> sno_deck { get; set; }
        public List<string> sno_format { get; set; }
        public List<string> sno_longform_order { get; set; }
        public List<string> sno_longform_title { get; set; }
        public List<string> sno_sr_tag { get; set; }
        public List<string> sno_sr_cat { get; set; }
        public List<string> sno_sr_title { get; set; }
        public List<string> sno_longform_main_title { get; set; }
        public List<string> sno_rails_number { get; set; }
        public List<string> sno_rails_writer { get; set; }
        public List<string> sno_rails_type { get; set; }
        public List<string> sno_rails_stories { get; set; }
        public List<string> sno_longform_image { get; set; }
        public List<string> sno_longform_image_master { get; set; }
        public List<string> sno_teaser { get; set; }
        public List<string> writer { get; set; }
        public List<string> jobtitle { get; set; }
        public List<string> featureimage { get; set; }
        public List<string> video { get; set; }
        public List<string> videographer { get; set; }
        public List<string> videolocation { get; set; }
        public List<string> related { get; set; }
        public List<string> audio { get; set; }
        public List<string> customlink { get; set; }
        public List<string> story_sport { get; set; }
        public List<string> teasertitle { get; set; }
        public List<string> teaser { get; set; }
        public List<string> grade { get; set; }
        public List<string> showratings { get; set; }
        public List<string> date { get; set; }
        public List<string> ourscore { get; set; }
        public List<string> theirscore { get; set; }
        public List<string> opponent { get; set; }
        public List<string> sport { get; set; }
    }

    public class Full
    {
        public string url { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }

    public class Thumbnail
    {
        public string url { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }

    public class Medium
    {
        public string url { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }

    public class Large
    {
        public string url { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }

    public class Carouselthumb
    {
        public string url { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }

    public class Tsmediumblock
    {
        public string url { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }

    public class Tsbigblock
    {
        public string url { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }

    public class Small
    {
        public string url { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }

    public class ThumbnailImages
    {
        public Full full { get; set; }
        public Thumbnail thumbnail { get; set; }
        public Medium medium { get; set; }
        public Large large { get; set; }
        public Carouselthumb carouselthumb { get; set; }
        public Tsmediumblock tsmediumblock { get; set; }
        public Tsbigblock tsbigblock { get; set; }
        public Small small { get; set; }
    }

    public class Post
    {
        public int id { get; set; }
        public string type { get; set; }
        public string slug { get; set; }
        public string url { get; set; }
        public string status { get; set; }
        public string title { get; set; }
        public string title_plain { get; set; }
        public string content { get; set; }
        public string excerpt { get; set; }
        public string date { get; set; }
        public string modified { get; set; }
        public List<object> categories { get; set; }
        public List<object> tags { get; set; }
        public Author author { get; set; }
        public List<object> comments { get; set; }
        public List<object> attachments { get; set; }
        public int comment_count { get; set; }
        public string comment_status { get; set; }
        public CustomFields custom_fields { get; set; }
        public string thumbnail { get; set; }
        public string thumbnail_size { get; set; }
        public ThumbnailImages thumbnail_images { get; set; }
    }

    public class Query
    {
        public bool ignore_sticky_posts { get; set; }
    }

    public class RootObject
    {
        public string status { get; set; }
        public int count { get; set; }
        public int count_total { get; set; }
        public int pages { get; set; }
        public List<Post> posts { get; set; }
        public Query query { get; set; }
    }
}

推荐答案

您可以使用Newtonsoft Json将类转换为json字符串,然后传递该字符串,然后在新活动中将其反序列化.

You could use Newtonsoft Json to convert the class to a json string, and pass the string, then deserialize it in the new activity.

确保您引用了Newtonsoft JSON

Make sure you reference Newtonsoft JSON

var MySerializedObject = JsonConvert.SerializeObject(MyComplexObject);
MyIntent.PutExtra("MyComplexObject", MySerializedObject);

然后,将其返回给对象...

Then, to get it back to the object...

var MyJsonString = Intent.GetStringExtra("MyComplexObject");
var MyObject = JsonConvert.DeserializeObject<ObjectType>(MyJsonString);

只需确保MyJsonString正是您所期望的并且在反序列化之前不为null,否则它可能引发异常.

Just be sure that MyJsonString is exactly what you're expecting and is not null before deserializing, otherwise it can throw an exception.

这篇关于如何在活动之间传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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