If you already have a little experience in cross-platform development with Xamarin.Forms, you probably know the problem: Navigation between different pages works very well, but there is no easy solution to display dialog boxes overlaying an active page.
Today I want to show you how you can implement this feature in your Xamarin.Forms application. I have created an example project for this purpose, which you can download from my GitHub site.
I first create a new Cross-Platform project in Visual Studio. I start without a template (blank) and use .NET standard for the shared code base. After the project is initialized, I add the plugin for the dialogs to all projects. You can find it via Nuget. The project is also open source on GitHub. To add a Nuget package to your projects, mark the project (or solution) and select the entry Manage NuGet Packages. Select all projects of your solution to install the package.
The first thing to do is to initialize the package. This takes place in the platform-specific projects. For Android, open the MainActivity.cs file and add the following to your OnCreate() method:
The same goes for iOS: Add the following snippet to your FinishedLaunching()-Method inside the AppDelegate.cs file.
If you want to support UWP, the relevant code goes inside the App-class:
More detailed information about initializing the plugin can be found in the projects documentation.
Now add a new Content Page to the .NET standard project. This is generated with predefined code, which must be adjusted as follows:
This is the foundation of the dialog. I just removed the sample content and added the namespace for the plugin. Finally I changed the root element of the page from ContentView to PopUpPage. These changes are also required in the code-behind file. All you have to do here is change the inheritance from ContentView to PopUpPage.
Next, I design the content of the dialog window. I want to show the user a simple login dialog, so I need input fields for username and password, as well as a button to confirm the input.
Now there is missing only one possibility to display the dialog. I added a simple button to my main page and linked it to an event handler that opens the dialog. In real applications, you would do this by adding bindings to the ViewModel, but I decided to use the quick and dirty variant to keep the example as simple as possible.
Calling the dialog probably reminds you syntactically of the normal navigation between the pages. Closing the dialog works analogously, I wired this part with the EventHandler in the code-behind file of the dialog.
If you now start the application on a device or emulator of your choice, everything should already work. Besides the pure dialog functionality, the plugin offers the possibility to animate the dialogs. The animation is defined in the XAML code of the dialog. You can also find complete documentation for this on the projects website. Here it’s a good idea to just play around with the available possibilities, but finally you’ll find a small example:
Subscribe via RSS