Search code examples
xamarinxamarin.formsxamarin-studio

SelectedItem in TabbedPage.xaml does not work Xamarin.Form


Hey everyone Good Day I have tabbed created in xaml, I prefer xaml because I have login in xaml code. My Code

Tab.xaml

<TabbedPage 
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:local="clr-namespace:FormsSample.Views;assembly=FormsSample"
        x:Class="FormsSample.Views.LoginPage"
        x:Name="TbPage">
     <TabbedPage.Children>
        <ContentPage x:Name="TbLog" Title="Login">
        </ContentPage>
        <ContentPage x:Name="TbSch" Title="Schedule">
        </ContentPage>
        <ContentPage x:Name="TbLis" Title="Customers">
        </ContentPage>
        </TabbedPage.Children>   
</TabbedPage>

Tab.xaml.cs

   namespace FormsSample.Views
   {
    public partial class LoginPage : TabbedPage 
    {

       private readonly TabbedPage _tbPage;
       private readonly ContentPage _tbList;
       private readonly ContentPage _tbLogn; 


       public LoginPage()
       {
        InitializeComponent ();
        _tbPage = this.FindByName<TabbedPage>("TbPage");
        _tbList = this.FindByName<ContentPage>("TbLis");
        _tbLogn = this.FindByName<ContentPage>("TbLog");
        RunTime();
        }

        private void RunTime()
        {
               _tbPage.CurrentPage = _tbLogn; 
           if (_tbPage.SelectedItem == _tbPage.Children[2])
           {
              DisplayAlert("Tab", "Hello World", "OK");
           }
         }

       }

     }

changing to

  _tbPage.SelectedItem == _tbList 

Its similar nothing happen, How to solve this? thanks a lot.


Solution

  • This may not be fancy but it works for me :D

            this.CurrentPageChanged += (o, e) => 
            {
               var i = this.Children.indexOf(this.CurrentPage); 
                if(i == 1 && UsrType == 2)
                {
                     DisplayAlert("Admin", "Administrator Privilege required!", "OK");
                     this.CurrentPage = _tbLog;
                }
            };