【问题标题】:How to display alert box with Xamarin.Forms for validation?如何使用 Xamarin.Forms 显示警报框以进行验证?
【发布时间】:2015-02-15 15:43:05
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: android ios iphone xamarin xamarin.forms


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,我发现它可以在异步环境中通过添加等待开始!!
    • 应该是例外的答案。谢谢!
    • @AhmadChishti,没有。您需要添加一个抽象层来实现这一点。
    【解决方案2】:

    你可以使用:

    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,如果它不为 null,则调用。

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

    我希望这会有所帮助:)。 万事如意!

    【讨论】:

    • 如何在 ViewModel 中注入这个服务?你也能帮忙吗?
    【解决方案3】:
    【解决方案4】:

    @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);
    

    希望以上代码对你有用。

    谢谢。

    【讨论】:

      【解决方案5】:

      使用 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 中(使用 Prism):

      >

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-16
        • 2013-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多