So a lot of folks asked me regarding my previous post, An Await-able Transparent, Custom, Popup Input Dialog in Xamarin.Forms! 😉 how they could implement this awesomeness in a MVVM-friendly manner in Xamarin.Forms, instead of having to deal with dirty code-behind ugliness.
Frankly I did the above original implementation in a MVVM-manner, given myself being a MVVM-practitioner, but since there are a lot of newbie Xamarin devs out there, I thought it would help if I push it out there in the simplest manner for the newbies to understand without a hassle.
Anyhow in order to do some justice for the Xamarin-Forms-MVVM practitioners out there, including myself 😉 here I am pushing out how to implement the same awesomeness in Xamarin-Forms MVVM environment in a beautiful code manner! 😀 ❤
When it comes to dealing with MVVM environments, Services are crucial, which is what we’re going to leverage our previous implementation to.
So we could call up this service from anywhere across our shared code.
We shall be using Prism as the MVVM framework for this demo Xamarin.Forms project because its awesome! 😉
And create a Service implementation for our XFCustomInputAlertDialog, which we’ll be register with the Prism’s default IoC container.
Rest is pure magic! lol 😉
PS: I shall not be spoon feeding from this post about MVVM or how to set up MVVM framework in Xamarin.Forms, I shall assume you have solid knowledge on MVVM based Xamarin.Forms implementations. 🙂
If you want to be ahead of all, you can grab the Github code from here: https://github.com/UdaraAlwis/XFInputAlertDialogService
Yep this time I’ve created a separate repo for the project. Alright then let’s get started!
Let’s do it…
Taking a step by step approach…
1. Prism MVVM Setup
Add Prism for Xamarin.Forms to your project and do the primarily set up for Prism MVVM framework. (if you’re unaware of how to do this, there’s plenty of tutorials and blog posts online about setting up Prism for Xamarin.Forms)
This also means you’re to set up the Views and ViewModels for the Project. (ex: MainPage -> MainPageViewModel).
2. Set up XFCustomInputAlertDialog basics
There’s few basic things you need to set up from my previous post as follows, and you can go back take a look at that post for these steps, An Await-able Transparent, Custom, Popup Input Dialog in Xamarin.Forms! 😉
- Setting up Rg.Plugins.Popup library
- Create InputAlertDialogBase control
- Create your Custom Input Views as you wish
- Manage code-behind of your Custom Input Views
That’s it! nothing more! Alright let’s get started off with leveraging to MVVM!
3. Creating the Service…
So we shall create the service for our Custom Alert Dialogs. We shall call it InputAlertDialogService, thereby start off by creating the interface, IInputAlertDialogService with required methods.
You can add any amount of Service method calls to the interface which you can use to implement the concrete implementation.
Next let’s create the concrete implementation of our service, InputAlertDialogService.
There we are inheriting from our interface and doing the necessary concrete implementation. You will add the Popup Dialog instantiating and pushing to the navigation stack logic as you did in XFCustomInputAlertDialog code. Since its going to be repeated I’m not going to post the same code snippet here.
You can take a quick peak in the Gitub Repo though 😉
Then we need to register our Service Interface and Concrete implementation in the Prism’s default Unity Container.
There you go, the service layer is now ready! 😀
4. Consuming the Service…
Now let’s consume our Custom Popup Dialog Service in the ViewModel. First let’s inject it to the ViewModel and prepare it to be used when you need it.
Thanks to the IoC pattern, look at that beautiful and clean code. 😉
Now you’re ready to invoke the awesome custom popup dialogs from anywhere in your ViewModel. Let’s do that as the final step, shall we?
If you don’t get the hint, you can simply create a Command in your ViewModel and bind that to a Button or something in your View and add the above call to the Command’s execution method as shown below. 😉
There you go pure MVVM-Xamarin.Forms goodness! 😀
If you’re one of the lazy you can grab the whole code from Github: : https://github.com/UdaraAlwis/XFInputAlertDialogService
Well fellas, that’s it!
Enjoy! 😀 Share the love!
-Udara Alwis 😀