Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make CarouselView work with dynamic item views/models #2

Open
biliktamas79 opened this issue Jul 20, 2015 · 0 comments
Open

Comments

@biliktamas79
Copy link

Hi!
I would like to make a CarouselView that has dynamic view/model types, but sadly I can't make it working.
I wanted to mix it with your CarouselViewWithVMViews implementation that uses a ListView to show dynamic view/model items.

I've merged this sample app with your dynamic ListView item sample code from here:

My modifications are:

public class DynamicTemplateLayout : ContentView// ViewCell
{
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();

        var vm = BindingContext as ICarouselViewModel;
        var page = vm.View;
        page.BindingContext = vm;
        this.Content = page;
    }
}

public class HomePage2 : ContentPage
{
RelativeLayout relativeLayout;
CarouselLayout.IndicatorStyleEnum _indicatorStyle = CarouselLayout.IndicatorStyleEnum.Dots;
SwitcherPageViewModel viewModel;

    public HomePage2()
    {
        viewModel = new SwitcherPageViewModel();
        BindingContext = viewModel;

        Title = "Dynamic Dots";//_indicatorStyle.ToString();

        relativeLayout = new RelativeLayout 
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand
        };

        var pagesCarousel = CreatePagesCarousel();
        var dots = CreatePagerIndicatorContainer();

        relativeLayout.Children.Add (pagesCarousel,
            Constraint.RelativeToParent ((parent) => { return parent.X; }),
            Constraint.RelativeToParent ((parent) => { return parent.Y; }),
            Constraint.RelativeToParent ((parent) => { return parent.Width; }),
            Constraint.RelativeToParent ((parent) => { return parent.Height/2; })
        );

        relativeLayout.Children.Add (dots, 
            Constraint.Constant (0),
            Constraint.RelativeToView (pagesCarousel, 
                (parent,sibling) => { return sibling.Height - 18; }),
            Constraint.RelativeToParent (parent => parent.Width),
            Constraint.Constant (18)
        );

        Content = relativeLayout;
    }

    CarouselLayout CreatePagesCarousel ()
    {
        List<ICarouselViewModel> pages = new List<ICarouselViewModel>()
        {
            new PageOneViewModel(),
            new PageTwoViewModel()
        };
        var carousel = new CarouselLayout {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            IndicatorStyle = _indicatorStyle,
            ItemsSource = pages, 
            ItemTemplate = new DataTemplate(typeof(DynamicTemplateLayout))
        };
        //carousel.SetBinding(CarouselLayout.ItemsSourceProperty, "Pages");
        //carousel.SetBinding(CarouselLayout.SelectedItemProperty, "CurrentPage", BindingMode.TwoWay);

        return carousel;
    }

    View CreatePagerIndicatorContainer()
    {
        return new StackLayout {
            Children = { CreatePagerIndicators() }
        };
    }

    View CreatePagerIndicators()
    {
        var pagerIndicator = new PagerIndicatorDots() { DotSize = 5, DotColor = Color.Black };
        pagerIndicator.SetBinding (PagerIndicatorDots.ItemsSourceProperty, "Pages");
        pagerIndicator.SetBinding (PagerIndicatorDots.SelectedItemProperty, "CurrentPage");
        return pagerIndicator;
    }
}

and in the SwitcherPage, the dots button's command was changed to use this new HomePage2:

Command = new Command((obj) => Navigation.PushAsync(new HomePage2()))

What did I miss here?
It compiles, but when you choose "Dots" button on the main screen, it throws runtime exception on Android (on iOS I did not try) inside the HomePage2's CreatePagesCarousel() method when it tries to instantiate the CarouselLayout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants