Xamarin 中的警报






4.80/5 (9投票s)
Xamarim.forms 中的移动警报
引言
在本文中,我们将介绍 Xamarim.forms
中的移动警报。对于任何移动应用程序,与用户、客户或产品负责人(如果我们使用敏捷环境(Scrum))一起定义是很重要的。对于 Android 和 iOS,即使我们在一个共享项目中工作,我们也需要知道每个环境在原生应用程序中可以提供什么。
背景
对 Xamarin.Forms
和 Visual Studio 2017 有一些了解。
Toast(提示)
这是一个简单的插件:Toasts.Forms.Plugin
,我们可以在 Xamarin.forms
项目中使用它。我建议从它开始,因为它类似于原生提示或通知 API,特别是对于 iOS 开发人员。
通知放置在中心内联,以靠近原生平台,并在设计上进行一些更改,它允许开发人员执行声音和徽章。
原生环境中的通知
iOS 使用 UNNotificationRequest
对象来管理通知。
Android 使用
- Snackbar(小吃栏),它显示一条简单的消息,并在一段时间后消失。 (https://developer.android.com.cn/reference/android/support/design/widget/Snackbar.html)
Notification.Builder
如果我们至少使用 Lollipop 版本,您可以在其中设置标题、图标(大图标或智能图标)。- https://developer.android.com.cn/reference/android/app/Notification.Builder.html)
- UWP/WinRT 使用
ToastNotification
:它包括文本或图像。
https://docs.microsoft.com/en-us/uwp/api/windows.ui.notifications.toastnotification)
如何使用插件
我们在 Visual Studio 2017 中的 NuGet 包管理器控制台中从每个平台添加此插件,即使是您的便携式库,因为它使用依赖项服务。
安装-包 Toasts.Forms.Plugin -Version 3.3.2
或者我们只需在 Nuget 包管理器中搜索 Toasts.Forms.Plugin
,然后单击安装
正如项目 GitHub 描述中所提到的,我们需要在每个平台(Android 或 iOS 或 UWP/WinRT)中注册依赖项。
因此,对于每个项目,我们将在 MainActivity.cs 或 MainPage.cs 中添加这些引用。
using Xamarin.Forms;
using Plugin.Toasts;
对于 Android,此代码将添加到 MainActivity.OnCreate
的末尾,以注册依赖项并对其进行初始化
DependencyService.Register<ToastNotification>();
ToastNotification.Init(this);
我们对其他平台也这样做。
对于 iOS,我们必须添加请求权限以显示通知
// Request Permissions
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
// Request Permissions
UNUserNotificationCenter.Current.RequestAuthorization
(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge |
UNAuthorizationOptions.Sound, (granted, error) =>
{
// Deal with the problem
});
}
else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null);
app.RegisterUserNotificationSettings(notificationSettings);
}
现在,我们可以在应用程序中的任何地方调用通知,例如,让我们回到 Portable 项目中的主页,以开始一个简单的示例。
我们首先定义通知选项,例如标题、描述,这是接口 INotificationOptions
public interface INotificationOptions
{
string Title { get; }
string Description { get; }
bool IsClickable { get; }
// Platform Specific Options
IWindowsOptions WindowsOptions { get; }
IAndroidOptions AndroidOptions { get; }
IiOSOptions iOSOptions { get; }
}
让我们开始
var options = new NotificationOptions()
{
Title = "Display notifications",
Description = "Some Description related to this title….",
IsClickable = false // Set to true if you need the result Clicked to come back
// (if the user clicks it)
};
// Use dependency service in order to resolve IToastNotificator.
var notification = DependencyService.Get<IToastNotificator>();
var result = await notification.Notify(options);
结果将返回一个 NotificationResult
,其中包含一个 Action
,具有以下值之一
[Flags]
public enum NotificationAction
{
Timeout = 1, // Hides by itself
Clicked = 2, // User clicked on notification
Dismissed = 4, // User manually dismissed notification
ApplicationHidden = 8, // Application went to background
Failed = 16 // When failed to display the toast
}
或者我们以这种方式调用设备
void ToastMessage(String title, String description)
{
Device.BeginInvokeOnMainThread(async () =>
{
var notifier = DependencyService.Get<IToastNotificator>();
var options = new NotificationOptions()
{
Title = title,
Description = description
};
var result = await notifier.Notify(options);
});
}
DisplayAlert (显示警报)
这是一个弹出窗口;它与通知不同,因为我们在警报中有一个按钮(确定或是/否)。
这是一个例子
DisplayAlert ("Alert!", "This is my first Alert", "OK");
或者
var answer = await DisplayAlert ("First question", "Do you know notifications in Xamarin", "Yes", "No");
Debug.WriteLine ("Answer: " + answer);
您可以在此 链接 中找到一个完整的示例。
UserDialogs (用户对话框)
我使用这个插件:Acr.UserDialogs
,由 Allan Ritchie 提供。
链接:https://github.com/aritchie/userdialogs
这是一种使用弹出链接的新方法,与 Toast 和 Alert 相比,它具有不同的设计。 正如在 GitHub 文档中提到的,它允许开发人员“从共享/便携式库调用标准用户对话框、操作表、警报、确认、加载、登录、进度、提示、Toast”。
在这个视频中,我解释了我们如何集成这个插件:Acr.UserDialogs
,在 Youtube 上。
简单的方式,如同原生
在本文中,作者解释了我们如何使用此组件以原生模式在 Android 环境中显示一个简单的 Toast:
- https://material.io/guidelines/components/snackbars-toasts.html
- https://medium.com/@frankiefoo/how-to-display-androids-toast-snackbar-in-xamarin-forms-pcl-project-7ec31b1639b7
GitHub 地址是:https://github.com/xyfoo/XamarinForms-AndroidPopUp