How can i use the progressbar

JosePereraJosePerera USMember ✭✭

Hello,
I have a doubt with the progress bar, actually i really don't know how to use it correctly, i have this progress bar declared:
<ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progressBarSplash" android:indeterminateBehavior="repeat" android:indeterminate="true" android:indeterminateOnly="false" />
and this is the code in the activity:
`protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        var progress = FindViewById<ProgressBar>(Resource.Id.progressBarSplash);
        progress.Visibility = ViewStates.Visible;
        VerifyProfile ();
        progress.Visibility = ViewStates.Gone;
        if (myProfile != null) {

        } else {
            StartActivity (typeof(CreateProfile));
        }
    }

    async void VerifyProfile()
    {
        await Task.Run(() => {
            myProfile = Profile.GetProfiles().FirstOrDefault();
        });
    }`

when the activity is executing don't show nothing...

Any help with that?

Posts

  • CheesebaronCheesebaron DKInsider, University mod

    You are not awaitng VerifyProfile, which means the code will just continue running.

  • JosePereraJosePerera USMember ✭✭

    Thanks, but i have a problem, you said that i miss the await on VerifyProfile(): I put this:
    await VerifyProfile ();
    and i got the error:
    Error CS4033: The await' operator can only be used when its containing method is marked with theasync' modifier (CS4033) (RealStateDiary.Android).
    Could you tell me how i should use the await??

  • LjusnanLjusnan DEMember ✭✭✭
    edited May 2015

    What about using an AsyncTask? In OnPreExecute show the ProgressBar, in DoInBackground do VerifyProfile and in OnPostExecute dismiss the ProgressBar.

  • TomOpgenorthTomOpgenorth CAXamarin Team Xamurai

    @jperera : Your best place to start learning about read up on async/await support here - http://developer.xamarin.com/guides/cross-platform/advanced/async_support_overview

    An AsyncTask would work (it's the "Java/Android" way of doing these things) too. It's your call (personally, I would favour async/await).

  • JosePereraJosePerera USMember ✭✭

    Thank you very much, do you have any example or something that you could comment me here to how use it because all the examples on the document is only about the download images or something and i think it is different of my case...
    Thank you very much again

  • CheesebaronCheesebaron DKInsider, University mod
    edited May 2015

    If we take your code as an entry point:

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.Main);
    
        var progress = FindViewById<ProgressBar>(Resource.Id.progressBarSplash);
        progress.Visibility = ViewStates.Visible;
        VerifyProfile ();
        progress.Visibility = ViewStates.Gone;
        if (myProfile != null) {
    
        } else {
            StartActivity (typeof(CreateProfile));
        }
    }
    
    async void VerifyProfile()
    {
        await Task.Run(() => {
            myProfile = Profile.GetProfiles().FirstOrDefault();
        });
    }
    

    Things to fix:

    1. Stop lying about VerifyProfile being async, it is not and you are just wrapping stuff in Task.Run go full out on async/await! So think about making GetProfiles() async internally. That really depends on what it is doing inside, how you want to approach that.
    2. If we ignore GetProfiles() and continue just using what you have the two things that you need to fix just to get that piece of code to work is:
      • await VerifyProfile()
      • mark OnCreate async
      • async void is a no no, thrown exceptions will get eaten and not bubble up, meaning you can't catch them :( Solution is to make async methods return a Task.

    So now your code will look like:

    protected override async void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.Main);
    
        var progress = FindViewById<ProgressBar>(Resource.Id.progressBarSplash);
        progress.Visibility = ViewStates.Visible;
        await VerifyProfile ();
        progress.Visibility = ViewStates.Gone;
        if (myProfile != null) {
    
        } else {
            StartActivity (typeof(CreateProfile));
        }
    }
    
    Task VerifyProfile()
    {
        return Task.Run(() => {
            myProfile = Profile.GetProfiles().FirstOrDefault();
        });
    }
    

    You should also be careful with using member variables when doing async stuff as you might run into a race condition. Another thing to consider is to use CancellationToken where applicable. For instance if you are fetching some profile information to display in an Activity and you are navigating away, you might want to cancel the Task instead of letting it run.

  • JosePereraJosePerera USMember ✭✭

    thank you very much for your help i appreciate a lot, now is working with your help thanks again...

  • Rasheed_ITLecRasheed_ITLec USMember ✭✭

    Here is Easiest Way To Implement #ProgressBar Inside #Xamarin.Forms Using #NuGet By #ITLec @ITLec
    http://www.itlec.com/2017/08/easiest-way-to-implement-progress-bar-xamarin.html

Sign In or Register to comment.