简体   繁体   中英

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

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

I know that we can display alert using below code from ContentView code behind but I want to display alertbox from my ViewModel.

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

I've registered my ViewModel against View with below code.

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

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

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

You can use:

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

But, this is a bad practice to use it inside the view model.

Instead, the best practice, in my opinion, would be decoupling this from the view models and pages. The solution is to create a service responsible for displaying the alerts in your application. Below you can find the simple answer to your problem. However, DisplayAlert method has many overloads, and you can add new methods to your service that will use the overloads.

Two Simple examples in one code:

First implement interface for your service:

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

Then, implement the interface in the concrete implementation:

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();
    }
}

The first method allows you to display alert and the second allows you to provide a call back method that will be called after user dismiss the alert box - for example navigates back to the previous page.

The second method can be used in both cases, just call:

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

The CallBackAferHide?.Invoke(); first checks if Action was provided, then invokes if it is not null.

In your view model, inject the service and just call its method providing parameters.

I hope that will help :). All the best!

@Nirav-Mehta

Please try this code to display an alert in ViewModel:

Normal Alert Box:

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

Ask a simple question alert box:

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

Display simple alert 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);

I hope the above code will be useful for you.

Thank You.

Using Rg.Plugins.Popup Nuget

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

Define the popup like this:

>

<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"...

And in ViewModel (Using Prism):

>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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