简体   繁体   English

Xamarin.forms 如何从 observablecollection 中获取属性并将其放入 CarouselView

[英]Xamarin forms how to take the property from a observablecollection and put it into a CarouselView

I want to create a level selector using CarouselView displaying 6 levels per each screen scrolls.我想使用 CarouselView 创建一个级别选择器,每个屏幕滚动显示 6 个级别。 I have create a Model for a LevelCard meaning each one of the cards have their own value, I've done this because each level has a number of stars you've scored to be displayed meaning I need to have an instance of each card to update the stars for each level(card).我已经为 LevelCard 创建了一个LevelCard ,这意味着每张卡片都有自己的价值,我这样做是因为每个级别都有许多要显示的星星,这意味着我需要每张卡片的实例更新每个级别(卡片)的星星。

Ingore PopulateCardInformation method that method isn't finished nor it will be like this, I just made it for quick testing but nothing seems to work. Ingore PopulateCardInformation方法尚未完成,也不会像这样,我只是为了快速测试而制作它,但似乎没有任何效果。

If you look at the last image you can see while debugging I can select the needed card but I can never actually pull the property to display as text.如果您在调试时查看最后一张图片,我可以 select 所需的卡片,但我永远无法真正拉出该属性以显示为文本。 I've searched every where and this is what I've tried to write in Path=card1[Game] , Path=Game[card1] , Path=card1.Game , Path=Game.card1 every time I add anything to do with a property name it actually looking in my Model.NameOfProperty ... its looking for another model called Game ...我已经搜索了Path=card1.Game Path=card1[Game] ,这Path=Game.card1Path=Game[card1]添加任何与它实际上在我的Model.NameOfProperty中寻找一个属性名称......它正在寻找另一个名为Game的 model ...

public class LevelCard
{
    public int Game { get; set; }
    public int Score { get; set; }
    public bool IsCompleted { get; set; }
}
public class LevelSelectorPopUpModel : BaseViewModel
{
    ObservableCollection<LevelCard> cards = new ObservableCollection<LevelCard>();
    public ObservableCollection<LevelCard> Cards
    {
        get => cards;
    }

    int game = 1;
    int totalGames;

    public LevelSelectorPopUpModel()
    {
        Initialize();
    }

    async void Initialize()
    {
        totalGames = await LevelReader.GetTotalGames();
        InitializeWidthHeightProperties();
        InitializeCards();
    }

    void InitializeCards()
    {
        LevelCard card1 = new LevelCard();
        LevelCard card2 = new LevelCard();
        LevelCard card3 = new LevelCard();
        LevelCard card4 = new LevelCard();
        LevelCard card5 = new LevelCard();
        LevelCard card6 = new LevelCard();

        cards.Add(card1);
        cards.Add(card2);
        cards.Add(card3);
        cards.Add(card4);
        cards.Add(card5);
        cards.Add(card6);

        PopulateCardInformation();
    }

    void PopulateCardInformation()
    {
        foreach (var card in cards)
        {
            if (totalGames < game)
                return;
            card.Game = game;
            game++;
        }
    }
}

XAML XAML

<CarouselView ItemsSource="{Binding Cards}"
                HorizontalScrollBarVisibility="Always"
                WidthRequest="{Binding Width}"
                HeightRequest="600"
                Margin="10, 0, 10, 0"
                BackgroundColor="Red"
                HorizontalOptions="CenterAndExpand"
                VerticalOptions="CenterAndExpand">

    <CarouselView.ItemTemplate>
        <DataTemplate>
            <Grid RowSpacing="50"
                    VerticalOptions="CenterAndExpand">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <StackLayout Orientation="Horizontal"
                                WidthRequest="600"
                                HeightRequest="250"
                                BackgroundColor="Orange"
                                VerticalOptions="CenterAndExpand"
                                HorizontalOptions="CenterAndExpand"
                                Margin="0, 10, 0, 0">
                    <Label Text="{Binding Path=card1, FallbackValue='Error'}"
                            BackgroundColor="Purple"
                            WidthRequest="200"
                            HeightRequest="200" />
                    <Label Text="{Binding Path=card2, FallbackValue='Error'}"
                            BackgroundColor="Purple"
                            WidthRequest="200"
                            HeightRequest="200" />
                    <Label Text="{Binding Path=card3, FallbackValue='Error'}"
                            BackgroundColor="Purple"
                            WidthRequest="200"
                            HeightRequest="200" />
                </StackLayout>

                <StackLayout Orientation="Horizontal"
                                WidthRequest="600"
                                HeightRequest="250"
                                BackgroundColor="Orange"
                                Grid.Row="1"
                                VerticalOptions="CenterAndExpand"
                                HorizontalOptions="CenterAndExpand"
                                Margin="0, 0, 0, 10">
                    <Label BackgroundColor="Purple"
                            WidthRequest="200"
                            HeightRequest="200" />
                    <Label BackgroundColor="Purple"
                            WidthRequest="200"
                            HeightRequest="200" />
                    <Label BackgroundColor="Purple"
                            WidthRequest="200"
                            HeightRequest="200" />
                </StackLayout>
            </Grid>
        </DataTemplate>
    </CarouselView.ItemTemplate>
</CarouselView>     

在此处输入图像描述 在此处输入图像描述

A CarousalView is not the best choice to show what you want. CarousalView不是显示您想要的内容的最佳选择。 However, you can make it work by understanding how it works.但是,您可以通过了解它的工作原理来使其发挥作用。 The CarousalView internally stores an item as CurrentItem . CarousalView在内部将项目存储为CurrentItem Each time you scroll it, it will change its CurrentItem to the next item.每次滚动它时,它会将其CurrentItem更改为下一个项目。 That means, the first time you see your CarousalView , its CurrentItem is the first card you added to the Cards collection.这意味着,您第一次看到CarousalView时,它的CurrentItem是您添加到Cards集合中的第一张卡片。 When you write {Binding card1} in the ItemTemplate it tries to find a property named card1 inside its CurrentItem , which is a Card and does not have such property.当您在ItemTemplate中编写{Binding card1}时,它会尝试在其CurrentItem中找到一个名为card1的属性,这是一个Card并且没有这样的属性。 If number 6 is fixed, you create a helper class:如果数字 6 是固定的,则创建一个助手 class:

public class LevelCardGroup 
{
    public LevelCard Card1 { get; }
    public LevelCard Card2 { get; }
    public LevelCard Card3 { get; }
    public LevelCard Card4 { get; }
    public LevelCard Card5 { get; }
    public LevelCard Card6 { get; }

    public LevelCardGroup(LevelCard card1, LevelCard card2, LevelCard card3,
        LevelCard card4, LevelCard card5, LevelCard card6)
    {
        Card1 = card1;
        Card2 = card2;
        Card3 = card3;
        Card4 = card4;
        Card5 = card5;
        Card6 = card6;
    }
}

And change your view model to add instances of the helper class inside the Cards collection:并更改您的视图 model 以在Cards集合中添加助手 class 的实例:

ObservableCollection<LevelCardGroup> cards = new ObservableCollection<LevelCardGroup>();
public ObservableCollection<LevelCardGroup> Cards
{
    get => cards;
}

void InitializeCards()
{
    LevelCard card1 = new LevelCard();
    LevelCard card2 = new LevelCard();
    LevelCard card3 = new LevelCard();
    LevelCard card4 = new LevelCard();
    LevelCard card5 = new LevelCard();
    LevelCard card6 = new LevelCard();

    cards.Add(new LevelCardGroup(card1, card2, card3, card4, card5, card6));

    PopulateCardInformation();
}

You have to change PopulateCardInformation as well.您还必须更改PopulateCardInformation And in your view:在你看来:

<Label Text="{Binding Path=Card1.Game, FallbackValue='Error'}"
    BackgroundColor="Purple"
    WidthRequest="200"
    HeightRequest="200" />

Now the CurrentItem of the CarousalView will be LevelCardGroup which contains a property named Card1 and its value has a property named Game .现在LevelCardGroupCurrentItem将是CarousalView ,它包含一个名为Card1的属性,它的值有一个名为Game的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM