Xamarin.Android: ProgressDialog 没有显示
Xamarin.Android: ProgressDialog does not show up
我无法显示 ProgressDialog:
以下代码片段在示例中显示了我的代码:
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
progressDialog = new ProgressDialog(Context);
progressDialog.SetCancelable(false);
progressDialog.SetMessage("Saving ...");
}
private void Method()
{
progressDialog.Show();
var testDialog = progressDialog.IsShowing;
SaveImage();
}
private void SaveImage()
{
// does lot
...
// especially this method takes some time
mutableBitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
}
我创建了一个 ProgressDialog,我想在调用 SaveImage 方法之前显示它(并在 SaveImage 完成时取消它)。
我的问题是在 SaveImage 方法完成之前不会显示 ProgressDialog。
在调试模式下,progressDialog.IsShowing 是 true,在 SaveImage[=30= 之前]-调用了方法,但是没有显示出来
您必须将与 IO 相关的代码转移到后台线程,为此您可以使用异步任务
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
private String resp;
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
saveImage();
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
progressDialog.dismiss();
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(getContext(),
"ProgressDialog",
"Loading");
}
}
我找到了适合我的解决方案:
private async void Method()
{
progressDialog.Show();
var thread = new System.Threading.Thread(new ThreadStart(delegate
{
SaveImage();
}));
thread.Start();
while(thread.ThreadState == ThreadState.Running)
{
await Task.Delay(100);
}
progressDialog.Dismiss();
}
与此同时,我将此代码外包到 public static class ThreadHelper
,它只需要一个 Action(在本例中为 SaveImage())和一个 ProgressDialog 作为参数。
我无法显示 ProgressDialog:
以下代码片段在示例中显示了我的代码:
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
progressDialog = new ProgressDialog(Context);
progressDialog.SetCancelable(false);
progressDialog.SetMessage("Saving ...");
}
private void Method()
{
progressDialog.Show();
var testDialog = progressDialog.IsShowing;
SaveImage();
}
private void SaveImage()
{
// does lot
...
// especially this method takes some time
mutableBitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
}
我创建了一个 ProgressDialog,我想在调用 SaveImage 方法之前显示它(并在 SaveImage 完成时取消它)。
我的问题是在 SaveImage 方法完成之前不会显示 ProgressDialog。
在调试模式下,progressDialog.IsShowing 是 true,在 SaveImage[=30= 之前]-调用了方法,但是没有显示出来
您必须将与 IO 相关的代码转移到后台线程,为此您可以使用异步任务
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
private String resp;
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
saveImage();
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
progressDialog.dismiss();
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(getContext(),
"ProgressDialog",
"Loading");
}
}
我找到了适合我的解决方案:
private async void Method()
{
progressDialog.Show();
var thread = new System.Threading.Thread(new ThreadStart(delegate
{
SaveImage();
}));
thread.Start();
while(thread.ThreadState == ThreadState.Running)
{
await Task.Delay(100);
}
progressDialog.Dismiss();
}
与此同时,我将此代码外包到 public static class ThreadHelper
,它只需要一个 Action(在本例中为 SaveImage())和一个 ProgressDialog 作为参数。