繁体   English   中英

如何使用 Xamarin.Forms 显示警报框以进行验证?

[英]How to display alert box with Xamarin.Forms for validation?

如何使用 Xamarin.Forms 显示警报框以进行验证?

我知道我们可以使用下面来自 ContentView 代码的代码显示警报,但我想从我的 ViewModel显示警报框。

DisplayAlert ("Alert", "You have been alerted", "OK");

我已经使用以下代码针对 View注册了我的ViewModel

ViewFactory.Register<[ContentPage], [ContentPageViewModel]> (); 

您可以通过静态App.CurrentMainPage属性从Xamarin.Forms项目中的任何位置显示警报,例如

await App.Current.MainPage.DisplayAlert("Test Title", "Test", "OK");

您可以使用:

Application.Current.MainPage.DisplayAlert(title, message, buttonText)

但是,在视图模型中使用它是一种不好的做法。

相反,在我看来,最佳实践是将其与视图模型和页面分离。 解决方案是创建一个服务,负责在您的应用程序中显示警报。 您可以在下面找到问题的简单答案。 但是,DisplayAlert 方法有许多重载,您可以向将使用重载的服务添加新方法。

一个代码中的两个简单示例:

首先为您的服务实现接口:

public interface IDialogService
{
    public Task ShowErrorAsync(string message, string title, string buttonText);
    public Task ShowErrorAsync(string message, string title, string buttonText, Action CallBackAferHide);
}

然后,在具体实现中实现接口:

public class DialogService : IDialogService
{
    public async Task ShowErrorAsync(string message, string title, string buttonText)
    {
        await Application.Current.MainPage.DisplayAlert(title, message, buttonText);
    }

    public async Task ShowErrorAsync(string message, string title, string buttonText, Action CallBackAferHide)
    {
        await Application.Current.MainPage.DisplayAlert(title, message, buttonText);
        CallBackAferHide?.Invoke();
    }
}

第一种方法允许您显示警报,第二种方法允许您提供回调方法,该方法将在用户关闭警报框后调用 - 例如导航回上一页。

两种情况都可以使用第二种方法,只需调用:

await ShowErrorAsync("message", "title", "OK", null);

CallBackAferHide?.Invoke(); 首先检查是否提供了 Action,如果它不为空则调用。

在您的视图模型中,注入服务并调用其提供参数的方法。

我希望这会有所帮助:)。 祝一切顺利!

@Nirav-Mehta

请尝试使用此代码在 ViewModel 中显示警报:

正常警报框:

App.Current.MainPage.DisplayAlert("Alert","You can write message here!!!","Ok");
OR
Application.Current.MainPage.DisplayAlert("Alert","You can write message here!!!","Ok");

问一个简单的问题警报框:

var answer = App.Current.MainPage.DisplayAlert("Question?", "Your Question Write Here !!!", "Yes", "No"));
OR
var answer = Application.Current.MainPage.DisplayAlert("Question?", "Your Question Write Here !!!", "Yes", "No"));
Debug.WriteLine("Answer: " + answer);

显示简单的警报 ActionSheet :

var action = App.Current.MainPage.DisplayActionSheet("ActionSheet: Send to?", "Cancel", null, "Email", "Twitter", "Facebook");
OR
var action = Application.Current.MainPage.DisplayActionSheet("ActionSheet: Send to?", "Cancel", null, "Email", "Twitter", "Facebook");
Debug.WriteLine("Action: " + action);

我希望上面的代码对你有用。

谢谢你。

使用 Rg.Plugins.Popup Nuget

( https://www.nuget.org/packages/Rg.Plugins.Popup/ )

像这样定义弹出窗口:

>

<popup:PopupPage xmlns="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:popup="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"...

在 ViewModel(使用棱镜)中:

>

await NavigationService.NavigateAsync("NavigationPage/[View]");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM