Xamarin Alert | Pop-ups | 弹窗相关

Xamarin 相关官方文档

简单用法:

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:

enter image description here

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:

enter image description here

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值