我正在xamarin表单中使用sqlite创建一个项目,我遇到了一个问题,在保存新项目页面上,我创建了我的项目模型的示例,并在项目中引用了另一个模型,即优先级,当我单击保存项目并将其置于调试模式时,我可以接收所有项目,包括保存的优先级,但在我添加SQLite中的项的变量中,它返回PRIORITY如下:ItemPriority = {CrudTasks.Models.Priority}
当我试图访问这些项目上的视图页面我得到没有回应。
//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);
}
我无法访问另一个模型的表
1条答案
按热度按时间mefy6pfw1#
根据你的代码,我创建了一个演示,并在我这边实现了这个功能。
你可以尝试修改你的代码如下:
1.为
Priority.cs
添加外键(public int ItemId { get; set; }
)字符串
2.将
Item.cs
的[ManyToOne]
更改为OneToOne
型
3.为
IItemsService.cs
添加几个方法(InsertPriority
和UpdatePriority
)型
4.为类
ItemsService.cs
实现以上接口InsertPriority
和UpdatePriority
。我们还需要将方法
GetAllItems()
上的代码var items = _connection.Table<Item>().ToList();
替换为代码var items = _connection.GetAllWithChildren<Item>();
型
5.修改
AddItemViewmodel.cs
的ExecuteSaveItemsCommand
命令型