我默认从TabbedPageMasterDetailPageViewModel推送到TabbedPage,显示了选择的第一个标签。如何选择第二个标签?

这是我在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在xaml中定义了两个选项卡
public partial class HomePage : TabbedPage

从侧面菜单调用MoviesTab()方法时,始终会出现第一个选择的选项卡。

编辑

第一种情况不起作用,第二种情况下抛出NullReferenceExeption异常

场景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;

方案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;

c# - 从ViewModel推送时如何更改TabbedPage的TabIndex-LMLPHP

最佳答案

TabbedPage保存到本地变量,并在显示它之前更改选项卡索引:

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

或在将其推为当前详细信息后获得当前的“详细信息”页面,将其转换为TabbedPage,然后设置索引:
((App.Current.MainPage as MasterDetailPage).Detail as TabbedPage).TabIndex = 1;

关于c# - 从ViewModel推送时如何更改TabbedPage的TabIndex,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53294630/

10-13 06:05