I'm trying to create a TabbedPage view on the company app.
First of all, to assure evertything is working, I created a XAML ContentPage MyTabbedPage, and configured the Dependency Injection for it on the MauiProgram.cs just like on the other views
mauiAppBuilder.Services.AddSingleton<MyTabbedPage>();
On my main page, I navigate to it using a button that is binded to this command
[RelayCommand]
private async Task Create() => await _navigationService.NavigateToPage<MyTabbedPage>();
This is the NavigateToPage method on the _navigationService:
public async Task NavigateToPage<T>(object? parameter = null) where T : Page
{
var toPage = ResolvePage<T>();
if (toPage is not null)
{
//Subscribe to the toPage's NavigatedTo event
toPage.NavigatedTo += PageNavigatedTo;
//Get VM of the toPage
var toViewModel = GetPageViewModelBase(toPage);
//Call navigatingTo on VM, passing in the parameter
if (toViewModel is not null)
await toViewModel.OnNavigatingTo(parameter);
//Navigate to requested page
await Navigation.PushAsync(toPage, true);
//Subscribe to the toPage's NavigatedFrom event
toPage.NavigatedFrom += PageNavigatedFrom;
}
else
throw new InvalidOperationException($"Unable to resolve type {typeof(T).FullName}");
}
And it works as expected.
But what I really need is a TabbedPage, so I change the ContentPage to a TabbedPage (on both the XAML and the xaml.cs files), and set the children of the TabbedPage, so the XAML is now this:
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:MyApp.Pages"
x:Class="MyApp.Pages.MyTabbedPage"
Title="MyTabbedPage">
<views:MyChildrenPage/>
</TabbedPage>
Now, when I press the button to navigate to it, I get the error:
Java.Lang.IllegalArgumentException: 'No view found for id 0x7f0a018b (CONFIDENTIALPATH/navigationlayout_toptabs) for fragment ViewFragment{a6b7d12} (fec1b5a7-e8fa-4129-a651-e8c4978783fb id=0x7f0a018b)'
*CONFIDENTIALPATH -> censored path with the name of the company's app.
I tried without the children, and also with a fresh ContentPage as a children, but the problem seems to be with the TabbedPage itself because I still get the same error.
Some information that might be useful:
- I couldn't find anything about this error on the internet, besides the "same error" on other languages/frameworks, like this one. None of those were caused by tabs, and they also didn't help me understand the problem.
- I'd like to avoid using Shell stuff if possible.
- I'm following Microsoft's TabbedPage documentation.
- I'm using MVVM, so the MyChildrenPage view has a binding with its ViewModel as follows
public partial class MyChildrenPage : ContentPage
{
public MyChildrenPage()
{
InitializeComponent();
//Done this way because TabbedPage will complain if the children has a constructor with
//parameters, even tho Microsoft tells us to use MVVM. How ironic huh?
var viewModel = Application.Current.MainPage.Handler.MauiContext.Services.GetService<MyChildrenViewModel>();
BindingContext = viewModel;
}
}