Arvind Chourasiya
Arvind Chourasiya

Reputation: 17422

How to change TabIndex of TabbedPage when pushing it from ViewModel

I am pushing TabbedPage to MasterDetailPage from ViewModel by default TabbedPage showing first tab selected. How can select second tab?

This is my code in ViewModel

async Task MoviesTab()
{
    await (App.Current.MainPage as MasterDetailPage).Detail.Navigation.PushAsync(new HomePage());
    (App.Current.MainPage as MasterDetailPage).IsPresented = false;
    //var pages=new HomePage().Children.GetEnumerator();
    //pages.MoveNext();
    //new HomePage().TabIndex = 1; //Not working
}

HomePage has two tab defined in xaml

public partial class HomePage : TabbedPage

When ever calling MoviesTab() method from side menu always appearing first tab selected.

Edit

The first scenario is not working & second throwing NullReferenceExeption exeption

Scenario 1

var tabbedPaged = new TabbedPage();
tabbedPaged.TabIndex = 1;
await (App.Current.MainPage as MasterDetailPage).Detail.Navigation.PushAsync(new HomePage());
(App.Current.MainPage as MasterDetailPage).IsPresented = false;

Scenario 2

await (App.Current.MainPage as MasterDetailPage).Detail.Navigation.PushAsync(new HomePage());
(App.Current.MainPage as MasterDetailPage).IsPresented = false;
((App.Current.MainPage as MasterDetailPage).Detail as TabbedPage).TabIndex = 1;

enter image description here

Upvotes: 2

Views: 1565

Answers (2)

Edgaras
Edgaras

Reputation: 527

Setting TabIndex does nothing.

This is how I navigate:

tabbedPage.CurrentPage = tabbedPage.Children.FirstOrDefault(c => c.Title == "Page Title");

Optional explanation:

If your tabbed page is inside a NavigationPage and is the first page, you can access your TabbedPage like this:

var tabbedPage = (App.Current.MainPage as NavigationPage).RootPage as TabbedPage;

Page title is set in the ContentPage, to which you want to navigate to.

Upvotes: 1

SushiHangover
SushiHangover

Reputation: 74154

Save the TabbedPage to a local variable and change the tab index before showing it:

var tabbedPaged = new TabbedPage();
tabbedPaged.TabIndex = 1;
await (App.Current.MainPage as MasterDetailPage).Detail.Navigation.PushAsync(tabbedPaged);

Or get the current Detail page after you have pushed it as the current Detail, cast it as a TabbedPage and then set the index:

((App.Current.MainPage as MasterDetailPage).Detail as TabbedPage).TabIndex = 1;

Upvotes: 2

Related Questions