1

I have a Xamarin Forms application with a TabbedPage that contains different Children pages. I want to be able to know which child (tab) is active from my viewmodel. So I can

The ServiceListPage.xaml looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<pages:BaseTabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:pages="clr-namespace:ABBI_XPlat_3.Pages;assembly=ABBI_XPlat_3"
    x:Class="ABBI_XPlat_3.Pages.ServiceListPage"
    SelectedItem="{Binding CurrentTab, Mode=TwoWay}"
    Title="Control ABBIs">
<pages:BaseTabbedPage.ToolbarItems>
    <ToolbarItem x:Name="DisconnectButton" Name="Disconnect" />    
</pages:BaseTabbedPage.ToolbarItems>
<pages:BaseTabbedPage.Children>
    <pages:ProfilesPage Title="Profiles" />
    <pages:VolumePage Title="Volume" />
    <pages:SensitivityPage Title="Sensitivity" />
    <pages:RemoteCtrlPage Title="Remote Control" />

</pages:BaseTabbedPage.Children>
</pages:BaseTabbedPage>

All the children pages use the same viewmodel. Which I navigate to using mvvmcross:

ShowViewModel<ServiceListViewModel>(new MvxBundle(bundle));

I have tried to Bind the CurrentTab property in the viewmodel, but the setter never gets called when I change tabs.

How can I detect which Tab is active then?

hcaltenco
  • 41
  • 1
  • 7
  • ItemsSource/SelectedItem, where the items source are a list of view models that are the tabs in the control and the selected item which is always the selected tab. This answer has a full example you can throw into a prototype and see how it works. http://stackoverflow.com/questions/5650812/how-do-i-bind-a-tabcontrol-to-a-collection-of-viewmodels/5651542#5651542 –  Dec 08 '16 at 15:37
  • You can use Messaging Center to send your current tab from xaml.cs to view model. – Yura Babiy Dec 08 '16 at 17:16
  • Thank you @Will for the suggestion. I tried to use TabControl instead of TabbedPage.Children, but the tabs did not look native and I did not manage to make it work. I must have been missing something. I solved the problem by 'OnPropertyChanged()' that raises a 'CurrentPageChangedEventHandler' event in the BaseTabbePage class. And from there I can assign the value of a public property in the BaseViewModel ('BaseViewModel.CurrentPage = CurrentPage;' which I can read from the view model – hcaltenco Dec 09 '16 at 11:38
  • Oh! Sorry, I didn't notice the xamarin tag! My bad. –  Dec 09 '16 at 13:46
  • @YuraBabiy such a dirty trick. Why would you recommend using this? – mr5 Dec 14 '18 at 02:57

0 Answers0