void DismissCurrent(Context context = null) { lock (dialogLock) { if (CurrentDialog != null) { waitDismiss.Set(); Action actionDismiss = () => { CurrentDialog.Hide(); CurrentDialog.Dismiss(); statusText = null; statusObj = null; imageView = null; progressWheel = null; CurrentDialog = null; waitDismiss.Reset(); }; //First try the SynchronizationContext if (Application.SynchronizationContext != null) { Application.SynchronizationContext.Send(state => actionDismiss(), null); return; } //Next let's try and get the Activity from the CurrentDialog if (CurrentDialog != null && CurrentDialog.Window != null && CurrentDialog.Window.Context != null) { var activity = CurrentDialog.Window.Context as Activity; if (activity != null) { activity.RunOnUiThread(actionDismiss); return; } } //Finally if all else fails, let's see if someone passed in a context to dismiss and it // happens to also be an Activity if (context != null) { var activity = context as Activity; if (activity != null) { activity.RunOnUiThread(actionDismiss); return; } } } } }
void showProgress(Context context, int progress, string status = null, MaskType maskType = MaskType.Black, TimeSpan?timeout = null, Action clickCallback = null, Action cancelCallback = null) { if (!timeout.HasValue || timeout == null) { timeout = TimeSpan.Zero; } if (CurrentDialog != null && progressWheel == null) { DismissCurrent(context); } lock (dialogLock) { if (CurrentDialog == null) { SetupDialog(context, maskType, cancelCallback, (a, d, m) => { var inflater = LayoutInflater.FromContext(context); var view = inflater.Inflate(Resource.Layout.loadingprogress, null); if (clickCallback != null) { view.Click += (sender, e) => clickCallback(); } progressWheel = view.FindViewById <ProgressWheel>(Resource.Id.loadingProgressWheel); statusText = view.FindViewById <TextView>(Resource.Id.textViewStatus); if (maskType != MaskType.Black) { view.SetBackgroundResource(Resource.Drawable.roundedbgdark); } progressWheel.SetProgress(0); if (statusText != null) { statusText.Text = status ?? ""; statusText.Visibility = string.IsNullOrEmpty(status) ? ViewStates.Gone : ViewStates.Visible; } return(view); }); if (timeout.Value > TimeSpan.Zero) { Task.Factory.StartNew(() => { if (!waitDismiss.WaitOne(timeout.Value)) { DismissCurrent(context); } }).ContinueWith(ct => { var ex = ct.Exception; if (ex != null) { Android.Util.Log.Error("AndHUD", ex.ToString()); } }, TaskContinuationOptions.OnlyOnFaulted); } } else { Application.SynchronizationContext.Send(state => { progressWheel.SetProgress(progress); statusText.Text = status ?? ""; }, null); } } }