2016-05-06 16 views
2

Ich bin dabei, meine Xamarin.Android App auf Xamarin.iOS zu portieren, ich kann mein Fortschrittsbalken nicht aktualisieren, wo liege ich falsch?UIProgressView aktualisiert nicht von BackgroundWorker.ProgressChanged

Die Werte werden in updateProgressBar() korrekt und progressBarValue in diesem Beispiel wie erwartet auf 0,25 festgelegt, aber das UIProgressView wird nicht auf dem Bildschirm aktualisiert. progressBar ist ein UIProgressView auf dem 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; 
    } 
} 

Ich habe auch versucht SetProgress(progressBarValue,true) mit

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

und mit 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); 
     }); 
    } 
} 
+1

Was sind die Alternativen zum Down-Voting? Down-Voting sollte für Extremfälle vorbehalten bleiben. Es ist nicht als Ersatz für Kommunikation und Bearbeitung gedacht. Wenn etwas nicht stimmt, hinterlassen Sie einen Kommentar oder bearbeiten Sie den Beitrag, um ihn zu korrigieren. http://stackoverflow.com/help/privileges/vote-down – tallpaul

+2

Ich verstehe nicht, warum Sie unten abgestimmt haben, Sie haben sogar Ihren Code +1 –

Antwort

0

Wie oft der Fall, mein Problem war nicht mit meinem Code in der Frage, oder die Frage überhaupt, ich kopierte es in eine neue Lösung und es hat gut funktioniert.

Incase jemand findet diese in der Zukunft und hat den gleichen Fehler gemacht:

Mein Problem war, dass ich UIProgressView.TintColor auf einen ungültigen Wert an anderer Stelle in meinem Code gesetzt hatte, so die ProgressView wurde die ganze Zeit modernisiert werden, aber es war einfach nicht sichtbar.

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 }; 

Sobald ich die Werte der RGB-Kanäle, die von 255 im GetColor Verfahren war es schwimmt verändert hatte und unterteilt dann eine sichtbare Farbe.

0

Sie benötigen, um Ihre UI-Updates sicher sein laufen auf dem main thread

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

Vielen Dank für Ihre Zeit Jason, versuchte ich diese Methode, aber immer noch ohne Glück. Ich hatte (fälschlicherweise) angenommen, dass 'BackgroundWorker.ProgressChanged' automatisch im UIhread wäre, wenn es von einem' ViewController' initiiert wurde. – tallpaul

+0

a. Sind Sie sicher, dass Ihre bg-Methoden aufgerufen werden? b. Der Fortschrittsbalkenwert ist ein Single, Sie verwenden einen Float - es kann ein Konvertierungsproblem sein – Jason

+0

.NET 'System.Single' == C#' float' :-) – poupou

Verwandte Themen