【问题标题】:Updating all lists in Xamarin form SQLite以 SQLite 形式更新 Xamarin 中的所有列表
【发布时间】:2021-11-23 02:21:25
【问题描述】:

我正在使用同一个 SQLite 表的列表视图,我在添加新项目或更新前一个项目时遇到问题,它会更新当前页面上的值,但需要重新打开应用程序以更新页面 B 上的列表.你有什么解决办法吗?Before Update Updated Value here Did'nt update here 那你能给我一个链接或视频来帮助我吗?

【问题讨论】:

  • 如果您希望我们帮助您,您需要发布相关代码。
  • 嗨! ListView 的 ItemSource 是哪个?你在使用 Binding,NotiftyPropertyChanged 吗?
  • 请注意,在 SO 上发布代码或错误作为图像是不可接受的。请阅读How to Ask 了解有关发布指南的更多信息

标签: sqlite xamarin.forms


【解决方案1】:

要从列表中添加和删除项目并立即进行更改,您需要在System.Collections.ObjectModel 中使用ObservableCollection。是否使用 SQLite 没有区别;这是关于列表的。

这是一个例子:

public partial class Page : ContentPage
{
    private ObservableCollection<Client> _client;
    public SQLiteAsyncConnection connection;
    
    public Page()
    {
        InitializeComponent();
    }

    protected override async void OnAppearing()
    {
        //initialize database
        await connection.CreateTableAsync<CLient>();
        var listofclient = await connection.Table<Client>().ToListAsync();

        //initialize observablecollection 
        _client = new ObservableCollection<Client>(listofclients);

        //using the list of client as source of listview
        clientListveiw.ItemsSource = _client;
        base.OnAppearing();
    }
    
    //add button
    private async void onadd(object sender, EventArgs e)
    {
        var client = new Client { Name = "jhon"};
        await connection.InsertAsync(patient);
        _client.Add(patient);
    }
}

关于更新,你应该使用INotifyPropertyChange接口;这是一个很长的故事,但你可以谷歌它以获取更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多