Xamarin Android C# 中的进度条和线程
Progress bar and thread in Xamarin Android C#
我尝试使用此方法加载我的 SQL。当我点击按钮登录时,该方法正在运行,它显示了圈子进度对话框,但是在进度对话框完成或线程完成后,圈子进度对话框仍然出现。
我希望使用 RunOnUiThread() 使 circleprogressbar 消失。我怎样才能弄清楚我的代码有什么问题?顺便说一下,我在我的片段中 运行 这个所以我需要使用 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();
我建议创建 event 来处理这种情况,如下所示:
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);
}
}
}
我在没有智能感知的情况下编写了该代码,因此某些声明或方法调用可能需要进行一些检查。这个解决方案不是很好,但如果你现在需要它 - 那会有所帮助。
另外,我建议您阅读有关 Xamarin Forms - Messaging Center 的内容。
这个工具更适合你的情况。
我尝试使用此方法加载我的 SQL。当我点击按钮登录时,该方法正在运行,它显示了圈子进度对话框,但是在进度对话框完成或线程完成后,圈子进度对话框仍然出现。
我希望使用 RunOnUiThread() 使 circleprogressbar 消失。我怎样才能弄清楚我的代码有什么问题?顺便说一下,我在我的片段中 运行 这个所以我需要使用 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();
我建议创建 event 来处理这种情况,如下所示:
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);
}
}
}
我在没有智能感知的情况下编写了该代码,因此某些声明或方法调用可能需要进行一些检查。这个解决方案不是很好,但如果你现在需要它 - 那会有所帮助。 另外,我建议您阅读有关 Xamarin Forms - Messaging Center 的内容。 这个工具更适合你的情况。