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生命周期相关命令演示

《关于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

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