Xamarin Alert | Pop-ups | 弹窗相关

2024-05-28 11:58
文章标签 相关 弹窗 xamarin ups alert pop

本文主要是介绍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:

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

这篇关于Xamarin Alert | Pop-ups | 弹窗相关的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1010501

相关文章

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

JavaScript Array.from及其相关用法详解(示例演示)

《JavaScriptArray.from及其相关用法详解(示例演示)》Array.from方法是ES6引入的一个静态方法,用于从类数组对象或可迭代对象创建一个新的数组实例,本文将详细介绍Array... 目录一、Array.from 方法概述1. 方法介绍2. 示例演示二、结合实际场景的使用1. 初始化二

Redis的Zset类型及相关命令详细讲解

《Redis的Zset类型及相关命令详细讲解》:本文主要介绍Redis的Zset类型及相关命令的相关资料,有序集合Zset是一种Redis数据结构,它类似于集合Set,但每个元素都有一个关联的分数... 目录Zset简介ZADDZCARDZCOUNTZRANGEZREVRANGEZRANGEBYSCOREZ

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

关于Maven生命周期相关命令演示

《关于Maven生命周期相关命令演示》Maven的生命周期分为Clean、Default和Site三个主要阶段,每个阶段包含多个关键步骤,如清理、编译、测试、打包等,通过执行相应的Maven命令,可以... 目录1. Maven 生命周期概述1.1 Clean Lifecycle1.2 Default Li

numpy求解线性代数相关问题

《numpy求解线性代数相关问题》本文主要介绍了numpy求解线性代数相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 在numpy中有numpy.array类型和numpy.mat类型,前者是数组类型,后者是矩阵类型。数组

Redis的Hash类型及相关命令小结

《Redis的Hash类型及相关命令小结》edisHash是一种数据结构,用于存储字段和值的映射关系,本文就来介绍一下Redis的Hash类型及相关命令小结,具有一定的参考价值,感兴趣的可以了解一下... 目录HSETHGETHEXISTSHDELHKEYSHVALSHGETALLHMGETHLENHSET

Ubuntu 24.04 LTS怎么关闭 Ubuntu Pro 更新提示弹窗?

《Ubuntu24.04LTS怎么关闭UbuntuPro更新提示弹窗?》Ubuntu每次开机都会弹窗提示安全更新,设置里最多只能取消自动下载,自动更新,但无法做到直接让自动更新的弹窗不出现,... 如果你正在使用 Ubuntu 24.04 LTS,可能会注意到——在使用「软件更新器」或运行 APT 命令时,

python中的与时间相关的模块应用场景分析

《python中的与时间相关的模块应用场景分析》本文介绍了Python中与时间相关的几个重要模块:`time`、`datetime`、`calendar`、`timeit`、`pytz`和`dateu... 目录1. time 模块2. datetime 模块3. calendar 模块4. timeit