经过来回的反复和一些很好的老式谷歌搜索之后,我能够使“导航”视图安静地工作(在这一点上,我为自己感到有些骄傲)。 我有2个问题(可能已连接),主要问题是,每当我选择一个菜单项时,它都能正确导航,但是在选择该菜单项时无法关闭/最小化左侧菜单。(请参见下面的屏幕截图) 第二个问题(我怀疑是连接的)打开应用程序时应该自动选择主页的代码行不断抛出异常,因此我删除了该行。 将是Mainpage.xaml.cs上的第40行,其内容为:
if (item is NavigationViewItem && item.Tag.ToString() == "Home_Page")
看起来像什么。 https://i.imgur.com/a59TATs.png
我试图设置一个变量:
NavView.IsPanelOpen = false;
但是我找不到正确的位置放置
这是MainPage.xaml
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:RussMenu"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-
compatibility/2006"
xmlns:Custom="using:Microsoft.Toolkit.Uwp.UI.Controls"
x:Class="RussMenu.MainPage"
mc:Ignorable="d"
Background="{ThemeResource
ApplicationPageBackgroundThemeBrush}">
<Grid>
<NavigationView x:Name="nvTopLevelNav"
Loaded="nvTopLevelNav_Loaded"
Margin="0,12,0,0"
SelectionChanged="nvTopLevelNav_SelectionChanged"
ItemInvoked="nvTopLevelNav_ItemInvoked"
IsTabStop="False"
>
<NavigationView.MenuItems>
<NavigationViewItem Icon="Home" x:Name="Nav_Home">
<TextBlock Tag="Nav_Home">Home</TextBlock>
</NavigationViewItem>
<NavigationViewItem Icon="SaveLocal"
x:Name="Nav_Submit_COAQC_Results">
<TextBlock Tag="Nav_Submit_COAQC_Results">Submit COA/QC
Results</TextBlock>
</NavigationViewItem>
<NavigationViewItem Icon="Trim"
x:Name="Nav_Edit_Test_Results">
<TextBlock Tag="Nav_Edit_Test_Results">Edit Test
Results</TextBlock>
</NavigationViewItem>
<NavigationViewItem Icon="ReportHacked"
x:Name="Nav_Create_Claims_Ticket">
<TextBlock Tag="Nav_Create_Claims_Ticket">Create Claims
Ticket</TextBlock>
</NavigationViewItem>
<NavigationViewItem Icon="Message"
x:Name="Nav_Message_Page">
<TextBlock Tag="Nav_Message_Page">Contact
Leadership</TextBlock>
</NavigationViewItem>
<NavigationViewItem Icon="Print" x:Name="Nav_Print_Page">
<TextBlock Tag="Nav_Print_Page">Print</TextBlock>
</NavigationViewItem>
<NavigationViewItem Icon="Help" x:Name="Nav_about">
<TextBlock Tag="Nav_about">About</TextBlock>
</NavigationViewItem>
<Image Source="C:
Projects\Russ\RussMenu\RussMenu\Assets\Russell-Standard.jpg">
</Image>
</NavigationView.MenuItems>
<NavigationView.AutoSuggestBox>
<!-- See AutoSuggestBox documentation for
more info about how to implement search. -->
<AutoSuggestBox x:Name="NavViewSearchBox"
QueryIcon="Find"/>
</NavigationView.AutoSuggestBox>
</NavigationView>
<Frame x:Name="contentFrame" HorizontalAlignment="Center"
VerticalAlignment="Center" Margin="50,12,50,50"/>
</Grid>
</Page>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Microsoft.Data.Sqlite;
// The Blank Page item template is documented at
https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace RussMenu
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a
Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public string PaneDisplayMode { get; private set; }
public MainPage()
{
this.InitializeComponent();
}
private void nvTopLevelNav_Loaded(object sender, RoutedEventArgs e)
{
// set the initial SelectedItem
foreach (NavigationViewItemBase item in nvTopLevelNav.MenuItems)
{
{
nvTopLevelNav.SelectedItem = item;
break;
}
}
}
private void nvTopLevelNav_SelectionChanged(NavigationView sender,
NavigationViewSelectionChangedEventArgs args)
{
{
}
}
private void nvTopLevelNav_ItemInvoked(NavigationView sender,
NavigationViewItemInvokedEventArgs args)
{
if (args.IsSettingsInvoked)
{
contentFrame.Navigate(typeof(Views.SettingsPage));
}
else
{
TextBlock ItemContent = args.InvokedItem as TextBlock;
if (ItemContent != null)
{
switch (ItemContent.Tag)
{
case "Nav_Home":
contentFrame.Navigate(typeof(Views.HomePage));
break;
case "Nav_about":
contentFrame.Navigate(typeof(Views.About));
break;
case "Nav_Message_Page":
contentFrame.Navigate(typeof(Views.Contact_Leadership));
break;
case "Nav_Create_Claims_Ticket":
contentFrame.Navigate(typeof(Views.CreateClaimsTicket));
break;
case "Nav_Print_Page":
contentFrame.Navigate(typeof(Views.PrintPage));
break;
case "Nav_Submit_COAQC_Results":
contentFrame.Navigate(typeof(Views.submitresults));
break;
case "Nav_Edit_Test_Results":
contentFrame.Navigate(typeof(Views.EditTestResults));
break;
}
}
}
}
}
}
它应该是什么样子。 https://i.imgur.com/HYUUhlJ.png
答案 0 :(得分:0)
关于第一个问题,如果要在选择项目时将其关闭/最小化到左侧菜单,则可以添加 PaneDisplayMode =“ LeftCompact” 。此外,您设置的框架应放置在NavigationView中。在这种情况下,展开的侧边栏将不会被textBlock遮盖。
<NavigationView x:Name="nvTopLevelNav"
Loaded="nvTopLevelNav_Loaded"
Margin="0,12,0,0"
SelectionChanged="nvTopLevelNav_SelectionChanged"
ItemInvoked="nvTopLevelNav_ItemInvoked"
IsTabStop="False"
PaneDisplayMode="LeftCompact"
>
...
<Frame x:Name="contentFrame" HorizontalAlignment="Center"
VerticalAlignment="Center" Margin="50,12,50,50"/>
</NavigationView>
关于第二个问题,错误的原因是您没有在NavigationViewItem中添加标签,而是在文本块中添加了标签。因此,当您尝试获取item.Tag时,它将崩溃。
<NavigationViewItem Icon="Home" x:Name="Nav_Home" Tag="Nav_Home">
<TextBlock>Home</TextBlock>
</NavigationViewItem>