Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

CarouselPage - Issues with opening at a specific page

$
0
0

Hi all -

I'm currently trying to implement a simple CarouselPage where users can swipe left and right to navigate a collection of images. This works fine. However, I want the user to be able to pick a starting point - an image to display first and be able to swipe away from. The correct page is chosen successfully but when it opens the content is not displayed, unless it is either the first or last data in the collection. I'd appreciate any advice.

My carouselpage:

public class PhotoCarouselPage : CarouselPage
{
    private int initialOpen = 0;
    public int selectedIndex = 0;

    public PhotoCarouselPage(Photo current)
    {
        CurrentPageChanged += PhotoCarouselPage_CurrentPageChanged;

        ItemsSource = App.SelectedCondition.Photos;
        ItemTemplate = new DataTemplate(() =>
        {
            return new PhotoPage(this);
        });

        for(int i = 0; i < App.SelectedCondition.Photos.Count; i++)
        {
            if(App.SelectedCondition.Photos[i].Id == current.Id)
            {
                initialOpen = i;
                break;
             }
        }

        CurrentPage = Children.ElementAt(initialOpen);
        //SelectedItem = ((List<Photo>)ItemsSource)[initialOpen];
    }

    private void PhotoCarouselPage_CurrentPageChanged(object sender, System.EventArgs e)
    {
        selectedIndex = Children.IndexOf(CurrentPage);
    }
}

Here is the photo page class. The content isn't loaded in the constructor as the current implementation of CarouselPage loads all pages at once. Content is loaded when within two pages of the position. Everything loads fine, but only if you start at the first or last element and then swipe through:

public class PhotoPage : ContentPage
{
    PhotoCarouselPage thisParent;
    Binding dateBinding;
    bool initialized = false;
    int thisIndex = -1;

    public PhotoPage(PhotoCarouselPage parentPage)
    {
        thisParent = parentPage;
        thisParent.CurrentPageChanged += ThisParent_CurrentPageChanged;
    }

    private void ThisParent_CurrentPageChanged(object sender, EventArgs e)
    {
        if(thisIndex == -1)
        {
            thisIndex = thisParent.Children.IndexOf(this);
        }

        int difference = Math.Abs(thisParent.selectedIndex - thisIndex);

        if(difference <= 2 && !initialized)
        {
            InitialisePage();
        }
        else if(difference > 2 && initialized)
        {
            initialized = false;
            Content = null;
            GC.Collect();
        }
    }

    public void InitialisePage()
    {
        // Declare layouts and scrollview here

        Image theImage = new Image
        {
            // image vars
        };

        theImage.SetBinding(Image.SourceProperty, "Url");

        content.Children.Add(theImage, Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((parent) => {
            return (parent.Width);
        }));

        // Label declarations here...

        StackLayout details = new StackLayout
        {
            // layout vars...
            Children =
            {
                // adding labels here
            }
        };

        content.Children.Add(details, Constraint.Constant(0),
        Constraint.RelativeToView(theImage, (parent, view) =>
        {
            return view.Y + view.Height + 10;
        }));

        scroller.Content = content;

        layout.Children.Add(scroller);
        Content = layout;
        initialized = true;
    }
}

Any help would be appreciated.


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>