Creating the PostDetail View & ViewModel
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:
1
namespace MvvmCrossDemo.Core.ViewModels
2
{
3
public class PostDetailViewModel : MvxViewModel
4
{
5
private readonly IPostService _postService;
6
7
public PostDetailViewModel(IPostService postService)
8
{
9
_postService = postService;
10
}
11
12
public override void Prepare()
13
{
14
// This is the first method to be called after construction
15
CommentList = new MvxObservableCollection<Comment>();
16
17
}
18
19
public override async Task Initialize()
20
{
21
// Async initialization, YEY!
22
23
await base.Initialize();
24
}
25
26
27
#region Post;
28
private Post _post;
29
public Post Post
30
{
31
get => _post;
32
set => SetProperty(ref _post, value);
33
}
34
#endregion
35
36
#region CommentList;
37
private MvxObservableCollection<Comment> _commentList;
38
public MvxObservableCollection<Comment> CommentList
39
{
40
get => _commentList;
41
set => SetProperty(ref _commentList, value);
42
}
43
#endregion
44
45
private async Task GetPost(int postId)
46
{
47
var response = await _postService.GetPost(postId);
48
if (response.IsSuccess)
49
{
50
Post = response.Result;
51
}
52
}
53
54
private async Task TaskGetComments(int postId)
55
{
56
var response = await _postService.GetComments(postId);
57
if (response.IsSuccess)
58
{
59
CommentList.AddRange(response.Result);
60
}
61
}
62
}
63
}
Copied!
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?
Last modified 3yr ago
Copy link