首页 > 解决方案 > CarouselView CarouselPage 如何使用数据库中的绑定项创建集合页面?没有 MVVM

问题描述

我是 Xamarin 的新手,我有这门课的数据库

[Table("Version")] 
public class Version
{
    [PrimaryKey,AutoIncrement,Column("_Id")]
    public int Version_ID { get; set; }
    public string Version_Name { get; set; }

    [ForeignKey(typeof(Profile))]               
    public int Version_ProfileID { get; set; }

    [ForeignKey(typeof(Version))]               
    public int Version_ParentID { get; set; }

    [OneToMany(CascadeOperations = CascadeOperation.All)]
    public List<ParameterValue> Version_ParameterValue { get; set; }
}

我想在没有 MVVM 的情况下,类中的 1 个项目已成为 Carousel 中的 1 页。(这不是主屏幕)这就是我所有的 XAML:

 CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d"
 x:Class="Versions"
 x:Name="LayoutVersions"
 ItemsSource="{Binding}">

<CarouselPage.ItemTemplate>
    <DataTemplate>
        <Label Text="{Binding Version_Name}"></Label>
   </DataTemplate>
</CarouselPage.ItemTemplate>
</CarouselPage>

和 xaml.cs:

 public Versions(Categories Category)
    {
        selectedCategory = Category;
        ItemsSource = App.Database7.GetItems(); // it`s line not worked, how to create new page with binding item from database? 
        InitializeComponent();
    }

版本库.cs:

    public IEnumerable<Version> GetItems()
    {      
        return database.GetAllWithChildren<Version>(recursive: true);
    }

新 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"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="SniperNote.ListParameters"
         x:Name="LayoutVersions">

<ContentPage.Content>
    <CarouselView x:Name="CarouselVersion">
        <CarouselView.ItemTemplate>
            <DataTemplate>
                <Label Text="{Binding Version_Name}"></Label>
            </DataTemplate>
        </CarouselView.ItemTemplate>
    </CarouselView>

新的 XAML.cs:

 public ListParameters(Categories Category)
    {
        InitializeComponent();
        selectedCategory = Category;
        CarouselVersion.ItemsSource = App.Database7.GetItems();
    }

PS对不起我的英语,谢谢你的回答。

标签: androidsqlitexamlxamarin

解决方案


推荐阅读