Search code examples
xamarincode-behindmessageboxshared

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


Solution

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