Nested tabs views are not binding to separate ViewModel - xamarin

i want to bind viewmodel to nested tab views in xamarin prism framework
i created 4 main pages(A,B,C,D) as main tabs and inside first tab(A) i created two more tabs(A1,A2).but data for nested tabs are not binding.even viewmodel for that views(A1,A2) are not hitting
MenuPage.xml
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Views.MenuPage"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
xmlns:views="clr-namespace:MyApp.Views"
BarBackgroundColor="White"
android:TabbedPage.ToolbarPlacement="Bottom"
NavigationPage.HasNavigationBar="True">
<TabbedPage.Children>
<views:A Title="A" Icon="abc.png" />
<views:B Title="B" Icon="abc.png" />
<views:C Title="C" Icon="abc.png" />
<views:D Title="D" Icon="abc.png"/>
</TabbedPage.Children>
</TabbedPage>
and my page A is like
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:MyApp.Views"
x:Class="MyApp.Views.A">
<!--Pages can be added as references or inline-->
<TabbedPage.Children>
<views:A1 Title="A1" />
<views:A2 Title="A2" />
</TabbedPage.Children>
</TabbedPage>
and i have separate viewmodel for A1 and A2.
so if i directly bind A1 to main navigation page it will work properly and render data.but if i do like above viewmodel for A1 is not hitting the constructor and nothing is displaying apart from static data.i am new to tabbed page navigation.any help is appreciated.this the view i am trying t achieve

I find we should add AutowireViewModel in the page's XAML to load the view model we want:
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
Generally, we use containerRegistry.RegisterForNavigation<>(); to bind the custom view model to a certain view. But you placed a sub tabbed page in the root tabbed page. This caused the nested view losing the mapping to the corresponding view model. After adding the AutowireViewModel, this issue fixed. We could still use RegisterForNavigation to bind your custom view model to your special view instead of the automatic naming conversation.
Here is my sample for a simple nested tabbed page: https://github.com/landl0526/PrismTabDemo. Refer to it for more detailed code.
Moreover, this only works on the Android platform as iOS only has the bottom aligned tabbar.

Related

what is the difference between x:DataType and BindingContext

I have these values on a xamarin.Form page. Both of them give me intellisense. What would be the scenarios to use either or both of them?
<ContentPage x:DataType="vm:AboutViewModel" >
<ContentPage.BindingContext>
<vm:AboutViewModel>
</vm:AboutViewModel>
</ContentPage.BindingContext>
</ContentPage>
BindingContext assigns an instance of the specified class at runtime.
x:DataContext is a design time helper that tells Intellisense what the type of the context for a given block of XAML is

How do I navigate from MasterDetailPage to ContentPage in Xamarin forms using Prism saving the navigation stack?

I'm working on a Xamarin+Prism app and now facing an issue. The application uses MasterDetailPage as a layout. The details page contains a google map with few markers. When you tap the marker, a popup appears, if you tap this popup, a separate view should be opened (let's call it Page1).
I want this view NOT to be a part of MasterDetailsPage and at the same time to have a "Back" button on the top navigation panel. This "Back" button should lead back to MasterDetailsPage. And here comes the issue. I haven't found a way to do this. If you don't use Prism, it's done very simple by pushing directly to NavigationStack. But Prism doesn't allow you to do so, you only have NavigationService with NavigateAsync. Is there a way to implement this kind of behavior? Or maybe any hacks?
MasterDetailsPage code
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Find.Views.MainPage"
xmlns:pages="clr-namespace:Find.Views">
<MasterDetailPage.Master>
<pages:MenuPage />
</MasterDetailPage.Master>
</MasterDetailPage>
I've tried few ways of navigation like:
await NavigationService.NavigateAsync($"Page1");
await NavigationService.NavigateAsync($"/Page1");
await NavigationService.NavigateAsync($"NavigationPage/Page1");
and others, but nothing worked.
I've found the similar question, but an answer there is not correct.
How do you navigate Xamarin Forms using Prism from Master Detail to Content Page without Master Detail
Do you want to achieve the result like following GIF?(Note: I will navigate a page1 when click the Button)
First of all, I registered my pages in RegisterTypes method like following code.
protected override void RegisterTypes(IContainerRegistry containerRegistry) {
containerRegistry.RegisterForNavigation<MenuPage>();
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<ViewA>();
containerRegistry.RegisterForNavigation<Page1>();
containerRegistry.RegisterForNavigation<ViewB>();
}
If you want to have a back arrow, you need keep your navigted page not in root navigate stack.
For example. I display the ViewA by default, this navigate code like following
await _navigationService.NavigateAsync(nameof(NavigationPage) + "/" + "ViewA");
Then, when we want to navigate to the Page1 from ViewA, we should use following code that will display the backarrow.
await _navigationService.NavigateAsync(nameof(NavigationPage) + "/" + "ViewA" + "/" + "Page1");
I upload my demo, you can refer to it.
https://github.com/851265601/XFromsPrismNavi

Duplicate Xamarin Navbar Toolbar Items

I'm making an app in Xamarin Forms and when I deploy it to Android, I get a weird problem. It appears that I have two toolbar items when I only declared one in XAML. Also, I believe this is proper behaviour, but I was wondering if it's possible to not display the navbar title on a lower line and rather display it upper center. Thanks!
Screenshot
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Biziboards.Views.MerchantListPage"
Title="Merchants">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Home"
x:Name="btnHome"
Clicked="btnHome_Clicked"
Order="Primary"/>
</ContentPage.ToolbarItems>
In your pages Xaml.cs file:
NavigationPage.SetHasNavigationBar(this, false);

How to add Search bar in top of a tabbed page in xamarin forms

In my xamarin app,I want to add a search bar on top of the page in my tabbed page.
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Sample.Mobile.Test">
<StackLayout>
<SearchBar></SearchBar>
</StackLayout>
<TabbedPage.Children>
<ContentPage x:Name="tab1" Title="Home">
</ContentPage>
</TabbedPage.Children>
</TabbedPage>
You can use follow this link for the Search bar Implementation
https://developer.xamarin.com/api/type/Xamarin.Forms.SearchBar/

How to setup a content page of a tabbed page in XAML in Xamarin?

I want to setup a tabbed page with three content pages they are page1, page2 and page3 all of them are content pages. When the program starts I want to show page1 content page with page1 tab selected. They do not follow item template model since they are all different content pages compared to the example showing in the Tabbed Page example # xamarian.
<TabbedPage.Children>
<ContentPage Title="Page 1" />
<ContentPage Title="Page 2" />
<ContentPage Title="Page 3" />
</TabbedPage.Children>
What property I should set here so that I can point to the content of the associated content page or do I follow tab selecte event and manually call the appropriate content page? I would like to try to do it with XAML as much as possible.
Thanks
Found the answer here just in case looking for the same question. In my case, I was able to do it in the code behind, but you can do the same in XAML as the post says. One additional thing is, when you add a content page to a tab item and if you want to add title and Icon to it, then you can reference the index of the child and you can set them manually as .Tile and .Icon.
this.Children.Add( new Page1 ());
this.Children[0].Title = "Page 1";
this.Children[1].Icon = "page1.png"

Resources