Ich habe eine ObservableCollection von der Viewmodel zu meinem Listview, aber nichts zu binden versucht, in der Listenansicht zeigt sich nicht einmal leere ZellenKann nicht ein Ansichtsmodell ObservableCollection binden an die Listview
Ich möchte die Elemente laden in meiner Datenbank zur Listen
Das Modell:
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; }
}
The View Modell:
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));
}
}
HauptSeite 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>
Mainpage-Code Behind:
public partial class MainPage : ContentPage
{
public MainPage()
{
BindingContext = new MainPageViewModel(Navigation);
InitializeComponent();
}
}
Wo liegt das Problem? Hinweis: Ich habe durch Debugging sichergestellt, dass meine beobachtbare Sammlung mit den Datenbankobjekten gefüllt wird
Versuchen Aufruf InitializeComponent erste – MickyD
Ich habe versucht, nicht funktioniert, obwohl –
versuchen, die VM zu schaffen, bevor Sie es mit dem Binding zuweisen – Jason