简体   繁体   English

Xamarin Android C#中的进度条和线程

[英]Progress bar and thread in Xamarin Android C#

I try to load my SQL using this method. 我尝试使用此方法加载我的SQL。 When I clicked the button login, the method is working and it shows the circle progress dialog but after the progress dialog complete or the thread was complete the circle progress dialog still appear. 当我单击登录按钮时,该方法正在运行,并且显示了圆环进度对话框,但是在进度对话框完成或线程完成后,圆环进度对话框仍会出现。

I want the circleprogressbar to disappear using RunOnUiThread(). 我希望使用RunOnUiThread()使circleprogressbar消失。 How can I to figure what is wrong with my code? 我该如何判断我的代码出了什么问题? Incidentally, I ran this in my fragment so I need to use Activity to call the RunOnUiThread function. 顺便说一句,我在片段中运行了它,因此我需要使用Activity来调用RunOnUiThread函数。

circleprogressbar.Visibility = ViewStates.Visible;
new Thread(new ThreadStart(delegate
    {

       while (progressValue < 100)
           {
               progressValue += 10;
               circleprogressbar.Progress = progressValue;
                Thread.Sleep(2000);
           }
  Activity.RunOnUiThread(() => { circleprogressbar.Visibility = ViewStates.Invisible; });
            })).Start();

I would suggest to create event to handle this situation, like this: 我建议创建事件来处理这种情况,如下所示:

public static class SomeClassWithEventDeclaration
{
    public event EventHandler<SomeClassType> Completed;
}

public class YourClassWithWork
{
    public void Work()
    {
        SomeClassWithEventDeclaration.Completed += (sender, data) =>
        { 
           CheckTheData(data);
           HideProgressBar();
        };
    }

    public void MethodWithDataProcessing()
    {
        try
        {
           //Your processing
           SomeClassWithEventDeclaration.Completed.Invoke(someData);
        }
        catch(Exception ex)
        {
           //SomeClassWithEventDeclaration.Completed.Invoke(someErrorData);
        }
    }
}

I wrote that code without intellisense, so some declarations or method callings might require some check. 我写的代码没有智能感知,因此某些声明或方法调用可能需要进行一些检查。 This solution is not great, but if you need it right now - that would help. 这个解决方案不是很好,但是如果您现在需要它-会有所帮助。 Also, I would suggest you to read about Xamarin Forms - Messaging Center . 另外,我建议您阅读有关Xamarin Forms-Messaging Center的信息 This tool would be better to use in your situation. 在您的情况下,最好使用此工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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