Xamarin Android ProgressBar

MicrosoftMicrosoft Member ✭✭
edited September 2018 in Xamarin.Android

Hey all,

I'm struggling to use the ProgressBar in the MVVMCross structure of Xamarin Android. Originally, I wanted to bind the visibility of the ProgressBar to a field in my ViewModel. That proved difficult, I tried several permutations and tried suggestions from here: How to set visibility for ProgressBar in android MvvmCross Xamarin

I do have other elements such as buttons I have been able to successfully bind else where in my project, so I'm not sure why that was such a big issue. After many tries I decided to try setting the Visibility pragmatically in the ViewModel. I've tried several methods but what I finally thought would for sure work doesn't.

Xaml:

<ProgressBar
    android:id="@+id/progressBarMap"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="gone"
    style="@android:style/Widget.ProgressBar.Large" />

I've tried with and without having a default visibility. I've tried the local:MvxBind with several normal methods of binding, and then using a custom converter or with the Visibility plugin.

In my Fragment I regretfully set a FragmentActivity property in my ViewModel just to see if I can get this to work in some form (OnCreateView):

ViewModel.FragActivity = this.Activity;
ViewModel.Progress = view.FindViewById<Android.Widget.ProgressBar>(Resource.Id.progressBarMap);

Notice I did also try setting the ProgressBar to a field called Progress, and use it in my ViewModel.

Here is where I'm manipulating the ProgressBar in my ViewModel:

private void AddOfflinePoints()
{
    try
    {
        FragActivity.RunOnUiThread(() => 
        {
            //I've also tried using the above Progress property. That wasn't null but did not update.
            ProgressBar barOfProgression = FragActivity.FindViewById<ProgressBar>(Resource.Id.progressBarMap);
            //barOfProgression is null :(
            barOfProgression.Visibility = ViewStates.Visible;
            barOfProgression.Enabled = true;
        });

        //Code to run while spinning the progress bar (It is inside Task.Run)
    }
    catch (Exception)
    {
        //Exception Handling code...
    }

    FragActivity.RunOnUiThread(() => Progress.Visibility = ViewStates.Gone);
}

What am I doing wrong? I've tried many permutations, anything on Google and consulted the limited Xamarin documentation. Any help or pointers is much appreciated.

Thank you.

Best Answer

  • MicrosoftMicrosoft ✭✭
    Accepted Answer

    Solved by adding the following code to the LinkerPleaseInclude:

    public static void Include(ProgressBar progressBar)
    {
       progressBar.Click += (s, e) => progressBar.Visibility = progressBar.Visibility - 1;
    }
    

Answers

  • MicrosoftMicrosoft Member ✭✭
    Accepted Answer

    Solved by adding the following code to the LinkerPleaseInclude:

    public static void Include(ProgressBar progressBar)
    {
       progressBar.Click += (s, e) => progressBar.Visibility = progressBar.Visibility - 1;
    }
    
Sign In or Register to comment.