简体   繁体   中英

UIProgressView not updating from BackgroundWorker.ProgressChanged

I'm in the process of porting my Xamarin.Android app to Xamarin.iOS, I can't make my progress bar update, where am I going wrong?

The values are set in updateProgressBar() correctly and progressBarValue in this example is set as 0.25 as expected, but the UIProgressView is not updated on the screen. progressBar is a UIProgressView on the storyboard.

public BackgroundWorker backgroundWorker { get; private set; }
private float progressBarValue { get; set; }

public override void ViewDidAppear(bool animated)
{
    base.ViewDidAppear(animated);
    startBackgroundWorker();
}

private void startBackgroundWorker()
{
    if (backgroundWorker == null || backgroundWorker.CancellationPending) backgroundWorker = new BackgroundWorker();
    backgroundWorker.DoWork += (s, e) =>
    {
        //do stuff  
        backgroundWorker.ReportProgress(25);
        //do stuff
    };
    backgroundWorker.RunWorkerCompleted += (s, e) => { //do stuff };
    backgroundWorker.ProgressChanged += (s, e) => { updateProgressBar(e.ProgressPercentage); };
    backgroundWorker.WorkerReportsProgress = true;
    backgroundWorker.RunWorkerAsync();
}

private void updateProgressBar(float v)
{
    if (v > 0){
        float value = v / 100;
        progressBarValue = progressBarValue + value;
        if (progressBarValue > 1) progressBarValue = 1;
        progressBar.Progress = progressBarValue;
    }
}

I also tried using SetProgress(progressBarValue,true)

private void updateProgressBar(float v)
{
    if (v > 0){
        float value = v / 100;
        progressBarValue = progressBarValue + value;
        if (progressBarValue > 1) progressBarValue = 1;
        progressBar.SetProgress(progressBarValue,true);
    }
}

and using InvokeOnMainThread

private void updateProgressBar(float v)
{
    if (v > 0){
        float value = v / 100;
        progressBarValue = progressBarValue + value;
        if (progressBarValue > 1) progressBarValue = 1;
        InvokeOnMainThread ( () => {
            // manipulate UI controls
            progressBar.SetProgress(progressBarValue,true);
        });
    }
}

you need to be sure your UI updates are running on the main thread

InvokeOnMainThread ( () => {
    // manipulate UI controls
    progressBar.SetProgress(progressBarValue,true);
});

As is often the case, my problem was not related to my code in the question, or the question at all, I copied it into a new solution and it worked fine.

Incase anyone finds this in the future and has made the same mistake:

My problem was that I had set UIProgressView.TintColor to an invalid value elsewhere in my code so the ProgressView was updating all along but it just wasn't visible.

progressView.TintColor = iOSHelpers.GetColor(GenericHelpers.colorPrimaryGreenRGBA);

iOSHelpers

public static UIColor GetColor(int[] rgba)
{
    if (rgba.Length == 4)
        return UIColor.FromRGBA(rgba[0], rgba[1], rgba[2], rgba[3]);
    else return UIColor.Black;
}

GenericHelpers

public static int[] colorPrimaryGreenRGBA = { 140, 185, 50, 1 };

Once I had changed the colorPrimaryGreenRGBA values to floats and divided the RGB channels by 255 in the GetColor method it was then a visible color.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM