【问题标题】:Xamarin MVVM Fill Model with a Model PropertyXamarin MVVM 使用模型属性填充模型
【发布时间】:2020-03-03 05:51:53
【问题描述】:

如何使用其中包含模型属性的模型?

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

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);
        }

这里是来自 api 的反序列化。

IEnumerable<TypeModel> TypeEnumerator;

public async Task<IEnumerable<TypeModel>> GetTypesAsync(bool r = false)
        {
            if (r)
            {
                var j = await HttpConstructor.GetStringAsync($"api/gettypes");
                return await Task.Run(() => JsonConvert.DeserializeObject<IEnumerable<TypeModel>>(j));

            }

            return TypeEnumerator; ;
        }

这是从 api 生成的类型的 json 信息

  {
    "pType": 10,
    "fDepartment": 1,
    "title": "Bigwig",
    "description": "For the bigwigs",
    "comments": "high priority",
    "version": "1.2.3"
  },
  {
    "pType": 11,
    "fDepartment": 1,
    "title": "Frontdesk",
    "description": "front end people",
    "comments": "none",
    "version": "1.2.4"
  }

【问题讨论】:

  • 什么不起作用?实际问题是什么?您是否收到错误或异常?
  • 当我将 fDepartment 从 int 更改为 DepartmentModel 时出现此错误 - Newtonsoft.Json.JsonSerializationException:将值 1 转换为类型“TestProject.Models.DepartmentModel”时出错。路径“[0].status”,第 1 行,位置 131。---> System.ArgumentException:无法从 System.Int64 转换或转换为 TestProject.Models.DepartmentModel。
  • 听起来您的 json 与您尝试反序列化的模型不匹配。修复一个或另一个,使它们匹配。
  • 这是我的问题。既然我从 int 变成了 model,我应该怎么做。
  • 您的问题根本没有提到反序列化或json,并且您没有提供示例json,并且您没有显示DepartmentModel的代码。如果我们必须先玩 20 个问题才能弄清楚您要做什么,那么提供帮助是非常困难的。您可以让 Newtonsoft 为您处理这个问题,但我个人可能会创建一个临时类来反序列化,然后将其转换为您的最终类。鉴于您提供的信息量有限,这是我能做的最好的了。

标签: xamarin mvvm properties model


【解决方案1】:

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

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 = "???" 
  };
}

【讨论】:

    【解决方案2】:

    通过使用 Dictionary 集合获得解决方案,避免调整模型和搞乱整个应用程序的业务逻辑。

    我保留了原始模型并创建了一个名为 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; }
    
            }
    

    然后我使用Linq join来组合信息并填写字典值。

        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
    
    
                                };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多