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

Multiple IsVisible performance question

$
0
0

Hi Guys,

I'm having a little problem that I've recreated below in that setting the IsVisible property of several child controls seems to take a lot longer than I was expecting. There's a timer at the top which shows the initial load is a approx 70ms whereas setting 20 labels takes upwards of 3000ms, even regenrating the content .

I've tried a couple of options including

  • setting the parent to invisible while the labels are set

  • removing the children from the list and re-adding

but to no avail.

I was assuming that this was being caused by the UI being redrawn each time the IsVisible is changed and was hoping there may be a way of disabling the redraw of the screen until I'm finished. I've noticed there's a batch procedure for properties of the same view, but can't find anything for the same property of different views. Is there anything like this?

Of course the other option would be to recreate the page when the visibility changes and while that is fine in my test app, the original code is a touch more involved and the initial load itself is +3000ms.

As a bit of background, the need for this essentially arises from workflow, I have a page that when a view is interacted with (a check box for instance) this then effects the visibility of other views on this page.

Code:

public class PlayPage : ContentPage
{
public PlayPage()
{
this.Content = GenerateContent();
}

    public View GenerateContent()
    {
        var totst = DateTime.Now;
        var layout = new StackLayout();

        layout.Spacing = 10;
        layout.VerticalOptions = LayoutOptions.FillAndExpand;
        layout.Orientation = StackOrientation.Vertical;
        layout.HorizontalOptions = LayoutOptions.FillAndExpand;
        BackgroundColor = Color.Blue;

        var loadtime = new Label { Text = "(ms): " };

        var totoggle = new StackLayout();
        for (int i = 0; i < 30; i++)
        {
            totoggle.Children.Add(new Label { Text = "Label Toggle #" + i });
        }
        var toggle = new Button { Text = "Toggle Visiblity" };
        toggle.Clicked += (sender, args) =>
        {
            var st = DateTime.Now;
            bool newvis = !totoggle.Children.First().IsVisible;
            foreach (var child in totoggle.Children)
            {
                child.IsVisible = newvis;
            }
            loadtime.Text += (newvis ? "show" : "hide") + ", " + (DateTime.Now - st).TotalMilliseconds + "; ";
        };

        var togglep = new Button { Text = "Toggle Visiblity (with parent)" };
        togglep.Clicked += (sender, args) =>
        {
            var st = DateTime.Now;
            bool newvis = !totoggle.Children.First().IsVisible;
            //totoggle.BatchBegin();
            totoggle.IsVisible = false;
            foreach (var child in totoggle.Children)
            {
                child.IsVisible = newvis;
            }
            totoggle.IsVisible = true;
            //totoggle.BatchCommit();
            loadtime.Text += (newvis ? "show" : "hide") + ", " + (DateTime.Now - st).TotalMilliseconds + "; ";
        };

        var toggle2 = new Button { Text = "Attach/Detach" };
        var children = new List<View>();
        toggle2.Clicked += (sender, args) =>
        {
            var st = DateTime.Now;
            bool newvis = children.Any();
            if (newvis)
            {
                foreach (var child in children)
                {
                    totoggle.Children.Add(child);
                }
                children.Clear();
            }
            else
            {
                children = totoggle.Children.ToList();
                totoggle.Children.Clear();
            }

            loadtime.Text += (newvis ? "attach" : "detach") + ", " + (DateTime.Now - st).TotalMilliseconds + "; ";
        };

        var toggle2p = new Button { Text = "Attach/Detach (with parent)" };
        var childrenp = new List<View>();
        toggle2p.Clicked += (sender, args) =>
        {
            var st = DateTime.Now;
            bool newvis = childrenp.Any();
            if (newvis)
            {
                this.Content = null;
                //layout.Children.Remove(totoggle);
                foreach (var child in childrenp)
                {
                    totoggle.Children.Add(child);
                }
                childrenp.Clear();
                this.Content = layout;
                //layout.Children.Add(totoggle);
            }
            else
            {
                childrenp = totoggle.Children.ToList();
                totoggle.Children.Clear();
            }

            loadtime.Text += (newvis ? "attach" : "detach") + ", " + (DateTime.Now - st).TotalMilliseconds + "; ";
        };

        var regen = new Button { Text = "Regenerate" };
        regen.Clicked += (sender, args) =>
        {
            var st = DateTime.Now;
            //this.Content = null;
            //this.Content = GenerateContent();
            GenerateContent();
            loadtime.Text += "regen, " + (DateTime.Now - st).TotalMilliseconds + "; ";
        };

        layout.Children.Add(loadtime);
        layout.Children.Add(toggle);
        layout.Children.Add(togglep);
        layout.Children.Add(toggle2);
        layout.Children.Add(toggle2p);
        layout.Children.Add(regen);

        layout.Children.Add(totoggle);


        loadtime.Text += "initial load, " + (DateTime.Now - totst).TotalMilliseconds + "; ";

        return layout;
    }
}

Thanks in advance,

Paul.


Viewing all articles
Browse latest Browse all 58056

Trending Articles