Search code examples
c#xamarinxamarin.formsprismviewmodel

How to call services when moving one ViewModel to another


I have LandingPageViewModel with GetHomework Button. If I click on GetHomework Button it takes to HomeworkPageViewModel.

Now the problem is, where to call my service to get Homework data?

In LandingPageViewModels GetHomeworkExecute() method and hold all Homework record in public accessible variable OR use NavigationParameter to pass to the next page then use it in HomeworkPageViewModel(or in other ViewModels if holding record in global variable).

OR

In HomeworkPageViewModels OnNavigatingTo()(act as onload I think) method overridden by INavigatedAware

What is the good way to call the service when moving one ViewModel to another?


Solution

  • I'd put it into HomeworkPageViewModel, probably in the constructor (unless it's an async call). Rationale: if the HomeworkPageViewModel can do it (because it does not need additional info), then let it do it. Try not to spread out things, keep your code together.