本文主要是介绍Xamarin Alert | Pop-ups | 弹窗相关,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Xamarin 相关官方文档
- Displaying Pop-ups
简单用法:
DisplayAlert ("Alert", "You have been alerted", "OK");
又返回结果的 Alert
:
var answer = await DisplayAlert("Exit", "Do you wan't to exit the App?", "Yes", "No");
if (answer)
{// User choose Yes
}
else
{// User choose No
}
进阶版 Alert
用 DisplayAlert
显示下载的状态,随着下载状态自行修改 DisplayAlert
text
Here is a simple “Dynamic Alert” for Forms and iOS
using UIAlertController
and Android
using a DialogFragment
and a Xamarin.Forms
dependency service:
Dependency Interface:
public interface IDynamicAlert
{void Show(string title, string message);void Update(string message);void Dismiss();
}
iOS IDynamicAlert
Dependency Implementation:
public class DynamicAlert : IDynamicAlert
{UIAlertController alert;public void Show(string title, string message){if (alert != null) throw new Exception("DynamicAlert already showing");alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);var rootVC = UIApplication.SharedApplication.Windows[0].RootViewController;rootVC.PresentViewController(alert, true, () =>{});}public void Update(string message){if (alert == null) throw new Exception("DynamicAlert is not showing, call Show first");alert.Message = message;}public void Dismiss(){if (alert == null) throw new Exception("DynamicAlert is not showing, call Show first");alert.DismissViewController(true, () =>{alert.Dispose();alert = null;});}
}
Example Usage:
var alert = DependencyService.Get<IDynamicAlert>();
if (alert != null)
{alert.Show("StackOverflow", "Starting your request...");await Task.Delay(2000); // Do some work...alert.Update("Your request is processing...");await Task.Delay(2000); // Do some work...alert.Update("Your request is complete...");await Task.Delay(750);alert.Dismiss();
}
else
{throw new Exception("IDynamicAlert Dependency not found");
}
Output:
Android Version:
The android version consists of a couple of parts, a DialogFragment
subclass and the IDynamicAlert
implementation that uses the custom DialogFragment
.
Android DialogFragment Subclass:
public class DynamicAlertDialogFragment : DialogFragment
{AlertDialog alertDialog;readonly Context context;public static DynamicAlertDialogFragment Instance(Context context, string title, string message){var fragment = new DynamicAlertDialogFragment(context);Bundle bundle = new Bundle();bundle.PutString("title", title);bundle.PutString("message", message);fragment.Arguments = bundle;return fragment;}public DynamicAlertDialogFragment(Context context){this.context = context;}public override Dialog OnCreateDialog(Bundle savedInstanceState){var title = Arguments.GetString("title");var message = Arguments.GetString("message");alertDialog = new AlertDialog.Builder(context).SetIcon(Android.Resource.Drawable.IcDialogInfo).SetTitle(title).SetMessage(message).Create();return alertDialog;}public void SetMessage(string message){(context as Activity).RunOnUiThread(() => { alertDialog.SetMessage(message);});}
}
Android IDynamicAlert
Dependency Implementation:
public class DynamicAlert : IDynamicAlert
{const string FRAGMENT_TAG = "DynamicAlert_Fragment";DynamicAlertDialogFragment fragment;static FormsAppCompatActivity currentActivity;public static FormsAppCompatActivity CurrentActivity { set { currentActivity = value; } }public void Show(string title, string message){if (currentActivity == null) throw new Exception("DynamicAlert.CurrentActivity needs assigned");var fragMgr = currentActivity.FragmentManager;var fragTransaction = fragMgr.BeginTransaction();var previous = fragMgr.FindFragmentByTag(FRAGMENT_TAG);if (previous != null){fragTransaction.Remove(previous);}fragTransaction.DisallowAddToBackStack();fragment = DynamicAlertDialogFragment.Instance(currentActivity, title, message);fragment.Show(fragMgr, FRAGMENT_TAG);}public void Update(string message){if (fragment == null) throw new Exception("DynamicAlert is not showing, call Show first");fragment.SetMessage(message);}public void Dismiss(){if (fragment == null) throw new Exception("DynamicAlert is not showing, call Show first");fragment.Dismiss();fragment.Dispose();fragment = null;}
}
Android Init / Usage:
When creating the AlertDialog
in the DialogFragment
we need access to the current Activity
and when using Xamarin.Forms
, that is normally the MainActivity
that is a FormsAppCompatActivity
subclass. Thus you will need to initialize the DynamicAlert.CurrentActivity
static property with this Activity
in your MainActivity.OnCreate
subclass:
Example:
protected override void OnCreate(Bundle bundle)
{TabLayoutResource = Resource.Layout.Tabbar;ToolbarResource = Resource.Layout.Toolbar;base.OnCreate(bundle);DynamicAlert.CurrentActivity = this;global::Xamarin.Forms.Forms.Init(this, bundle);LoadApplication(new App());
}
Android Output:
这篇关于Xamarin Alert | Pop-ups | 弹窗相关的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!