将数据模型传递给前台服务

时间:2018-02-23 02:33:42

标签: android xamarin.android

我有一个前台服务,我在我的Splash Activity上触发

Intent StartServiceIntent;
StartServiceIntent = new Intent(this, typeof(PagesService));

StartServiceIntent.PutExtra("Table", LINKMODEL);

Log.Info("101028", "Connectted");
StartService(StartServiceIntent);

在我使用PutExtra时,我想将列表List<LINKMODEL>中的数据模型项发送到服务,以下数据模型

public class LINKMODEL
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public string Url { get; set; }       
    }

似乎该服务只接受几个参数,如数组等,

如何将整个模型发送到服务?

1 个答案:

答案 0 :(得分:1)

最好的方法是使用Newtonsoft Json Nuget Package。

1.安装Newtonsoft JSON软件包,以防您没有

Install-Package Newtonsoft.Json -Version 11.0.1

2.当您必须将对象从一个活动发送到另一个活动时,将您的obj序列化为JSON字符串。

string toSend = JsonConvert.SerializeObject(yourModelobj);

3.将其作为字符串传递给意图

4.在接收到其他活动后,将其反序列化并根据需要使用它。

LINKMODEL localDetails = JsonConvert.DeserializeObject<LINKMODEL>(intentData);

你也可以在List&lt;&gt;上执行此操作物镜&#39; S

古德勒克!