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

Strange issue with CarouselPage (full code example)

$
0
0

I've been battling with the CarouselPage control for some time now (apologies for yet another thread, but this one deals with a specific issue and I'm very close to having the control working exactly as I need it to).

When I run this app I can swipe between each of the 9 pages quickly and seamlessly. Pages 2-9 each contain 2 StackLayouts and a Button, this doesn't seem to have any effect on performance or scrolling ability.

When the "i" button is tapped I add another StackLayout which contains some text, this again has no effect on performance or scrolling ability.

It wasn't until I added the last StackLayout (which contains another 5 buttons that sit to the left of the "i" button) I started experiencing problems and I'll do my best to explain what those are... It isn't that the scrolling is slow (which I experienced when creating and inserting all elements in one go in my GetMainPage function) or stutters (when I tried to create/insert elements using the CarouselPage.CurrentPageChanged event)... the problem is that sometimes my swipe gesture is completely ignored, I'll try and swipe to the next page and nothing happens (the swipe is registered in the console), swipe again, nothing happens, then what seems like a random number of attempts later, the swipe will work and the page will change just as quickly/smoothly as when no controls are on the pages. The problem ONLY occurs when all the elements are visible, you'll see from code that when the "i" button becomes an "X" button and is tapped, the StackLayouts containing the text and the buttons are each removed, thus ensuring smooth and consistent swiping.

Code below for App.cs and MainActivity.cs (you can ignore SceneNumber as it isn't used yet)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace CarouselApp
{
    public class App
    {
        public static Page GetMainPage()
        {
            CarouselPage MyCarouselPage = new CarouselPage
            {
                Children =
                {
                    new ContentPage { BackgroundColor = Color.Navy },
                    new ContentPage { Content = CreateStackLayout(0) },
                    new ContentPage { Content = CreateStackLayout(1) },
                    new ContentPage { Content = CreateStackLayout(2) },
                    new ContentPage { Content = CreateStackLayout(3) },
                    new ContentPage { Content = CreateStackLayout(4) },
                    new ContentPage { Content = CreateStackLayout(5) },
                    new ContentPage { Content = CreateStackLayout(6) },
                    new ContentPage { Content = CreateStackLayout(7) }
                }
            };

            return MyCarouselPage;
        }

        private static StackLayout CreateStackLayout(int SceneNumber)
        {
            Button InformationButton = new Button { Text = "i" };



            StackLayout TopStackLayout = new StackLayout
            {
                Children = 
                {
                    new Label
                    {
                        HorizontalOptions = LayoutOptions.CenterAndExpand /*Pushes buttons to right*/
                    }, 
                    InformationButton // Height defaults to 48
                },
                Orientation = StackOrientation.Horizontal,
            };



            StackLayout MainLayout = new StackLayout
            {
                Children = 
                {
                    TopStackLayout
                },
                Padding = 0,
                Spacing = 0
            };



            InformationButton.Clicked += (s, e) =>
            {
                if (MainLayout.Children.Count == 2)
                {
                    MainLayout.Children.RemoveAt(1);
                    TopStackLayout.Children.RemoveAt(1);

                    InformationButton.Text = "i";
                }
                else if (MainLayout.Children.Count == 1)
                {
                    Button Page1Button = new Button { Text = "1" }; 
                    Button Page2Button = new Button { Text = "2" };
                    Button Page3Button = new Button { Text = "3" };
                    Button Page4Button = new Button { Text = "4" };
                    Button Page5Button = new Button { Text = "5" };

                    StackLayout PageStackLayout = new StackLayout
                    {
                        Children =
                        {
                            Page1Button,
                            Page2Button,
                            Page3Button,
                            Page4Button,
                            Page5Button
                        },
                        Opacity = 1,
                        Orientation = StackOrientation.Horizontal
                    };



                    if (TopStackLayout.Children.Count == 2)
                    {
                        TopStackLayout.Children.Insert(1, PageStackLayout); // Commenting out this line seems to fix the issue as it stops Buttons 1-5 from being inserted
                    }



                    ScrollView TextScrollView = new ScrollView
                    {
                        Content = new Label
                        {
                            Font = Font.SystemFontOfSize(14),
                            Opacity = 1,
                            Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse faucibus varius ante at blandit. Nulla diam dui, blandit at nunc et, dapibus consequat massa. Sed facilisis mi libero, sit amet sagittis elit pulvinar sed. Aenean pretium blandit volutpat. Donec id elit ipsum. Maecenas suscipit ut sem sodales laoreet. Pellentesque gravida turpis aliquam nunc fermentum, quis lacinia risus lobortis. Sed nulla felis, tempor non tristique et, pulvinar eu sem. Donec id vulputate turpis. Nulla sagittis blandit erat. Vivamus consequat erat tortor, sed semper erat finibus quis. Etiam non tellus id nunc consequat posuere sit amet at quam. Praesent ut imperdiet nisi, a rutrum tellus.",
                            TextColor = Color.Black
                        }
                    };
                    TextScrollView.IsClippedToBounds = true;



                    MainLayout.Children.Add(new StackLayout
                    {
                        BackgroundColor = Color.FromHex("CCB8B8B8"),
                        Children =
                        {
                            TextScrollView
                        },
                        Opacity = 1,
                        Padding = 10,
                        VerticalOptions = LayoutOptions.FillAndExpand,
                        Spacing = 0
                    });

                    InformationButton.Text = "X";
                }                
            };

            return MainLayout;
        }
    }
}


using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using Xamarin.Forms.Platform.Android;

namespace CarouselApp.Droid
{
    [Activity(Label = "CarouselApp", MainLauncher = true, ScreenOrientation = Android.Content.PM.ScreenOrientation.Landscape)]
    public class MainActivity : AndroidActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Window.AddFlags(WindowManagerFlags.Fullscreen);            

            Xamarin.Forms.Forms.Init(this, bundle);

            SetPage(App.GetMainPage());
        }
    }
}

Some output below:

11-12 15:27:09.962 I/SurfaceTextureClient( 3890): [STC::queueBuffer] (this:0x57c27c58) fps:18.06, dur:1162.87, max:456.57, min:18.87
11-12 15:27:13.593 I/SurfaceTextureClient( 3890): [STC::queueBuffer] (this:0x57c27c58) fps:3.86, dur:3630.13, max:3356.01, min:14.18
11-12 15:27:15.718 I/SurfaceTextureClient( 3890): [STC::queueBuffer] (this:0x57c27c58) fps:9.41, dur:2125.33, max:1441.63, min:17.25
11-12 15:27:17.635 I/SurfaceTextureClient( 3890): [STC::queueBuffer] (this:0x57c27c58) fps:10.43, dur:1916.63, max:1231.55, min:17.21
11-12 15:27:19.141 I/SurfaceTextureClient( 3890): [STC::queueBuffer] (this:0x57c27c58) fps:13.94, dur:1506.55, max:819.91, min:17.17

The first 4 are examples of swipes being detected but nothing happens, the 5th worked and the page changed quickly and smoothly.

I have also noticed that I don't experience this problem on a Tablet with 1GB RAM, the device I'm using is a Tecno P5S with 512 MB RAM.

Does anyone have any ideas?


Viewing all articles
Browse latest Browse all 58056

Trending Articles