ほとんどの場合、2つのメソッド、ShowLoading()とHideLoading()が必要な場合は、これを使用してください。
public static void ShowLoading()
{
HideLoading();
myLoadingThread = new Thread(new ThreadStart(LoadingThread));
myLoadingThread.Start();
}
private static void LoadingThread()
{
Looper.Prepare();
myProgressDialog = new ProgressDialog(myActivity,
Resource.Style.AppTheme_Dialog);
myProgressDialog.SetMessage("Loading..."); // Or a @string...
myProgressDialog.SetIcon(Resource.Drawable.your_loading_icon);
myProgressDialog.SetProgressStyle(ProgressDialogStyle.Spinner);
myProgressDialog.SetCancelable(false);
myProgressDialog.Show();
Looper.Loop();
}
public static void HideLoading()
{
if (myProgressDialog != null)
{
myProgressDialog.Dismiss();
myProgressDialog = null;
}
if (myLoadingThread != null)
myLoadingThread.Abort();
}
ここで、コードサンプルで使用した次の変数を宣言して説明します。そのうちの1つはグローバルです。グローバル変数を使用したくない場合、または一度に2つの読み込みダイアログが必要な場合(wtf .. .. )別の解決策を探します。これは最も単純な方法であり、最もフレンドリーで、ネストされたメソッド、新しいクラス、およびそのような単純なもののためのあらゆる場所での継承を備えた奇妙なコードがありません。
private Thread myLoadingThread;
private ProgressDialog myProgressDialog;
// Some people will hate me for this, but just remember
// to call myActivity = this; on each OnStart() of your app
// and end with all your headaches
public Activity myActivity;