I am creating a project using sqlite in xamarin forms, I am experiencing a problem that, on the save new items page, I create an instance of my ITEM model and within the ITEM I reference another model which is PRIORITY, when I click on save items and I leave it in debug mode, I can receive all the items, including the saved PRIORITY, but within the variable where I add the items in SQLite it returns the PRIORITY as follows: ItemPriority = {CrudTasks.Models.Priority}
And when I try to access these items on the View Page I get no response.
//Function SaveItems
private async void ExecuteSaveItemsCommand()
{
try
{
Item itemSaved = new Item
{
Name = NameSave,
Description = DescriptionSave,
ItemPriority = new Priority()
{
PriorityName = PrioritySave,
PriorityColor = "#ccc"
}
};
_itemsService.InsertItem(itemSaved);
ExecuteBackProductPageCommand();
}
catch (Exception ex)
{
await Shell.Current.DisplayAlert("Error", ex.Message, "OK");
}
}
//Model Item
namespace CrudTarefas.Models
{
[Table("Items")]
public class Item
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[ManyToOne]
public Priority ItemPriority { get; set; }
}
}
//Model Priority
namespace CrudTarefas.Models
{
[Table("Priorities")]
public class Priority
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string PriorityName { get; set; }
public string PriorityColor { get; set; }
}
}
//CollectionView ListItemsPage
<ScrollView VerticalScrollBarVisibility="Never">
<StackLayout>
<CollectionView ItemsSource="{Binding ItemsList}">
<CollectionView.ItemsLayout>
<LinearItemsLayout ItemSpacing="20" Orientation="Vertical" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Grid
Margin="0,0,0,20"
Padding="10"
xct:CornerRadiusEffect.CornerRadius="12"
BackgroundColor="{Binding ItemPriority.PriorityColor}"
RowDefinitions="*,20,20"
RowSpacing="10">
<Frame
Grid.Row="0"
Padding="5"
HorizontalOptions="Start">
<Label Text="{Binding ItemPriority.PriorityName}" />
</Frame>
<Label
Grid.Row="1"
Text="{Binding Name}"
TextColor="Black" />
<Label
Grid.Row="2"
Text="{Binding Description}"
TextColor="Black" />
</Grid>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ScrollView>
//Viewmodel ListItems
-> In the LOADITEMS method in the commented code, I can access the static item with the collectionView
#region constructor
public ListItemsViewmodel(INavigation navigation)
{
Navigation = navigation;
_itemsService = new ItemsService();
LoadItems();
GoAddItemPageCommand = new Command(ExecuteGoAddItemPageCommand);
LoadItemsCommand = new Command(ExecuteLoadItemsCommand);
}
#endregion
#region commands
public ICommand GoAddItemPageCommand { get; set; }
public ICommand LoadItemsCommand { get; set; }
#endregion
#region methods
-> private void LoadItems()
{
var items = _itemsService.GetAllItems();
ItemsList = new ObservableCollection<Item>(items);
}
I can't access another model's table
Based on your code, I created a demo and achieved this function on my side.
You can try to modify your code as follows:
1.add a foreign Key (
public int ItemId { get; set; }) forPriority.cs2.change
[ManyToOne]toOneToOneforItem.cs3.add several methods(
InsertPriorityandUpdatePriority) forIItemsService.cs4.implement above interface
InsertPriorityandUpdatePriorityfor classItemsService.cs.And we also need to replace code
var items = _connection.Table<Item>().ToList();with codevar items = _connection.GetAllWithChildren<Item>();on methodGetAllItems()5.modify command
ExecuteSaveItemsCommandforAddItemViewmodel.cs