首页 > 解决方案 > Xamarin / SQLite:数据库中的更新双打

问题描述

我正在开发一个 Xamarin.Forms 应用程序,该应用程序将存储一些化学品的收据列表。化学品有名称(用户不可更改)和浓度。我希望用户能够更改表单中的浓度并存储它们。

我有它,我可以使用 UPDATE 按钮修改列表中的值,我可以添加(或删除)列表的成员,所有成员都是持久的。但是,我无法弄清楚如何更改条目框中列表中的值。

我试图做一些像Arvind Chourasiya 在这里所做的事情,但我无法弄清楚等效的“connection.Update”。我认为 SQLite 无法连接,因为它不是正确的事件,但我不确定。

我最实用的 C# 代码是...

using System;
using System.ComponentModel;
using Xamarin.Forms;
using SQLite;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;

namespace contactBook
{

    public class Recipe : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        private string _name;

        [MaxLength(255)]
        public string Name
        { 
            get { return _name; }
            set
            {
                if (_name == value)
                    return;

                _name = value;

                OnPropertyChanged();
            }
        }

        private double _concentration;

        public double Concentration
        {
            get
            { return _concentration; }
            set
            {
                if (_concentration == value)
                    return;

                _concentration = value;

                OnPropertyChanged();
            }
        }

        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        private SQLiteAsyncConnection _connection;
        private ObservableCollection<Recipe> _recipes;

        public MainPage()
        {
            InitializeComponent();

            _connection = DependencyService.Get<ISQLiteDb>().GetConnection();
            //setBasicReceipies();

        }

        protected override async void OnAppearing()
        { 
            await _connection.CreateTableAsync<Recipe>();

            var recipes = await _connection.Table<Recipe>().ToListAsync();
            _recipes = new ObservableCollection<Recipe>(recipes);
            recipesListView.ItemsSource = _recipes;

            base.OnAppearing();
        }

        async void setBasicReceipies()  // worked during tests
        {

            var recipe1 = new Recipe { Name = "NH3", Concentration = 0.0 };
            var recipe2 = new Recipe { Name = "H2SO4", Concentration = 0.1 };
            var recipe3 = new Recipe { Name = "NaCl", Concentration = 0.2 };

            await _connection.InsertAsync(recipe1);
            await _connection.InsertAsync(recipe2);
            await _connection.InsertAsync(recipe3);
        }

        async void OnAdd(object sender, System.EventArgs e)
        {
            var recipe = new Recipe { Name = "test ", Concentration = 0.0 };
            await _connection.InsertAsync(recipe);

            _recipes.Add(recipe);
        }

        async void OnUpdate(object sender, System.EventArgs e)
        {
            var recipe = _recipes[0];
            recipe.Concentration += 0.05;

            await _connection.UpdateAsync(recipe);
        }

        async void OnDelete(object sender, System.EventArgs e)
        {
            var recipe = _recipes[0];
            await _connection.DeleteAsync(recipe);
            _recipes.Remove(recipe);
        }

        //async void Entry_PropertyChanged(System.Object sender, System.ComponentModel.PropertyChangedEventArgs e)
        //{
        //    await _connection.UpdateAllAsync();
        //}

    }
}

标签: c#sqlitexamarinxamarin.forms

解决方案


推荐阅读