Retrieving the return result from the previous ViewModel
Last updated
Was this helpful?
Last updated
Was this helpful?
Now it is time to deal with the Edit function. You can find the PostEditView
for specific-platform projects on my GitHub: .
Create a new class file named PostEditViewModel.cs
in the ViewModels
folder in the MvvmCrossDemo.Core
project, as shown below:
There are a lot of similarities between the PostDetailViewModel
and the PostEditViewModel
. We need to receive the current post id and get the data from the APIs. But for PostEditViewModel
, we need to edit the Post
and pass the result to the PostListViewModel
to update the UI.
Come back to check the IMvxNavigationService
interface. There is an interface that can return a result to the previous ViewModel
, as shown below:
And another interface to receive the result:
Open the PostEditViewModel.cs
and change the parent class of it from MvxViewModel<PostViewModel>
to MvxViewModel<PostViewModel, Post>
, that means it will return a result which is the Post
type. Add two new commands here:
There are two buttons to respond to the user’s action. Each of them will call the Close
method but for the Cancel
button, it will return a null as the result. When the user clicks the Ok
button, the EditPostAsync
method will post the update to the API and if the result is a success, another Close
method will be called so the MvxNavigationService
will close the current ViewModel
and return a result to the previous ViewModel
.
Then update the EditPostAsync
method in the PostListViewModel.cs
file to receive the result, as shown below:
Launch the App for the Android and the UWP. Now you can edit the Post. When you return to the PostListView
, you should notice that the Post has been updated.
We did a lot of changes in this section. Be aware of the data context of your current controls to implement the data-binding. If you found that the command does not work well, check the data context first to make sure the command is correctly binding to the control.
Notice that the resource will not be really updated on the server but it will be faked as if. For more details about the fake API here: .