c# - UIProgressView 未从 BackgroundWorker.ProgressChanged 更新

标签 c# ios xamarin xamarin.ios backgroundworker

我正在将我的 Xamarin.Android 应用程序移植到 Xamarin.iOS,我无法更新进度条,哪里出错了?

updateProgressBar() 中正确设置了值,本示例中的 progressBarValue 设置为预期的 0.25,但 UIProgressView 未在屏幕上更新。 progressBar 是 Storyboard 上的一个 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;
    }
}

我还尝试使用 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);
    }
}

并使用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);
        });
    }
}

最佳答案

您需要确保您的 UI 更新正在 main thread 上运行

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

关于c# - UIProgressView 未从 BackgroundWorker.ProgressChanged 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37071582/

相关文章:

c# - 签名出现在 AttributeStatement 旁边时出现 SAML 2.0 错误 (.Net 4.5)

c# - NHibnerate 数据设计建议

svn - 将 SVN 与 Xamarin Studio 结合使用。还有其他选择吗?

c# - 如何使用项目按钮单击将 ListView 值传递到另一个页面?

c# - 将颜色声明为常量

c# - 使用 MVVM 模式中的 DXGrid 绑定(bind)/编辑数据的最佳选择是什么?

iOS 8 JavaScript 到 UIWebView 的通信错误

ios - 如何解决导航限制警告?

php - 邀请 Facebook 好友使用来自 Objective C 或 PHP 的应用程序

android - 在 Fragment 中添加 SearchView