具有模型属性的Xamarin MVVM填充模型

具有模型属性的Xamarin MVVM填充模型,xamarin,mvvm,properties,model,Xamarin,Mvvm,Properties,Model,如何处理包含模型属性的模型 我正在成功地从api中提取信息,但在我尝试将模型从int更改为model(如下所示)后,它不起作用: public class TypeModel { [PrimaryKey] public int pType { get; set; } public DepartmentModel fDepartment { get; set; } public string Title { get; set; } public str

如何处理包含模型属性的模型

我正在成功地从api中提取信息,但在我尝试将模型从int更改为model(如下所示)后,它不起作用:

public class TypeModel
{
    [PrimaryKey]
    public int pType { get; set; }

    public DepartmentModel fDepartment { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public string Comments { get; set; }

    public string Version { get; set; }

}
这是部门模型

public class DepartmentModel
{

    public int pDepartment { get; set; }

    public string Name { get; set; }

}
我的ViewModel有此代码,并且正在工作。我一直在尝试改变,因为我认为我需要以某种方式改变它

        Types.Clear();

        IEnumerable<TypesModel> types = await DataSource.GetTypesAsync(typeinfo.pType, true);

        foreach (var column in types)
        {
            Types.Add(column);
        }

这就是我要做的。毫无疑问,还有其他方法可以解决这个问题

public class TypeModel
{
    [PrimaryKey]
    public int pType { get; set; }
    public int fDepartment { get; set; }
    public DepartmentModel Department { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Comments { get; set; }
    public string Version { get; set; }
}

List<TypesModel> types = await DataSource.GetTypesAsync(typeinfo.pType, true); 

foreach (var type in types)
{
  type.Department = new DepartmentModel 
  { 
      pDepartment = type.fDeparment, 
      Name = "???" 
  };
}
公共类类型模型
{
[主密钥]
公共int pType{get;set;}
公共int fDepartment{get;set;}
公共部门模型部门{get;set;}
公共字符串标题{get;set;}
公共字符串说明{get;set;}
公共字符串注释{get;set;}
公共字符串版本{get;set;}
}
List types=await DataSource.GetTypesAsync(typeinfo.pType,true);
foreach(类型中的变量类型)
{
type.Department=新部门模型
{ 
pDepartment=type.fdepartment,
Name=“?”
};
}

通过使用字典集合获得了一个解决方案,避免了调整模型和打乱整个应用程序的业务逻辑

我保留了原始模型,并创建了一个名为TypesModel的新模型,用于列表视图

   public class TypesModel
        {
            [PrimaryKey]
            public int pType { get; set; }

            public Dictionary<int, string> fDepartment { get; set; }

            public string Title { get; set; }

            public string Description { get; set; }

            public string Comments { get; set; }

            public string Version { get; set; }

        }
公共类类型模型
{
[主密钥]
公共int pType{get;set;}
公共字典fDepartment{get;set;}
公共字符串标题{get;set;}
公共字符串说明{get;set;}
公共字符串注释{get;set;}
公共字符串版本{get;set;}
}
然后,我使用LINQJOIN组合信息,并填充字典值

    var query = from t in types
                            join d in departments
                            on t.fDeparment equals d.pDepartment
                            select new TypesModel
                            {
                                pType = t.pType,
                                fDepartment = new Dictionary<int, string>()
                                {
                                    { d.pDepartment, d.Name }
                                },  
                                Title = t.Title,
                                Description = t.Description


                            };
var query=来自类型中的t
加入d部门
关于t.f部门等于d.p部门
选择新类型模型
{
pType=t.pType,
fDepartment=新词典()
{
{d.p部门,d.Name}
},  
Title=t.Title,
描述=t.描述
};

什么不起作用?实际问题是什么?您是否收到错误或异常?当我将fDepartment从int更改为DepartmentModel时收到此错误-Newtonsoft.Json.JsonSerializationException:将值1转换为类型“TestProject.Models.DepartmentModel”时出错。路径“[0]。状态”,第1行,位置131。-->System.ArgumentException:无法从System.Int64强制转换或转换为TestProject.Models.DepartmentModel。听起来您的json与您尝试反序列化为的模型不匹配。修正其中一个,使它们匹配。这是我的问题。既然我从int改为model,我该怎么做呢?你的问题根本没有提到反序列化或json,你也没有提供json示例,你也没有展示
DepartmentModel
的代码。如果我们必须先回答20个问题,然后才知道你想做什么,那么很难提供帮助。您可以让Newtonsoft为您处理这个问题,但就个人而言,我可能会创建一个临时类进行反序列化,然后将其转换为最终类。鉴于你提供的信息有限,这是我能做的最好的了。
   public class TypesModel
        {
            [PrimaryKey]
            public int pType { get; set; }

            public Dictionary<int, string> fDepartment { get; set; }

            public string Title { get; set; }

            public string Description { get; set; }

            public string Comments { get; set; }

            public string Version { get; set; }

        }
    var query = from t in types
                            join d in departments
                            on t.fDeparment equals d.pDepartment
                            select new TypesModel
                            {
                                pType = t.pType,
                                fDepartment = new Dictionary<int, string>()
                                {
                                    { d.pDepartment, d.Name }
                                },  
                                Title = t.Title,
                                Description = t.Description


                            };