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

相关文章

sqlite3 相关知识

WAL 模式 VS 回滚模式 特性WAL 模式回滚模式(Rollback Journal)定义使用写前日志来记录变更。使用回滚日志来记录事务的所有修改。特点更高的并发性和性能;支持多读者和单写者。支持安全的事务回滚,但并发性较低。性能写入性能更好,尤其是读多写少的场景。写操作会造成较大的性能开销,尤其是在事务开始时。写入流程数据首先写入 WAL 文件,然后才从 WAL 刷新到主数据库。数据在开始

两个月冲刺软考——访问位与修改位的题型(淘汰哪一页);内聚的类型;关于码制的知识点;地址映射的相关内容

1.访问位与修改位的题型(淘汰哪一页) 访问位:为1时表示在内存期间被访问过,为0时表示未被访问;修改位:为1时表示该页面自从被装入内存后被修改过,为0时表示未修改过。 置换页面时,最先置换访问位和修改位为00的,其次是01(没被访问但被修改过)的,之后是10(被访问了但没被修改过),最后是11。 2.内聚的类型 功能内聚:完成一个单一功能,各个部分协同工作,缺一不可。 顺序内聚:

log4j2相关配置说明以及${sys:catalina.home}应用

${sys:catalina.home} 等价于 System.getProperty("catalina.home") 就是Tomcat的根目录:  C:\apache-tomcat-7.0.77 <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" /> 2017-08-10

Node Linux相关安装

下载经编译好的文件cd /optwget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-x64.tar.gztar -xvf node-v10.15.3-linux-x64.tar.gzln -s /opt/node-v10.15.3-linux-x64/bin/npm /usr/local/bin/ln -s /opt/nod

git ssh key相关

step1、进入.ssh文件夹   (windows下 下载git客户端)   cd ~/.ssh(windows mkdir ~/.ssh) step2、配置name和email git config --global user.name "你的名称"git config --global user.email "你的邮箱" step3、生成key ssh-keygen

zookeeper相关面试题

zk的数据同步原理?zk的集群会出现脑裂的问题吗?zk的watch机制实现原理?zk是如何保证一致性的?zk的快速选举leader原理?zk的典型应用场景zk中一个客户端修改了数据之后,其他客户端能够马上获取到最新的数据吗?zk对事物的支持? 1. zk的数据同步原理? zk的数据同步过程中,通过以下三个参数来选择对应的数据同步方式 peerLastZxid:Learner服务器(Follo

rtmp流媒体编程相关整理2013(crtmpserver,rtmpdump,x264,faac)

转自:http://blog.163.com/zhujiatc@126/blog/static/1834638201392335213119/ 相关资料在线版(不定时更新,其实也不会很多,也许一两个月也不会改) http://www.zhujiatc.esy.es/crtmpserver/index.htm 去年在这进行rtmp相关整理,其实内容早有了,只是整理一下看着方

枚举相关知识点

1.是用户定义的数据类型,为一组相关的常量赋予有意义的名字。 2.enum常量本身带有类型信息,即Weekday.SUN类型是Weekday,编译器会自动检查出类型错误,在编译期间可检查错误。 3.enum定义的枚举类有什么特点。         a.定义的enum类型总是继承自java.lang.Enum,且不能被继承,因为enum被编译器编译为final修饰的类。         b.只能定义

java计算机毕设课设—停车管理信息系统(附源码、文章、相关截图、部署视频)

这是什么系统? 资源获取方式在最下方 java计算机毕设课设—停车管理信息系统(附源码、文章、相关截图、部署视频) 停车管理信息系统是为了提升停车场的运营效率和管理水平而设计的综合性平台。系统涵盖用户信息管理、车位管理、收费管理、违规车辆处理等多个功能模块,旨在实现对停车场资源的高效配置和实时监控。此外,系统还提供了资讯管理和统计查询功能,帮助管理者及时发布信息并进行数据分析,为停车场的科学

鸿蒙开发中实现自定义弹窗 (CustomDialog)

效果图 #思路 创建带有 @CustomDialog 修饰的组件 ,并且在组件内部定义controller: CustomDialogController 实例化CustomDialogController,加载组件,open()-> 打开对话框 , close() -> 关闭对话框 #定义弹窗 (CustomDialog)是什么? CustomDialog是自定义弹窗,可用于广告、中