Xamarin & MvvmCross Handbook
  • Introduction
  • Using MvvmCross to create your first Xamarin App
    • Creating the Core project
      • Understanding MVVM
      • Adding a simple Service
      • Adding a ViewModel
      • Adding an App class
    • Creating the Android project
      • Adding an Android Application class
      • Adding the Android Layout View (AXML)
      • Understanding the data-binding syntax
      • Adding the View class
    • Creating the iOS project
      • Updating the AppDelegate class
      • Adding the iOS View for the first ViewModel
      • Understanding the data-binding syntax
    • Creating the UWP project
      • Updating the App.xaml.cs and the App.xaml
      • Adding the UWP View
      • Understanding the data-binding syntax
    • Creating the REST API Service
      • Creating the models
      • Creating the interface and the implementation for the PostService
      • Creating the ViewModel
      • Adding the View for the Android project
      • Adding the View for the iOS project
      • Adding the View for the UWP project
    • Navigation
      • Creating the PostDetail View & ViewModel
      • Command with the parameter
      • Retrieving the param from the previous ViewModel
      • Understanding the IMvxNavigationService
      • Responding the events from different controls in the ListView
      • Retrieving the return result from the previous ViewModel
    • Creating the Xamarin.Forms project
      • Creating the Forms.UI project
      • Adding the App.xaml and the App.xaml.cs
      • Adding the View
      • Creating the Android project
      • Creating the iOS project
      • Creating the UWP project
      • Summary For Forms
    • Summary
  • Implementing MasterDetail layout in Xamarin.Forms by MvvmCross
    • Introduction
    • Creating the project by MvxScaffolding
    • Creating the MasterDetailPage
      • Creating the ViewModel
      • Creating the XAML file
    • Creating the MasterPage
      • Creating the ViewModel
      • Creating the XAML file
    • Creating the DetailPages
      • Creating the ViewModels
      • Creating the XAML files
    • Implementing the Menu functionalities
      • Displaying the MasterPage and the DetailPage
      • Setting the menu navigation
      • The other approaches to set the data-binding
    • Fine-tuning the UI
      • Adding the hamburger icon for iOS
      • Adding the header bar for Android and iOS
      • Adjust the height of the item for UWP
    • Summary
Powered by GitBook
On this page

Was this helpful?

  1. Using MvvmCross to create your first Xamarin App
  2. Creating the REST API Service

Creating the ViewModel

PreviousCreating the interface and the implementation for the PostServiceNextAdding the View for the Android project

Last updated 6 years ago

Was this helpful?

Add a new ViewModel file into the ViewModel folder and name it as PostListViewModel.cs. It should inherit from the MvxViewModel class. Inject the implementation for the IPostService in the constructor method, like this:

private readonly IPostService _postService;

public PostListViewModel(IPostService postService)
{
     _postService = postService;
}

The constructor method is the right way to use the IoC container to inject all its dependencies.

Then add a new property for Data-Binding. You can type mvxpropdp and press Tab to complete the input, as shown as below:

#region PostList;
private MvxObservableCollection<Post> _postList;
public MvxObservableCollection<Post> PostList
{
    get => _postList;
    set => SetProperty(ref _postList, value);
}
#endregion

Now we need to find a place to get the data when the page is loading. Find more details about the lifecycle of the ViewModel here: .

Let us focus on the Initialize method in the ViewModel, which is used to do some heavy loading operations, such as loading data from the APIs. It returns a Task, which can be marked as async. Add an override to this method, like this:

public override async Task Initialize()
{
    // Async initialization, YEY!
    await base.Initialize();
    var response = await _postService.GetPostList();
    if (response.IsSuccess)
    {
        PostList = new MvxObservableCollection<Post>(response.Result);
    }
    else
    {
        // TODOï¼› Show some error messages.
    }
}

That is why I use a ResponseMessage class to encapsulate the result from the APIs. If there are any problems about the HTTP request, this encapsulation is able to make sure the UI layer can deal with the response result correctly because all exceptions and the unexpected values have been handled in the Service layer. If you got a result, which has a False value to the IsSuccess property, show a friendly message to users, rather than throwing an exception.

Then we need to navigate to the PostListViewModel from the FirstViewModel. Update the constructor of the FirstViewModel to inject the IMvxNavigationService, as shown below:

private readonly IGreetingService _greetingService;
private readonly IMvxNavigationService _navigationService;

public FirstViewModel(IGreetingService greetingService, IMvxNavigationService navigationService)
{
    _greetingService = greetingService;
    _navigationService = navigationService;
}

IMvxNavigationService is used to implement the navigation between views. I will describe more about it in the next section. Use the code snippet mvxcmda to create an async command in the FirstViewModel.cs to navigate to the PostListViewModel, like this:

        #region NavToPostListAsyncCommand;
        private IMvxAsyncCommand _navToPostListAsyncCommand;
        public IMvxAsyncCommand NavToPostListAsyncCommand
        {
            get
            {
                _navToPostListAsyncCommand = _navToPostListAsyncCommand ?? new MvxAsyncCommand(NavToPostListAsync);
                return _navToPostListAsyncCommand;
            }
        }
        private async Task NavToPostListAsync()
        {
            // Implement your logic here.
            await _navigationService.Navigate<PostListViewModel>();
        }
        #endregion
https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle