[PRISM] Carousel Page Binding ItemsSource

voidstreamvoidstream FRMember ✭✭✭
edited December 2016 in Xamarin.Forms

Hello guys,

I'm trying to bind a CarouselPage.ItemsSource but i only got a result when the list is populate on the constructor.
I read this in Xamarin Documentation:
"The CarouselPage is populated with data by setting the ItemsSource property in the constructor for the code-behind file"
We can only bind itemsSource on the constructor?
I need to bind items in the OnNavigatedTo methods (PRISM MVVM) :neutral:

XAML:

<?xml version="1.0" encoding="utf-8" ?>
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
              prism:ViewModelLocator.AutowireViewModel="True"
              x:Class="APP.Views.Manager.Needs"
              ItemsSource="{Binding Pages}">

    <CarouselPage.ItemTemplate>
        <DataTemplate>
            <ContentPage BackgroundColor="Red">
                <Label Text="Hello world!"/>
            </ContentPage>
        </DataTemplate>
    </CarouselPage.ItemTemplate>

</CarouselPage>

ViewModel:

    public ObservableCollection<ContentPage> Pages { get; private set; }

    public async void OnNavigatedTo(NavigationParameters parameters)
    {
        List<ContentPage> ContentPages = new List<ContentPage>();
        ContentPages.Add(new ContentPage());
        Pages = new ObservableCollection<ContentPage>(ContentPages);
    }

Best regards

Best Answer

Answers

  • wilconwilcon USMember ✭✭✭

    If I am right, when you're using an async method you need to use await on an action in there to actually run things in async.
    Correct me if I am wrong on this.

  • voidstreamvoidstream FRMember ✭✭✭

    @WilcotenVoorde said:
    If I am right, when you're using an async method you need to use await on an action in there to actually run things in async.
    Correct me if I am wrong on this.

    Yes it's true, the code in my method is not complete. I hide a "useless part" for the forum =)

  • Hi there,

    I'm trying to use a CarouselView to see some XAML Pages I already have built, but I can't get it work, did your carousel work with a list of content pages?? Did it shows shows the pages?

  • voidstreamvoidstream FRMember ✭✭✭
    edited August 9

    @FranciscoGonzalez.9224 said:
    Hi there,

    I'm trying to use a CarouselView to see some XAML Pages I already have built, but I can't get it work, did your carousel work with a list of content pages?? Did it shows shows the pages?

    On this topic i talk of CarouselPage which works only with ContentPage whereas CarouselView works with Layouts (ContentView, StackLayout etc...). I'm not sure but i think CarouselPage is obsolete and will be replaced by CarouselView !

  • NithyaNithya USUniversity ✭✭

    @voidstream I am trying to populate by carousel page but its not happening..im also using the prism model..
    tried your sample...added multiple pages to the collection but its didn't work can you help me where I might have went wrong

    List temp = new List();
    temp.Add(Color.Red);
    temp.Add(Color.Yellow);
    temp.Add(Color.Blue);
    List ContentPages = new List();
    for (int i = 0; i < temp.Count(); i++)
    {
    ContentPages.Add(new ContentPage() { BackgroundColor = temp[i] });

            }
            Pages = new ObservableCollection<ContentPage>(ContentPages);
    

    I have placed this piece of code in the OnNavigatedTo in the ViewModel

  • voidstreamvoidstream FRMember ✭✭✭

    @Nithya

    Can you try this (it's the 'same'):

    private ObservableCollection<ContentPage> _pages;
    public ObservableCollection<ContentPage> Pages { get { return _pages; } set { SetProperty(ref _pages, value); } }
    
    public async void OnNavigatedTo(NavigationParameters parameters)
    {
        List<Color> colors = new List<Color>
        {
            Color.Red,
            Color.Yellow,
            Color.Blue
        }
    
        List<ContentPage> contentPages = new List<ContentPage>();
    
        foreach(Color currentColor in colors)
        {
            contentPages.Add(new ContentPage
            {
                BackgroundColor = currentColor
            });
        }
    
        Pages = new ObservableCollection<ContentPage>(contentPages);     
    }
    

    Can you drop the XAML? Others binding works?

Sign In or Register to comment.