I will not post all the codes here and you can get the codes from the GitHub repo: https://github.com/yanxiaodi/MvvmCrossDemo. I also created a new Comment model, a service interface and the implementation for comments. Now look at the codes in the PostDetailViewModel:
namespaceMvvmCrossDemo.Core.ViewModels{publicclassPostDetailViewModel:MvxViewModel {privatereadonlyIPostService _postService;publicPostDetailViewModel(IPostService postService) { _postService = postService; }publicoverridevoidPrepare() { // This is the first method to be called after construction CommentList =newMvxObservableCollection<Comment>(); }publicoverrideasyncTaskInitialize() { // Async initialization, YEY!await base.Initialize(); }#regionPost;privatePost _post;publicPost Post {get=> _post;set=>SetProperty(ref _post, value); }#endregion#regionCommentList;privateMvxObservableCollection<Comment> _commentList;publicMvxObservableCollection<Comment> CommentList {get=> _commentList;set=>SetProperty(ref _commentList, value); }#endregionprivateasyncTaskGetPost(int postId) {var response =await_postService.GetPost(postId);if (response.IsSuccess) { Post =response.Result; } }privateasyncTaskTaskGetComments(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?