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. Navigation

Creating the PostDetail View & ViewModel

PreviousNavigationNextCommand with the parameter

Last updated 6 years ago

Was this helpful?

I will not post all the codes here and you can get the codes from the GitHub repo: . I also created a new Comment model, a service interface and the implementation for comments. Now look at the codes in the PostDetailViewModel:

namespace MvvmCrossDemo.Core.ViewModels
{
    public class PostDetailViewModel : MvxViewModel
    {
        private readonly IPostService _postService;

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

        public override void Prepare()
        {
            // This is the first method to be called after construction
            CommentList = new MvxObservableCollection<Comment>();

        }

        public override async Task Initialize()
        {
            // Async initialization, YEY!

            await base.Initialize();
        }


        #region Post;
        private Post _post;
        public Post Post
        {
            get => _post;
            set => SetProperty(ref _post, value);
        }
        #endregion

        #region CommentList;
        private MvxObservableCollection<Comment> _commentList;
        public MvxObservableCollection<Comment> CommentList
        {
            get => _commentList;
            set => SetProperty(ref _commentList, value);
        }
        #endregion

        private async Task GetPost(int postId)
        {
            var response = await _postService.GetPost(postId);
            if (response.IsSuccess)
            {
                Post = response.Result;
            }
        }

        private async Task TaskGetComments(int postId)
        {
            var response = await _postService.GetComments(postId);
            if (response.IsSuccess)
            {
                CommentList.AddRange(response.Result);
            }
        }
    }
}

In this ViewModel, we have to get the postId from the previous view for the data initialization. But how to pass the param to the PostDetailViewModel?

https://github.com/yanxiaodi/MvvmCrossDemo