Entity Framework Core SQLite与Xamarin.Forms不更新Db。

我正在做一个Xamarin.Forms MVVM App,用Entity Framework Core SQLite在手机本地保存一个Db。

我生成了Db和一些初始项目,之后通过App添加了一个新的项目,目前工作正常(在App中刷新列表视图并正确显示所有项目)。

问题是当我想更新Db中的一个项目时。我更新了这个项目,但是在列表视图中没有更新。在列表视图中更新的唯一方法是重新启动应用程序。

我怎样才能用更新的项目刷新列表视图?

这是Datacontext代码。

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

这是MainPageViewModel代码:

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

这是SensorPageViewModel代码。

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

这是SensorItemViewModel代码。

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

解决方案:

我调试了一下progrma流程,发现当我更新项目并保存在手机上时,就会出现一个新的项目。_dataContextSensorViewModel 它不出现在 “中国梦 “中。MainPageViewModel.

将更新项目保存在 SensorViewModel 我补充道 Actualizado 的名字。

introducir la descripción de la imagen aquí

但当我读到de _dataContextMainViewModel 它没有出现。

introducir la descripción de la imagen aquí

当我添加一个新的项目时,它的工作正常,没有问题。

给TA打赏
共{{data.count}}人
人已打赏
未分类

onActivityResult没有被片段捕获(Kotlin)。

2022-9-9 16:52:17

未分类

我必须为现有的数据库重新创建表作为模型吗?

2022-9-9 16:52:19

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索