Xamarin.Forms DisplayPromptAsync issues in 4.4 & 4.5 for UWP
I'm in the middle of building a cross platform Xamarin.Forms app for an unattended device that needs to utilize DisplayPromptAsync to capture input from the user.
We're deploying the app to Android and Windows 10 via UWP.
Testing the application on Android worked fine however I encountered a strange issue when testing on Windows. Basically, when we should be showing a prompt via the DisplayPromptAsync method built into Xamarin.Forms, the input box never appears and the app seems to just freeze in place.
What DisplayPromptAsync Does
DisplayPrompt Async displays an input box for the user to enter some details. The user can either click the cancel button, or type in a value and then click "OK".
You'll see a screenshot from the Android Emulator below. You'll notice that there is a prompt asking for user input and the numerical keyboard that is automatically displayed on Android when we open a prompt.
My code looks like this:
var result = await DisplayPromptAsync("Employee ID", "Enter your employee ID", "OK", "Cancel", null, 8, keyboard: Keyboard.Numeric, "");
My code is using the following method.
DisplayPromptAsync(string title, string message, string accept = "OK", string cancel = "Cancel", string placeholder = null, int maxLength = -1, Keyboard keyboard = null, string initialValue = "");
It's important to note that we can specify the keyboard type. I only want my users to enter a number, therefore I specify Keyboard.Numeric. Other options include Keyboard.Text, Keyboard.Email & Keyboard.Telephone.
This code works fine on Android & iOS, but the prompt just never shows when you try to run the app on a Windows 10 using UWP.
After doing some research, I came across this Github issue where another developer reported the same problem in the Xamarin.Forms repository.
It seems that there was a bug introduced into an earlier version of Xamarin.Forms that broke DisplayPromptAsync on UWP Windows.
The fix is to update the Xamarin.Forms nuget package to version 4.6 or higher.
These are the steps I took in Visual Studio 2019 to fix the issue:
- Right click the solution and select "Manage Nuget Packages for Solution".
- Click on the "Installed" tab.
- Click on Xamarin.Forms.
- Tick all required projects, select the latest stable version from the dropdown and click Update.
That should be it. When you run through the DisplayPromptAsync code on Windows 10 UWP you should now see this prompt.