我正在做一个Xamarin.Forms MVVM App,用Entity Framework Core SQLite在手机本地保存一个Db。
我生成了Db和一些初始项目,之后通过App添加了一个新的项目,目前工作正常(在App中刷新列表视图并正确显示所有项目)。
问题是当我想更新Db中的一个项目时。我更新了这个项目,但是在列表视图中没有更新。在列表视图中更新的唯一方法是重新启动应用程序。
我怎样才能用更新的项目刷新列表视图?
public class DataContext : DbContext
{
public DataContext() : base()
{
//Database.EnsureDeleted();
Database.EnsureCreated();
}
public DbSet<Sensor> Sensors { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite($"Filename={Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "database.sqlite")}");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Sensor>(s =>
{
s.HasKey(en => en.Id);
//s.HasIndex(en => en.ClientId).IsUnique();
s.Property(en => en.Name).IsRequired();
});
modelBuilder.Entity<Sensor>()
.HasData(
new Sensor { Id = Guid.NewGuid().ToString(), ClientId = "11111", Name = "First item", Description = "This is a private item description.", Payload = "Off" },
new Sensor { Id = Guid.NewGuid().ToString(), ClientId = "11112", Name = "Second item", Description = "This is a shopping item description.", Payload = "Off" },
new Sensor { Id = Guid.NewGuid().ToString(), ClientId = "11113", Name = "Third item", Description = "This is a work item description.", Payload = "Off" }
);
}
public class MainPageViewModel : ViewModelBase
{
private readonly INavigationService _navigationService;
private readonly DataContext _dataContext;
private static MainPageViewModel _instance;
private DelegateCommand _addSensorCommand;
private ObservableCollection<SensorItemViewModel> _sensors;
public MainPageViewModel(
INavigationService navigationService,
DataContext dataContext) : base(navigationService)
{
_navigationService = navigationService;
_dataContext = dataContext;
_instance = this;
LoadSensors();
}
public DelegateCommand AddSensorCommand => _addSensorCommand ?? (_addSensorCommand = new DelegateCommand(AddSensor));
public ObservableCollection<SensorItemViewModel> Sensors
{
get => _sensors;
set => SetProperty(ref _sensors, value);
}
public static MainPageViewModel GetInstance()
{
return _instance;
}
private async void AddSensor()
{
await _navigationService.NavigateAsync("SensorPage", null);
}
public async void LoadSensors()
{
try
{
var _sensors = await _dataContext.Sensors.ToListAsync();
Sensors = new ObservableCollection<SensorItemViewModel>(_sensors.Select(s => new SensorItemViewModel(_navigationService)
{
Id = s.Id,
ClientId = s.ClientId,
Name = s.Name,
Description = s.Description,
Payload = s.Payload
}).ToList());
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert("Error", ex.Message, "Aceptar");
}
}
}
public class SensorPageViewModel : ViewModelBase
{
private readonly INavigationService _navigationService;
private readonly DataContext _dataContext;
private DelegateCommand _addCommand;
private Sensor _sensor;
private string _buttonText;
public SensorPageViewModel(
INavigationService navigationService,
DataContext dataContext) : base(navigationService)
{
_navigationService = navigationService;
_dataContext = dataContext;
}
public DelegateCommand AddCommand => _addCommand ?? (_addCommand = new DelegateCommand(Add));
public string ButtonText
{
get => _buttonText;
set => SetProperty(ref _buttonText, value);
}
public Sensor Sensor
{
get => _sensor;
set => SetProperty(ref _sensor, value);
}
public override void OnNavigatedTo(INavigationParameters parameters)
{
base.OnNavigatedTo(parameters);
if (parameters.Count >= 1)
{
var parameterSensor = parameters.GetValue<SensorItemViewModel>("Sensor");
Sensor = new Sensor()
{
Id = parameterSensor.Id,
ClientId = parameterSensor.ClientId,
Name = parameterSensor.Name,
Description = parameterSensor.Description,
Payload = parameterSensor.Payload
};
Title = Sensor.Name;
ButtonText = "Editar";
}
else
{
Sensor = new Sensor();
Title = "Nuevo sensor";
ButtonText = "Salvar";
}
}
private async void Add()
{
if (ButtonText == "Editar")
{
ButtonText = "Salvar";
Title = "Editar";
return;
}
try
{
if (Title == "Editar")
{
_dataContext.Sensors.Update(Sensor);
await _dataContext.SaveChangesAsync();
}
else
{
Sensor.Id = Guid.NewGuid().ToString();
_dataContext.Sensors.Add(Sensor);
await _dataContext.SaveChangesAsync();
}
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert("Error", ex.Message, "Aceptar");
return;
}
MainPageViewModel.GetInstance().LoadSensors();
await _navigationService.GoBackAsync();
}
}
public class SensorItemViewModel : Sensor
{
private readonly INavigationService _navigationService;
private DelegateCommand _selectSensorCommand;
public SensorItemViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
}
public DelegateCommand SelectSensorCommand => _selectSensorCommand ?? (_selectSensorCommand = new DelegateCommand(SelectSensor));
private async void SelectSensor()
{
var parameters = new NavigationParameters
{
{"Sensor", this}
};
await _navigationService.NavigateAsync("SensorPage", parameters);
}
}
这里的问题不是Xamarin.Forms不更新数据库。当你关闭然后打开应用程序时,你说你可以看到更新的值。这意味着该值已经成功地在数据库中持久化了。问题是UI不知道某些东西已经更新了。当你从数据库中添加删除项目时 ObservableCollection那么这个集合就可以解决这个问题,因为它提出了 OnPropertyChanged 为您提供。然而,当你只是简单地更新一个项目(改变它的一些属性)时,你需要告诉UI有什么变化。这可以通过 INotifyPropertyChanged 介面。
见 此处 关于 INotifyPropertyChanged
最简单的实现就是这样。
public class PropertyChangedViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected void SetValue<T>(ref T backingField, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(backingField, value))
{
return;
}
backingField = value;
OnPropertyChanged(propertyName);
}
}
然后,你就可以拥有你的 SensorItemViewModel.cs
继承 PropertyChangedViewModel
扬 PropertyChanged
事件的时候。
还有一点--你千万不要把数据库实体类和你的ViewModel类一起子类化。MVVM的理念与拥有同一个类两次完全不同,有点像。你的 SensorItemViewModel
应该有它自己的属性,对于每个你想在UI中动态更新的属性,你需要提出 PropertyChanged
事件。
假设你只有一个属性(名称),而你想动态地改变它。那么你的 SensorItemViewModel
会是这样的。
public class SensorItemViewModel : PropertyChangedViewModel
{
private string name;
public string Name
{
get => name;
set => SetValue(ref name, value);
}
}
这里,每当 Name
属性变化,它将调用 SetValue
方法,它会为你处理事件的引发,从而通知UI需要更改名称redrawn。