无法将视图模型可观察集合绑定到列表视图



我一直在尝试将观测值从viewModel绑定到我的列表视图,但是列表中没有任何内容显示view bambly clamn

我想将数据库中的项目加载到ListView

模型:

public class Note
{
    [PrimaryKey]
    [AutoIncrement]
    public int Id { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
    public DateTime DateCreated { get; set; }
}

视图模型:

public class MainPageViewModel : INotifyPropertyChanged
{
    public string Boom { get; set; }
    public ICommand AddNoteIconCommand { get; private set; }
    private SQLiteAsyncConnection _db;
    private ObservableCollection<Note> _notes;
    public ObservableCollection<Note> Notes
    {
        get { return _notes; }
        set
        {
            if (_notes == value)
                return;
            _notes = value;
            OnPropertyChanged();
        }
    }
    private INavigation Navigation;
    public event PropertyChangedEventHandler PropertyChanged;
    public MainPageViewModel(INavigation Navigation)
    {
        this.Navigation = Navigation;
        AddNoteIconCommand = new Command(AddNewNote);
        PrepareDatabase();
        Boom = "Hello";
    }
    private async void PrepareDatabase()
    {
        _db = DependencyService.Get<ISQLiteDb>().GetConnection();
        var _databasenotes = await _db.Table<Note>().ToListAsync();
        _notes = new ObservableCollection<Note>(_databasenotes);
    }
    private async void AddNewNote()
    {
        await Navigation.PushAsync(new NewNotePage());
    }
    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

主页xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:NoteTaker"
         x:Class="NoteTaker.MainPage"
         Padding="10">
<ContentPage.ToolbarItems>
    <ToolbarItem Icon="Plus.png" Command="{Binding AddNoteIconCommand}" />
</ContentPage.ToolbarItems>
<StackLayout>
    <ListView HasUnevenRows="True"
              ItemsSource="{Binding Notes, Mode=TwoWay}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell Text="{Binding Title}" Detail="{Binding Body}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
<Entry Text = "{Binding Boom}" />
</StackLayout>

主页代码背后:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        BindingContext = new MainPageViewModel(Navigation);
        InitializeComponent();            
    }
}

这里的问题在哪里?注意:我已经确保通过调试我可观察的收藏集填充了数据库对象

您需要通知MainPageViewModel.Notes已更改的视图。

您的代码Notes = new ObservableCollection<Note>(_notes);未向视图发送任何通知,因此该视图未更新。您可以将MainPageViewModel作为BindableObject并致电OnPropertyChanged,以通知有新笔记的观点。

作为旁注,您应该管理模型中的数据库连接,而不是视图模型以遵循MVVM模式。

最后,我设法修复了错误,问题是在"新ObservableCollection"的行中,我通过手动通过foreach循环添加项目来修复该项目,从而将其添加到可观察的集合中。这解决了问题,就像魅力一样工作!

public class MainPageViewModel : INotifyPropertyChanged
{
    public ICommand AddNoteIconCommand { get; private set; }
    private SQLiteAsyncConnection _db;
    private ObservableCollection<Note> _notes = new ObservableCollection<Note>();
    public ObservableCollection<Note> Notes            
    {
        get { return _notes; }
        set
        {
            if (_notes == value)
                return;
            _notes = value;
            OnPropertyChanged();
        }
    }

    private INavigation Navigation;
    public event PropertyChangedEventHandler PropertyChanged;
    public MainPageViewModel(INavigation Navigation)
    {
        this.Navigation = Navigation;
        AddNoteIconCommand = new Command(AddNewNote);
        PrepareDatabase();
    }
    private async void PrepareDatabase()
    {
        _db = DependencyService.Get<ISQLiteDb>().GetConnection();
        var _databasenotes = await _db.Table<Note>().ToListAsync();
        FillObservable(_databasenotes);
    }
    private async void AddNewNote()
    {
        await Navigation.PushAsync(new NewNotePage());
    }
    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    private void FillObservable(List<Note> _dbnotes)
    {
        foreach (var note in _dbnotes)
        {
            _notes.Add(note);
        }
    }
}

最新更新