简体   繁体   中英

how to display message dailogue in PCL project class file

I want to try display message dialogue in .cs file that is in pcl project.I want to call its method from my view.xaml.cs for displaying alert.

i tried to use

   var dialog = new MessageDialog(e.Message, e.GetType().ToString());
        dialog.Commands.Add(new UICommand("Ok", null));

        await dialog.ShowDialogSafely();

but that require windows.UI.Popups namespace that is not available in class file of pcl project.I think its because it shared by all platform and that namespace is for windows only.then how can I achieve this.please help. thanks in advance

You can use the User dialogs Plugin to achieve this. It provides cross platform dialogs. https://github.com/aritchie/userdialogs . And then it's just a one liner:

await UserDialogs.Instance.AlertAsync(e.Message, e.GetType().ToString());

You can install it via NuGet: https://www.nuget.org/packages/Acr.UserDialogs/ . Please read the github readme on how to set it up correctly.

AcrUserDialogs库允许在PCL https://www.nuget.org/packages/Acr.UserDialogs/中使用对话框

Replacement for Acr.UserDialogs which worked for me is:

 var result = await Application.Current.MainPage.DisplayAlert(
        title,
        message,
        buttonConfirmText,
        buttonCancelText);

Good side is because you don't need Plugin reference in the project either the 'UserDialogs.Init(this);' in your MainActivity class. One possibility is also with Prism but you need to install it: https://prismlibrary.readthedocs.io/en/latest/Xamarin-Forms/4-Page-Dialog-Service/

Source: https://forums.xamarin.com/discussion/48009/pop-display-alert-message-from-viewmodel

This is achievable without third-party plug-ins. Xamarin.Forms has it implemented: https://developer.xamarin.com/guides/xamarin-forms/user-interface/navigation/pop-ups/#Displaying_an_Alert

A simple informative MessageBox as follows:

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

A window with more than 1 option like so:

var answer = await DisplayAlert ("Question?", "Would you like to play a game", "Yes", "No");

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