当导航到其他帧(UWP)时,如何更改选定的导航视图项?

当导航到其他帧(UWP)时,如何更改选定的导航视图项?,uwp,navigationview,Uwp,Navigationview,我有一个UWP,它的主页是NavigationView 虽然页面导航正确,但NavigationView.MenuItem中的SelectedItem仍然是频道,如何在函数中将其更改为Player 图片说明如下: 如您所见,框架实际上是PlayerPage,而SelectedItem是ChannelsPage。您可以创建一个公共方法来设置NavigationView的SelectedItem 在MainPage.xaml.cs中: 现在,导航到PlayerPage后,您可以执行以下操作: Fra

我有一个UWP,它的主页是NavigationView

虽然页面导航正确,但NavigationView.MenuItem中的SelectedItem仍然是频道,如何在函数中将其更改为Player

图片说明如下:

如您所见,框架实际上是PlayerPage,而SelectedItem是ChannelsPage。

您可以创建一个公共方法来设置NavigationView的SelectedItem

在MainPage.xaml.cs中:

现在,导航到PlayerPage后,您可以执行以下操作:

Frame appFrame = Window.Current.Content as Frame;
MainPage mainPage = appFrame.Content as MainPage;
mainPage.SetSelectedNavigationItem( 1 );
当然,访问主页面最好更简单,比如在主页面上创建静态方法:

另一个改进是创建一个枚举来替换神奇的索引整数。

您可以创建一个公共方法来设置NavigationView的SelectedItem

在MainPage.xaml.cs中:

现在,导航到PlayerPage后,您可以执行以下操作:

Frame appFrame = Window.Current.Content as Frame;
MainPage mainPage = appFrame.Content as MainPage;
mainPage.SetSelectedNavigationItem( 1 );
当然,访问主页面最好更简单,比如在主页面上创建静态方法:


另一个改进是创建一个枚举来替换神奇的索引整数。

谢谢,它解决了我的问题。这里的核心是将框架作为其他页面的主页。需要注意的一点是,导航ViewItemSeparator包含在MenuItems中,因此请记住在索引/枚举值中考虑它们。谢谢,它解决了我的问题。这里的核心是将框架作为其他页面中的主页。需要注意的一点是,导航ViewItemSeparator包含在MenuItems中,因此请记住在索引/枚举值中考虑它们。
public void SetSelectedNavigationItem(int index)
{
    MyNavigationView.SelectedItem = MyNavigationView.MenuItems[index];
}
Frame appFrame = Window.Current.Content as Frame;
MainPage mainPage = appFrame.Content as MainPage;
mainPage.SetSelectedNavigationItem( 1 );
public static MainPage GetCurrent()
{
    Frame appFrame = Window.Current.Content as Frame;
    return appFrame.Content as MainPage;
}