Has anyone done a splash screen that shows loading progress from the PCL project?

JKnottJKnott Member ✭✭✭
edited October 19 in Xamarin.Forms

I am trying to build a splash screen for my app which has a lengthy startup process.
I have been trying to use messagingcenter to contend with this, but I am running into problems with getting the message subscriber to update a progress bar in my android project from the PCL project.

What I have done so far is as follows .

[app.droid]

[Activity(  Theme = "@style/splashscreen",
            MainLauncher = true,
            NoHistory = true)]
    public class SplashActivity : Activity
 {
        protected override void OnCreate(Bundle savedInstanceState)
        {
             try
                {
                     base.OnCreate(savedInstanceState);
                    SetContentView(Resource.Layout.Startup);
                    Android.Widget.ProgressBar progbar= FindViewById<Android.Widget.ProgressBar>(Resource.Id.splashProgress);
                    MessagingCenter.Subscribe<PopupPage, int>(this, "pbStep",
                            (progressb, x) =>
                            {
                                progbar.Progress += x;
                                progbar.RefreshDrawableState();
                            });
            //This invokes my long running code in the PCL project.
                    BeginLongBits();
                    StartActivity(typeof(MainActivity));
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"***Event***{ex.Message}==={ex.InnerException}");
                }
        }
}

Startup.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="25px"
        android:minHeight="25px">
        <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:minWidth="25px"
            android:minHeight="25px"
            android:id="@+id/splashProgress"
            android:layout_marginBottom="1.0dp"
            android:layout_marginTop="353.0dp"
            android:layout_marginLeft="0.0dp"
            android:max="100"
            android:drawingCacheQuality="auto" />

    </LinearLayout>

[app.pcl]
in various locations that represent steps in the progress of finishing the start up I use a send like this

            MessagingCenter.Send<object, int>(this, "pbStep", 10);

Best Answer

  • JKnottJKnott ✭✭✭
    edited October 20 Accepted Answer

    LOL Please disregard... I again found my stupidity...
    I had been using the wrong signature for the subscriber on the android side....
    SO if anyone wants to implement a counter on their load screen, this is actually a good approach to take (at least as far as I can tell).

    So in my above in my above example, make this simple change, and whallah you have a progressbar showing your loading progress.
    [app.droid]
    Original...

         MessagingCenter.Subscribe<PopupPage, int>(this, "pbStep",
                            (progressb, x) =>
                            {
                                progbar.Progress += x;
                                progbar.RefreshDrawableState();
                            });
    

    Fixed...

         MessagingCenter.Subscribe<object, int>(this, "pbStep",
                            (progressb, x) =>
                            {
                                progbar.Progress += x;
                            });
    

    and for your message sender, of course

            MessagingCenter.Send<object, int>(this, "pbStep", 10);
    

Answers

  • JKnottJKnott Member ✭✭✭
    edited October 20 Accepted Answer

    LOL Please disregard... I again found my stupidity...
    I had been using the wrong signature for the subscriber on the android side....
    SO if anyone wants to implement a counter on their load screen, this is actually a good approach to take (at least as far as I can tell).

    So in my above in my above example, make this simple change, and whallah you have a progressbar showing your loading progress.
    [app.droid]
    Original...

         MessagingCenter.Subscribe<PopupPage, int>(this, "pbStep",
                            (progressb, x) =>
                            {
                                progbar.Progress += x;
                                progbar.RefreshDrawableState();
                            });
    

    Fixed...

         MessagingCenter.Subscribe<object, int>(this, "pbStep",
                            (progressb, x) =>
                            {
                                progbar.Progress += x;
                            });
    

    and for your message sender, of course

            MessagingCenter.Send<object, int>(this, "pbStep", 10);
    
  • LandLuLandLu Member, Xamarin Team Xamurai

    If this issue has been solved by yourself you can also mark it to give some hints for other community members.

Sign In or Register to comment.