I am currently working on a Xamarin forms app. The idea is to get data from an API and users will be able to view recipes, add it to favorites and search.
I ran into a bit of a problem with displaying my list of favorited items items. I am unable to view my data into my list, Not sure If I am even inserting into the table properly. I will try to post relevant code as much as I can. I would appreciate any help:)
Here I am clicking the add to fave button - DBManager.cs
public void Button_Clicked(System.Object sender, System.EventArgs e)
{
//add to favoritelist
dbModel.insertNewToDo(temp);
}
Here is the insertNewToDo function DBManager.cs
private SQLiteAsyncConnection _connection;
private ObservableCollection<Pancake>temp;
public DBManager()
{
_connection = DependencyService.Get<ISQLiteDb>().GetConnection();
}
public async Task<ObservableCollection<Pancake>> CreateTable()
{
await _connection.CreateTableAsync<Pancake>();
//get
var pancake = await _connection.Table<Pancake>().ToListAsync();
var allTasks = new ObservableCollection<Pancake>(pancake);
return allTasks;
}
Here is favorite page is supposed display list of pancakes but doesn't:/:((( FavePage.xaml.cs
ObservableCollection<Pancake> allPancakes;
public FavePage()
{
InitializeComponent();
}
protected async override void OnAppearing()
{
allPancakes = await dbModel.CreateTable();
allFaveTable.ItemsSource = allPancakes;
base.OnAppearing();
}
Here is FavePage.xaml
<StackLayout>
<StackLayout Orientation="Horizontal">
<Button Text="Delete" />
</StackLayout>
<ListView x:Name="allFaveTable">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
It looks like you haven't bound the ItemsSource of the ListView to the observable collection. Make the ObservableCollection into a public property of the page that contains the StackLayout, and use x:Bind in the ListView to bind the ListView's ItemsSource to the ObservableCollection.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.