[英]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? 我正在将我的Xamarin.Android应用程序移植到Xamarin.iOS,我无法让我的进度条更新,我哪里出错了?
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. 这些值在updateProgressBar()
正确设置,并且此示例中的progressBarValue
按预期设置为0.25,但UIProgressView未在屏幕上更新。 progressBar
is a UIProgressView
on the storyboard. progressBar
是故事板上的UIProgressView
。
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)
我也尝试过使用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
并使用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 您需要确保您的UI更新正在主线程上运行
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. 我的问题是我将UIProgressView.TintColor
设置为我的代码中的其他地方的无效值 ,因此ProgressView
一直在更新,但它只是不可见。
progressView.TintColor = iOSHelpers.GetColor(GenericHelpers.colorPrimaryGreenRGBA);
iOSHelpers 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 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. 一旦我将colorPrimaryGreenRGBA
值更改为浮点数并在GetColor
方法中将RGB通道除以255,那么它就是可见颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.