C
C#2y ago
reeeeeee

MAUI navigation to new page with injected viewModel

If you think this belong more into #mobile 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) THis is what happens when you open the app. But what about navigating to another page, for example public SecondPage(SecondViewModel viewModel) Without injected viewModel I could do 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 in the mainViewModel as well, and navigate to another page like await App.Current.MainPage.Navigation.PushAsync(new SecondPage(secondViewModel)); or is there any otehr way?
5 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
reeeeeee
reeeeeee2y ago
Will try to implement it like this, thanks! why do you think that injecting VM via DI is bad?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
reeeeeee
reeeeeee2y ago
Idk, it seemed easier to just inject the VM and get it from the constructor (except navigating, haha), and there was no need for something like IoC Container or something like that (like I had it in Xamarin.Forms). I was researching this a bit more, and came across AppShell, which I never used it before... The usage for navigating is pretty easy await Shell.Current.GoToAsync("newPage") (and ofc I have to register it in App.xaml.cs), which also gets injected objects in constructor. The downside of it is sendong some parameters when navigating to new page, but yeah... Did u ever use AppShell? What do you think about AppShell vs NavigationPage?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server
More Posts
catching an exception in an async Task [Answered]I can't seem to catch an exception that is thrown in an async Task, I put try/catch within the asyncpass data onclick from one page to another page in asp nethi all, im trying to redirect a page from **Main Page** to **Sub Page** when i click an item on **MASP NET Core default DI-Container, how pass in constructor not-registered parameter? [Answered]I want to create a Model class using a DI container, services registered in the container are passedShould I return the whole created object in POST or just return the id of that object ?When I creating a post method I find myself repeat what I did in GetById Should I just return the Iasp net cshtml javascript filehi all, i have an asp net project (SimpleApp) and in a cshtml file of my **SimpleApp ** project i trChange Visual Studio suggetions with custom analysersHiya.. ohh discords new "forum" channels are super useful for this huh?. anyway. So I managed to impProgram won’t run with just .net runtime [Answered]So my program that I published and is targeting .net 6 won’t run when the pc has just .net 6 runtimeNullReferenceException Object reference not set to an instance of an object```cs List<Effects> effectsList = activePlayerEffects[targetPlayer] ?? new List<Effects>(); ``` someDbContext.SaveChangesAsync has no effect on the collection of changed objectsHi. I get products collection from dbContext, change some values and want to save it in the databaseWindowsIdentity claims don't include all AD attributes, how do I fix that?I'm using windows authentication and want to display a user's full name, however on inspection the i