【发布时间】:2019-07-22 10:53:50
【问题描述】:
我正在尝试在我的 UWP 应用中实现 NavigationView。我按照 Microsoft 教程进行操作。
我检查了每个视图页面中的文本块与 NavigationView 元素的 XAML 代码中的项目标记和代码隐藏中的字符串完全相同。
//这是C#代码隐藏
区域 NavigationView 事件处理程序
private void nvTopLevelNav_Loaded(object sender, RoutedEventArgs e)
{
foreach (NavigationViewItemBase item in nvTopLevelNav.MenuItems)
{
if (item is NavigationViewItem && item.Tag.ToString() == "logFood")
{
nvTopLevelNav.SelectedItem = item;
break;
}
}
ContentFrame.Navigate(typeof(LogFood));
}
private void nvTopLevelNav_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
}
private void nvTopLevelNav_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
if (args.IsSettingsInvoked)
{
//put settings page here
}
else
{
TextBlock ItemContent = args.InvokedItem as TextBlock;
if (ItemContent != null)
switch (ItemContent.Tag.ToString())
{
case "logFood":
ContentFrame.Navigate(typeof(LogFood));
break;
case "calculate":
ContentFrame.Navigate(typeof(MyNewPage));
break;
case "setNutritionGoals":
ContentFrame.Navigate(typeof(SetGoals));
break;
}
}
}
#endregion
//这是XAML代码
<NavigationViewItem x:Name="logFood_NavViewItem" Icon="Edit" Content="Log Food" Tag="logFood"/>
<NavigationViewItem x:Name="calculate" Icon="Calculator" Content="Calculate Nutrition Goals" Tag="calculate" />
<NavigationViewItem x:Name="saveGoals" Icon="Save" Content="Set Nutrition Goals" Tag="setNutritionGoals"/>
</NavigationView.MenuItems>
<Frame x:Name="ContentFrame">
<Frame.ContentTransitions>
<TransitionCollection>
<NavigationThemeTransition/>
</TransitionCollection>
</Frame.ContentTransitions>
</Frame>
</NavigationView>
我希望能够在不同的 XAML 页面之间导航。但是,当我按下导航视图中的每个按钮时,所选项目会突出显示,但没有任何反应。
【问题讨论】: