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

How to properly indicate page is loading?

$
0
0

Hello,

I have tried many different ways of doing this and none are consistent across all platforms. I have my testpage.cs that loads 400 labels in a grid which takes several seconds and am in need of a way to indicate that it is busy loading. I will most likely have other pages in my app that need the same ability.

Could someone please show me with-in my sample code how to display a loading page/popup while my page is loading? If there isn't a clean way to display a loading page an activity indicator would be ok but I would prefer a loading page. My main issue is finding a solution that displays from the moment my 'click me' button is pressed until the page is actually loaded. I haven't been able to come up with anything that displays the entire time on all 3 platforms.

Below is a sample with all my previous attempts removed.

**MainPage.cs
**
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using Xamarin.Forms;

        namespace App1
        {
            public class MainPage : ContentPage
            {
                public MainPage()
                {
                    this.Title = "Main Page";
                    Button button = new Button();
                    button.Text = "Click ME";
                    button.Clicked += button_Clicked;
                    this.Content = button;
                }
                async void button_Clicked(object sender, EventArgs e)
                {
                    await this.Navigation.PushAsync(new TestPage());
                }
            }

        }


**TestPage.cs  **

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Xamarin.Forms;
    using System.Windows.Input;

    namespace App1
    {
        public class TestPage : ContentPage
        {
            public TestPage()
            {
                LoadPage();
            }

            private void LoadPage()
            {
                Label Header = new Label
                {
                    Text = "Test Page",
                    Font = Font.SystemFontOfSize(25, FontAttributes.Bold),
                    HorizontalOptions = LayoutOptions.Center
                };
                this.Title = "Test Page";
                this.Padding = new Thickness(5, Device.OnPlatform(40, 20, 20), 5, 5);
                StackLayout layout = new StackLayout
                {
                    VerticalOptions = LayoutOptions.FillAndExpand,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    Orientation = StackOrientation.Vertical,
                };
                    RowDefinition RD = new RowDefinition { Height = GridLength.Auto };
                    ColumnDefinition CD1 = new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) };
                    ColumnDefinition CD2 = new ColumnDefinition { Width = GridLength.Auto };
                    ColumnDefinitionCollection CDC = new ColumnDefinitionCollection();
                    CDC.Add(CD1);
                    CDC.Add(CD2);
                    RowDefinitionCollection RDC = new RowDefinitionCollection();
                    for (int i = 0; i < 200; i++)
                    {
                        RDC.Add(RD);
                    }
                    var grid = new Grid
                    {
                        ColumnSpacing = 10,
                        IsClippedToBounds = false,
                        RowDefinitions = RDC,
                        ColumnDefinitions = CDC
                    };
                    for (int i = 0; i < 200; i++)
                    {
                            Label labelReq = new Label();
                            labelReq.Text = "Label " + i;
                            if (i == 2)
                                labelReq.Text = "Some really long text that should go to the next line.";
                            Label labeldate = new Label();
                            labeldate.Text = "__/__/__";
                            grid.Children.Add(labelReq, 0, i);
                            grid.Children.Add(labeldate, 1, i);
                    }
                    layout.Children.Add(grid);

                var scroll = new ScrollView();

                var stack = new StackLayout
                {
                    Children = { Header,  layout }
                };
                this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);
                scroll.Content = stack;
                this.Content = scroll;
            }
        }
    }

**Loading.cs**

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App1
{
    public class Loading : ContentPage
    {

        protected override void OnAppearing()
        {
            base.OnAppearing();
            IsBusy = true;
        }
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            IsBusy = false;
        }
        public Loading()
        {
            Label Header = new Label
            {
                Text = "Loading",
                Font = Font.SystemFontOfSize(25, FontAttributes.Bold),
                HorizontalOptions = LayoutOptions.Center
            };
            this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);
            this.Content = Header;
        }
    }

}

Viewing all articles
Browse latest Browse all 58056

Trending Articles



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