导航视图不自动关闭
Navigation View Not automatically closing
经过一些来回和一些良好的旧谷歌搜索,我能够让导航视图安静地工作(在这一点上我有点自豪)。
我有 2 个问题(可能已连接),主要问题是任何时候我 select 一个菜单项它都可以正确导航但是它无法 close/minimize 在 select 菜单项到左侧.(见下面的截图)
第二个问题(我怀疑是相关的)打开应用程序时本应自动 select 主页的代码行不断抛出异常,因此我删除了该行。
该行是 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
关于第一个问题,如果你想close/minimize选择项目左侧的菜单,你可以添加PaneDisplayMode="LeftCompact" .另外,你设置的frame应该放在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>
关于第二个issus,报错的原因是你没有给NavigationViewItem添加tag,而是在获取item.Tag的时候添加到textblock.So,会crash.You 可以像下面这样更正。
<NavigationViewItem Icon="Home" x:Name="Nav_Home" Tag="Nav_Home">
<TextBlock>Home</TextBlock>
</NavigationViewItem>
经过一些来回和一些良好的旧谷歌搜索,我能够让导航视图安静地工作(在这一点上我有点自豪)。 我有 2 个问题(可能已连接),主要问题是任何时候我 select 一个菜单项它都可以正确导航但是它无法 close/minimize 在 select 菜单项到左侧.(见下面的截图) 第二个问题(我怀疑是相关的)打开应用程序时本应自动 select 主页的代码行不断抛出异常,因此我删除了该行。 该行是 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
关于第一个问题,如果你想close/minimize选择项目左侧的菜单,你可以添加PaneDisplayMode="LeftCompact" .另外,你设置的frame应该放在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>
关于第二个issus,报错的原因是你没有给NavigationViewItem添加tag,而是在获取item.Tag的时候添加到textblock.So,会crash.You 可以像下面这样更正。
<NavigationViewItem Icon="Home" x:Name="Nav_Home" Tag="Nav_Home">
<TextBlock>Home</TextBlock>
</NavigationViewItem>