How to Bind ViewModels Correctly in Prism for .NET MAUI TabbedPage Navigation?
New Setup with Prism and TabbedPage: To replicate the same tabbed experience with Prism, I'm using TabbedPage like so:
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage
xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls"
android:TabbedPage.IsSmoothScrollEnabled="False"
android:TabbedPage.IsSwipePagingEnabled="False"
android:TabbedPage.ToolbarPlacement="Bottom"
xmlns:viewmodels="clr-namespace:App.ViewModels"
xmlns:views="clr-namespace:App.Views"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="Automatic">
<views:DashboardMainPage Title="Dashboard" IconImageSource="tab_home" />
<views:ProgressMainPage Title="Progress" IconImageSource="tab_progress" />
</TabbedPage>
And in my Prism registration:
containerRegistry.RegisterForNavigation<TabbedMainPage, TabbedMainPageViewModel>();
containerRegistry.RegisterForNavigation<DashboardMainPage, DashboardMainViewModel>();
In DashboardMainViewModel, I’ve added a simple TestCommand, which is bound to a button in the DashboardMainPage. However, after migrating to Prism and using this TabbedPage, the command is not invoked at all.
The Problem: It seems like the ViewModel for DashboardMainPage is not being initialized or bound correctly. In Shell, I used to manually inject the ViewModel and set BindingContext in the constructor, but now with Prism and TabbedPage, that doesn’t appear to be working.
Question: How can I ensure each tab’s content (e.g., DashboardMainPage) correctly binds to its corresponding ViewModel (DashboardMainViewModel) when using TabbedPage in Prism for .NET MAUI?
Any insights or working examples would be greatly appreciated!
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage
xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls"
android:TabbedPage.IsSmoothScrollEnabled="False"
android:TabbedPage.IsSwipePagingEnabled="False"
android:TabbedPage.ToolbarPlacement="Bottom"
xmlns:viewmodels="clr-namespace:App.ViewModels"
xmlns:views="clr-namespace:App.Views"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="Automatic">
<views:DashboardMainPage Title="Dashboard" IconImageSource="tab_home" />
<views:ProgressMainPage Title="Progress" IconImageSource="tab_progress" />
</TabbedPage>
And in my Prism registration:
containerRegistry.RegisterForNavigation<TabbedMainPage, TabbedMainPageViewModel>();
containerRegistry.RegisterForNavigation<DashboardMainPage, DashboardMainViewModel>();
In DashboardMainViewModel, I’ve added a simple TestCommand, which is bound to a button in the DashboardMainPage. However, after migrating to Prism and using this TabbedPage, the command is not invoked at all.
The Problem: It seems like the ViewModel for DashboardMainPage is not being initialized or bound correctly. In Shell, I used to manually inject the ViewModel and set BindingContext in the constructor, but now with Prism and TabbedPage, that doesn’t appear to be working.
Question: How can I ensure each tab’s content (e.g., DashboardMainPage) correctly binds to its corresponding ViewModel (DashboardMainViewModel) when using TabbedPage in Prism for .NET MAUI?
Any insights or working examples would be greatly appreciated!