© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
17 replies
reeeeeee

MAUI navigation to new page with injected viewModel

If you think this belong more into No Access channel I can move, but I think some of you who are more familiar with dependency injection might be able to understand/help.
In .net MAUI there is a dep.inj and I watched some tutorial where they injected ContentPage classes, as well as viewModels (and those have some injected stuff in constructors as well).
So currently I have something like this:
public App(MainPage mainPage)
{
    MainPage = mainPage);
}
public App(MainPage mainPage)
{
    MainPage = mainPage);
}

and my Mainpage constructor would be
public MainPage(MainViewModel viewModel)
public MainPage(MainViewModel viewModel)

THis is what happens when you open the app. But what about navigating to another page, for example
public SecondPage(SecondViewModel viewModel)
public SecondPage(SecondViewModel viewModel)

Without injected viewModel I could do
await App.Current.MainPage.Navigation.PushAsync(new SecondPage());
await App.Current.MainPage.Navigation.PushAsync(new SecondPage());
but now it gives an error becase of missing VM. So how should I approach this? Should I inject
SecondViewModel
SecondViewModel
in the mainViewModel as well, and navigate to another page like
await App.Current.MainPage.Navigation.PushAsync(new SecondPage(secondViewModel));
await App.Current.MainPage.Navigation.PushAsync(new SecondPage(secondViewModel));
or is there any otehr way?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

MAUI - How to merge flyout navigation with navigation from code - GoToAsync()?
C#CC# / help
2y ago
❔ .NET MAUI MVVM Navigation and pass Object to new page, Throw Exception: Missing method
C#CC# / help
3y ago
How to Bind ViewModels Correctly in Prism for .NET MAUI TabbedPage Navigation?
C#CC# / help
8mo ago
ViewModel with BaseViewModel
C#CC# / help
2mo ago