【问题标题】:Can't reference object properties after navigating to new page (C#, Xamarin)导航到新页面后无法引用对象属性(C#、Xamarin)
【发布时间】:2020-07-27 04:56:55
【问题描述】:

我无法从我的配方对象(引用 InventoryItem 对象(或成分))中引用某些属性。首先,这是课程。

public class InventoryItem
{
    [PrimaryKey]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Size { get; set; }
    public string Stock { get; set; }
}

public class Recipe
{
    [PrimaryKey]
    public int ID { get; set; }
    public string Name { get; set; }

    [OneToOne]
    public InventoryItem Ingredient { get; set; }
}

接下来,这是创建新配方并将成分属性设置为inventoryItem 的代码。 (inventoryItem 已经被实例化并正确设置了成分的每个属性。)

        Recipe recipe = new Recipe()
        {
            ID = (maxPK == null ? 1 : maxPK.ID + 1),
            Name = nameEntry.Text,
            Ingredient = inventoryItem
        };

最后,我只是想测试一下这个配方成分是否设置正确,所以我检查了一下,recipe.Ingredient.Name 确实正确弹出。

        db.Insert(recipe);
        await DisplayAlert(null, recipe.Name + " - " + recipe.Ingredient.Name + " saved!", "okay.");
        await Navigation.PopAsync();

现在解决问题...首先,当我打开一个新的 ViewRecipePage 时,我会发送选定/指定的食谱作为参考。指定的配方是从所有配方的 collectionView 中选择的。

    private async void checkRecipe_Clicked(object sender, EventArgs e)
    {
        await Navigation.PushAsync(new ViewRecipePage(recipe));
    }

然后在 ViewRecipePage 上,我想查看该食谱的不同属性,当然包括成分。

        try
        {
            Label recipeName = new Label();
            recipeName.Text = recipe.Name;
            grid.Children.Add(recipeName);

            Label ingredient1Name = new Label();
            ingredient1Name.Text = recipe.Ingredient.Name;
            grid.Children.Add(ingredient1Name);

        }catch(Exception e) {  DisplayAlert(null, e.ToString(), "okay."); };

recipe.Name 显示得很好,但是,每当我尝试以任何方式引用 recipe.Ingredient.Name 时,我都会收到错误“System.NullReferenceException: Object reference not set to an instance of an object.”

我确定这是我在途中遗漏的一些愚蠢的东西,但我无法弄清楚为什么当我测试 recipe.Ingredient.Name 在我将食谱添加到数据库时有效...

无论如何,如果我能提供更多信息,请告诉我!谢谢参观!干杯。

编辑:

带有 OneToMany 成分的新配方类

public class Recipe
{
    [PrimaryKey]
    public int ID { get; set; }
    public string Name { get; set; }

    [OneToMany("ID")]
    public List<InventoryItem> Ingredient { get; set; }
}

现在,当添加新食谱时,我会为成分创建一个列表,然后将成分列表设置为该新列表...

        inventoryItem = (InventoryItem)ingredient1Picker.SelectedItem;
        List<InventoryItem> invList = new List<InventoryItem>();
        invList.Add(inventoryItem);

        Recipe recipe = new Recipe()
        {
            ID = (maxPK == null ? 1 : maxPK.ID + 1),
            Name = nameEntry.Text,
            Ingredient = invList    
        };

然后我插入数据库并检查成分是否已设置 - 这一切正常。

        db.Insert(recipe);
        await DisplayAlert(null, recipe.Name + recipe.Ingredient[0].Name + " saved!", "okay.");

但是,在我的“GetRecipesPage”上,我像这样获取collectionView:

        collectionView.ItemsSource = db.GetAllWithChildren<Recipe>().OrderBy(x => x.Name).ToList();

但是,当我选择一个食谱并导航到 ViewRecipePage 并尝试显示成分列表(我只是尝试查看列表中的第一个也是唯一一个成分)时,我收到错误索引超出范围。我认为这意味着该列表根本没有填充,即使在我将配方的成分列表设置为包含成分的列表并对其进行测试之后也是如此。

        try
        {
            Label recipeName = new Label();
            recipeName.Text = recipe.Name;
            grid.Children.Add(recipeName);

            Label ingredient1Name = new Label();
            ingredient1Name.Text = recipe.Ingredient[0].Name;
            grid.Children.Add(ingredient1Name);

        }catch(Exception e) {  DisplayAlert(null, e.ToString(), "okay."); };

【问题讨论】:

  • 你是如何填充你的 CollectionView 的?您确定您使用的查询也包括子对象吗?
  • 我不确定它是否包括子对象... collectionView.ItemsSource = db.Table().OrderBy(x =>x.Name).ToList();现在我看到了,我假设它不是,但我也不知道如何哈哈。
  • ...我不知道如何在这个网站上格式化,抱歉大声笑。
  • 如果您使用的是 sqlite.net 扩展,请尝试GetWithChildren
  • 我将编辑我的问题,以便您可以看到我所做的更改,并且我可以更清楚地解释新错误。

标签: c# database xamarin


【解决方案1】:

如果您使用 SQLite 扩展,则需要确保在处理子表/对象时使用 InsertWithChildrenGetWithChildren

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2017-07-23
    相关资源
    最近更新 更多