How can I bind the full answer from SQLite?

I am making a ListView inside my C # file. But instead, I want to add the data I get from sqlite, also a data-bound xaml file, so I can still edit the layout using xaml. Therefore, each response from sqlite should be added as a label ( <TextCell Text="{Binding Name}" /> ).

My question is: how can I bind a response from GetCategoryByMenuID to TextCell Text="{Binding Name}" ?

xaml page (CategoriePage.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" x:Class="AmsterdamTheMapV3.CategoriePage"> <ListView x:Name="listView"> <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding Name}" /> </DataTemplate> </ListView.ItemTemplate> </ListView> </ContentPage> 

Back-end / C # (CategoriePage.xaml.cs):

 namespace AmsterdamTheMapV3 { public partial class CategoriePage : ContentPage { public CategoriePage(String txt) { InitializeComponent(); var layout = new StackLayout { Padding = new Thickness(5, 10) }; int page = Int32.Parse(txt); this.Content = layout; var categories = App.Database.GetCategoryByMenuID(page); var datatemplate = new DataTemplate(() => { var nameLabel = new Label(); nameLabel.SetBinding(Label.TextProperty, "Name"); //nameLabel.SetBinding(Label.idProperty, "Name"); return new ViewCell { View = nameLabel }; }); var listView = new ListView { ItemsSource = categories, ItemTemplate = datatemplate }; layout.Children.Add(listView); } } } 

GetCategoryPage Function:

 public List<Categories> GetCategoryByMenuID(int menuID) { lock (locker) { return db.Table<Categories>().Where(x => x.Menu_ID == menuID).ToList(); } } 
+6
source share
2 answers

Considering

 <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="AmsterdamTheMapV3.CategoriePage"> <ListView x:Name="listView"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.View> <StackLayout> <Label Text="{Binding Name}" /> </StackLayout> </ViewCell.View> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </ContentPage> 

Update code for

 namespace AmsterdamTheMapV3 { public partial class CategoriePage : ContentPage { public CategoriePage(string txt) { InitializeComponent(); this.Appearing += (s,e) => { if(NotInDesignMode) { int page = 0; if(int.TryParse(txt, out page)){ var categories = App.Database.GetCategoryByMenuID(page); this.listView.ItemsSource = categories; } } }; } bool NotInDesignMode { get { return Application.Current != null; } } } } 
+2
source

You can do this in an ugly and quickly implemented way (in the GetCategoryByMenuID method, run the update name by id, which will work by category).

Or by creating a collection that is menu-based, instead of an index based like a regular list.

  public class CategoriesCollection<T> : KeyedCollection<int, T> { protected override int GetKeyForItem(T item) { return (item as Category).Id; } /// <summary> /// Method to get the index into the List{} in the base collection for an item that may or may /// not be in the collection. Returns -1 if not found. /// </summary> protected override int GetItemIndex(T itemToFind) { int keyToFind = GetKeyForItem(itemToFind); return BaseList.FindIndex((T existingItem) => GetKeyForItem(existingItem).Equals(keyToFind)); } } 

sql should be ok, saving / retrieving everything that is icollection, so although you couldn't use a dictionary, this should work. At least this worked for me in sqlite (before I switched from sqliteAPI to entity)

+1
source

Source: https://habr.com/ru/post/1012627/


All Articles