Creating a movie app using Xamarin.Forms and Prism – Part 2

Pieter van der Westhuizen, a professional freelance web & mobile developer and founder of Coalition Software.
Search for a command to run...

Pieter van der Westhuizen, a professional freelance web & mobile developer and founder of Coalition Software.
A recent requirement came up for configurable dynamic forms in a mobile application. There are a few dynamic form packages for Flutter, but I wanted a bit more control over the schema and behavior of the form. Here follows my attempt at creating dyna...

If you've ever written a mobile app with Flutter that integrates with an API, then you'll know that there are a lot of model objects that get passed between the app and the API. JSON Serialization in Flutter can also take a fair amount of boilerplate...

Back in May 2020, Microsoft announced a new feature for Azure Blob Storage called Blob Index. Essentially, this enables you to add key/value tags to your Blob objects and be able to query said Blob objects without having to use a separate service lik...

In my last post, "Using AWS Cognito with Xamarin Forms", I showed how to authenticate with Amazon Cognito using Xamarin Forms and the Xamarin.Essentials Web Authenticator. In this post, we'll go through the process of using the AWS Cognito Hosted UI ...

In this post, I'll show you how to quickly and easily set up user authentication for your Xamarin Forms app using Amazon Cognito. AWS Cognito is a user identity management solution by Amazon. It is a really easy way to add authentication to your appl...

In the previous post, Creating a movie app using Xamarin.Forms and Prism – Part 1, we went through the process of creating and setting up a new Xamarin. Forms project using the Prism library and Visual Studio.
In today’s post, we’ll go through the steps of retrieving data from the web service and displaying it in a list view.
Since Part 1, the Prism library has had significant updates. If you’ve created the app from scratch before the Prism updates, you would need to update your NuGet packages as well as the RegisterTypes method in the App.xaml.cs, MainActivity.cs and AppDelegate.cs file in each project, to the following:
We’ll continue on the code we created Part 1. You can get all the code for this series on GitHub, so go ahead and open the project you created in Part 1. We’re going to show a list of movie genres on the first page of the app. The project already contains a MainPageViewModel class which we can use, however, we’ll add our own Genre page and ViewModel by following these steps:
GenrePage.xaml and click the Add button.
Because you’re using the Prism ContentPage item template, it will automatically create the following files for you:
GenrePage.xaml file with it’s associated code-behind Genre.xaml.cs and;GenrePageViewModels.cs file inside the ViewModels folder, which will act as the new page’s associated view model.It will also automatically register the newly created page for navigation by adding the following line to theRegisterTypes method in the App.xaml.cs file:
containerRegistry.RegisterForNavigation();
Now, since we want to display a list of movie genres when the app first opens, we need to specify that the newly added GenrePage, should be the first page that is shown. To do this open App.xaml.cs and change the following line:
await NavigationService.NavigateAsync("NavigationPage/MainPage");
to
await NavigationService.NavigateAsync("NavigationPage/GenrePage");
Next, we need to fetch a list of movie genres from The Movie DB API and populate a list view on the Genre page. First, we need to make a small change to the ViewModelBase class by doing the following:
ViewModelBase.cs file in the ViewModels folder.IsBusy to the ViewModelBase class. You can use the Prism propp snippet to make this easier. The property should look like:If you want to learn more about the available Prism snippets, be sure to read my post, Prism Library Quick Tip – Snippets
Next, we need to work on the view model for the Genre page.
GenrePageViewModel.cs file inside the ViewModels folder.ViewModelBase and add a constructor with two parameters, one of type INavigationService and another IMovieRepository:ObservableCollection property, called Genres, that will contain a list of genres to the class:DelegateCommand, which will be used to retrieve the genre data, called LoadGenresCommand. Use the cmd snippet to generate most of the code for you.LoadGenres() method to the following (The ToObservableCollection method is an extension method in the ListExtensions class inside the Extensions folder): LoadGenresCommand.Execute();
RegisterTypes method in the App.xaml.cs class:At this point, we have all the necessary code in place, we just need to create the UI to list the genres. We’ll create a simple UI to start with, that will only list the available movie genres, by completing the following steps:
GenrePage.xaml file inside the View folderGenrePage.xaml with the following:In the code above, we bind the page title to the view model’s Title property, as well as bind the list view to the Genres property. Note, that we also bind IsRefreshing to our view models’ IsBusy property – this will automatically show a progress indicator when the data loads.
You should now be able to see a list of genres retrieved from The Movie Db:


If you’re having problems running the example on iOS, do not panic. There are a few things we need to do to make it work that will be covered in Part 3.
In the next part, we’ll create another page showing all movies for a specific genre and the movie detail. You can find all code for this series on GitHub.
Thank you for reading. Until next time, keep coding!