Xamarin Android 进度对话框在 Android 版本低于 lollipop 的 show 方法中崩溃

Xamarin Android progress dialog crashes in Android version below lollipop on show method

loginButton.click += delegate
{
 ProgressDialog progress = new ProgressDialog(Application.Context, 
 Resource.Style.CustomAlertDialogStyle);
 progress.SetMessage(" Please wait...");
 progress.SetIcon(Resource.Drawable.next);
 progress.SetCancelable(false);
 progress.SetProgressStyle(ProgressDialogStyle.Spinner);
 progress.SetInverseBackgroundForced(true);
 progress.Indeterminate = true;     
 progress.SetIndeterminateDrawable(GetDrawable(Resource.Drawable.Header));
 progress.Show();               
}

这是我的进度对话框代码,我想要的是当我登录时出现该对话框,因为我必须从中调用大量数据 服务端,它在更高的 android 版本中正常工作,但问题是当我在具有 Android 版本 KitKat 或以下的设备上使用它时 当编译器达到 progress.show() 时应用程序崩溃;而且我无法理解为什么我是 Xamarin Android.

的新手

我不知道为什么会出现此错误,但在 Click 事件上创建 ProgressDialog 是个坏主意

将所有代码移至 onCreate() 方法

ProgressDialog progress = new ProgressDialog(Application.Context, 
 Resource.Style.CustomAlertDialogStyle);
 progress.SetMessage(" Please wait...");
 progress.SetIcon(Resource.Drawable.next);
 progress.SetCancelable(false);
 progress.SetProgressStyle(ProgressDialogStyle.Spinner);
 progress.SetInverseBackgroundForced(true);
 progress.Indeterminate = true;     
 progress.SetIndeterminateDrawable(GetDrawable(Resource.Drawable.OxyHeader));

并且只在点击方法中显示进度

if(progress != null){
progress.Show();  
}

我通过执行以下操作找到了答案

    ProgressDialog dialog;
    loginButton.Click += async delegate
        {
            dialog = ProgressDialog.Show(this, string.Empty, string.Empty);
            dialog.SetContentView(Resource.Layout.Loader);
    .
    .
    .

           }

loader.xml

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/layout_root"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#105499"
      android:orientation="vertical"
    >
     <ProgressBar
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:layout_gravity="center_vertical|center_horizontal"
      android:background="@android:color/transparent"
      android:indeterminateDrawable="@drawable/loader_Anime"
      android:layout_marginBottom="20dp"/>
    </LinearLayout>

Loader_anime.xml

         <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
            <item
             android:drawable="@drawable/Oxy_top"
             android:duration="50"/>

            <item
              android:drawable="@drawable/Oxy_right"
              android:duration="50"/>


            <item
             android:drawable="@drawable/Oxy_bottom"
             android:duration="50"/>


           <item
             android:drawable="@drawable/Oxy_left" 
             android:duration="50"/>
            </animation-list>