我正在尝试在食谱页面中显示配料对象列表。
我有2个表成分
public class Ingredients
{
// primary key
[PrimaryKey, AutoIncrement]
public int IngredientsId { get; set; }
// specify the foreign key
[ForeignKey(typeof(Recipe))]
public int RecipeId { get; set; }
// many to one relationship with recipe
[ManyToOne]
public Recipe Recipe { get; set; }
// other attributes
public string IngredientsName { get; set; }
public double Ammount { get; set; }
public string Units { get; set; }
public override string ToString()
{
return IngredientsName + " " + Ammount + Units;
}
}
和食谱
public class Recipe
{
[PrimaryKey, AutoIncrement]
public int RecipeID { get; set; }
public string RecipeName { get; set; }
public string VideoURL { get; set; }
public string PrepTime { get; set; }
public string CookTime { get; set; }
public int Serves { get; set; }
public string MealType { get; set; }
public string Equipment { get; set; }
public string Method { get; set; }
// one to many relationship with ingredients
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<Ingredients> Ingredients { get; set; }
}
它们彼此以两种方式保持联系,我通过调试知道这一点。我现在正试图使用数据绑定来显示一个包含ListView中所有成分的食谱页面。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:video="clr-namespace:SQLiteListView.FormsVideoLibrary"
xmlns:local="clr-namespace:SQLiteListView.Models"
x:Class="SQLiteListView.Views.ItemPage"
Title="{Binding RecipeName}">
<ContentPage.Content>
<ScrollView>
<StackLayout>
<Label Text="{Binding RecipeName}"
FontSize="50"/>
<video:VideoPlayer HeightRequest="320" WidthRequest="220">
<video:VideoPlayer.Source>
<video:ResourceVideoSource>
<video:ResourceVideoSource.Path>
<OnPlatform x:TypeArguments="x:String">
<On Platform="iOS" Value="Videos/iOSApiVideo.mp4" />
<On Platform="Android" Value="ScrambledEggs.mp4" />
<On Platform="UWP" Value="Videos/UWPApiVideo.mp4" />
</OnPlatform>
</video:ResourceVideoSource.Path>
</video:ResourceVideoSource>
</video:VideoPlayer.Source>
</video:VideoPlayer>
<Label
Margin="10"
Text="Meal Type"
FontSize="Large"
FontAttributes="Bold"/>
<Label Text="{Binding MealType}"
Margin="10"
FontSize="Medium"/>
<Label Margin="10"
Text="Serves"
FontSize="Large"
FontAttributes="Bold"/>
<Label Text="{Binding Serves}"
Margin="10"
FontSize="Medium"/>
<Label Margin="10"
Text="Preperation Time"
FontSize="Large"
FontAttributes="Bold"/>
<Label Text="{Binding PrepTime}"
Margin="10"
FontSize="Medium"/>
<Label Margin="10"
Text="Cooking Time"
FontSize="Large"
FontAttributes="Bold"/>
<Label Text="{Binding CookTime}"
Margin="10"
FontSize="Medium"/>
<Label Margin="10"
Text="Equipment"
FontSize="Large"
FontAttributes="Bold"/>
<Label Text="{Binding Equipment}"
Margin="10"
FontSize="Medium"/>
<Label Margin="10"
Text="Ingredients"
FontSize="Large"
FontAttributes="Bold"/>
<ListView x:Name="IngriedientsListView" ItemsSource ="Ingredients">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding IngredientName }"/>
<Label Text="{Binding Ammount}"/>
<Label Text="{Binding Units}"/>
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Label Margin="10"
Text="Method"
FontSize="Large"
FontAttributes="Bold"/>
<Label Text="{Binding Method}"
Margin="10"
FontSize="Medium"/>
</StackLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
我曾尝试从灌肠者那里获取食材,但是我无法仅仅将食材附在食谱上。我试图将Recipe.Ingredients作为ItemSource添加到ListView,但这似乎也不起作用。
任何人都可以帮忙吗?