C# 使用MvvmCross,将模型转换为要在列表中显示的模型的最佳实践是什么?
我有一个无法更改的数据模型,比如:C# 使用MvvmCross,将模型转换为要在列表中显示的模型的最佳实践是什么?,c#,mvvm,mvvmcross,C#,Mvvm,Mvvmcross,我有一个无法更改的数据模型,比如: public class TodoItem { public string Id { get; set; } [JsonProperty(PropertyName = "userid")] public Guid UserId { get; set; } [JsonProperty(PropertyName = "text")] public string Text { get; set; } [JsonP
public class TodoItem
{
public string Id { get; set; }
[JsonProperty(PropertyName = "userid")]
public Guid UserId { get; set; }
[JsonProperty(PropertyName = "text")]
public string Text { get; set; }
[JsonProperty(PropertyName = "complete")]
public bool Complete { get; set; }
}
在线服务返回ObservableCollection。我想在我的视图中展示这一点,但我需要添加PropertyChanged,以及一些与切换完成相关的静态操作,可能使用MvxViewModel作为基类。在将模型转换为视图模型的同时仍然确保视图模型中的更改会更改模型,有哪些好的方法?在视图模型对象中是否有对原始对象的引用
public class ToDoItemViewModel : MvxViewModel
{
private TodoItem _item;
public ToDoItemViewModel(TodoItem item)
{
_item = item;
}
public Guid UserId
{
get
{
return _item.UserId;
}
set
{
if (_item.UserId == value) return;
_item.UserId = value;
RaisePropertyChanged(() => UserId);
}
}
public string Text
{
get
{
return _item.Text;
}
set
{
if (_item.Text == value) return;
_item.Text = value;
RaisePropertyChanged(() => Text);
}
}
public bool Complete
{
get
{
return _item.Complete;
}
set
{
if (_item.Complete == value) return;
_item.Complete = value;
RaisePropertyChanged(() => Complete);
CompleteChanged.Invoke(_item);
}
}
public static Action<TodoItem> CompleteChanged;
}
您可以只在模型上实现INotifyPropertyChanged吗?您可以只在模型上实现INotifyPropertyChanged吗?
var todoItems = await todoTable
.Where(todoItem => todoItem.Complete == false)
.ToCollectionAsync();
Items = new ObservableCollection<TodoItemViewModel>(todoItems.Select(i => new TodoItemViewModel(i)));
Items = await _todoTable
.Where(item => item.Complete == false)
.Select(item => new TodoItemViewModel(item))
.ToCollectionAsync();