ItemInvoked 与 SelectionChanged
ItemInvoked vs SelectionChanged
在 Windows UWP 中,NavigationView
的 ItemInvoked
和 SelectionChanged
事件有什么区别? API reference 状态
ItemInvoked
Occurs when an item in the menu receives an interaction such as a click or tap.
SelectionChanged
Occurs when the currently selected item changes.
在我看来 SelectionChanged
甚至可以通过单击 NavigationView.MenuItem
以外的其他方法检测导航何时发生,所以使用更好、更全面的选项会更好吗?
或者每个都有不同的用例?
主要区别在于 SelectionChanged
事件仅执行一次,但如果您重复单击所选项目,则不会触发。 ItemInvoked
另一方面,每次单击一个项目时都会执行,即使它已经被选中。
此外 - 当您在代码中手动设置 SelectedItem
时,将执行 SelectionChanged
事件。
另一件可能有用的注意事项是当您单击 NavigationView
项时 ItemInvoked
在 SelectionChanged
之前触发
我最近一直在努力解决这个问题,并且在使用 SelectionChanged 事件时遇到了问题。连续点击同一个菜单项没有产生任何结果。
然后我发现这个博客 post (https://blogs.msdn.microsoft.com/appconsult/2018/05/06/using-the-navigationview-in-your-uwp-applications/) 建议使用 ItemInvoked
事件,原因与答案 #1 中给出的原因相同。
这对我来说不起作用,因为在博客中 posted 但是在更改代码以使用 InvokedItemContainer
标签 属性 之后,解决方案工作得很好。
我已经包含了我用来验证解决方案的测试代码。
private void NvTopLevelNav_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
if (args.IsSettingsInvoked)
{
contentFrame.Navigate(typeof(SettingsPage));
}
else
{
string navTo = args.InvokedItemContainer.Tag.ToString();
if ( navTo != null)
{
switch (navTo)
{
case "Nav_Home":
contentFrame.Navigate(typeof(HomePage));
break;
case "Nav_Shop":
contentFrame.Navigate(typeof(ShopPage));
break;
case "Nav_ShopCart":
contentFrame.Navigate(typeof(CartPage));
break;
case "Nav_Message":
contentFrame.Navigate(typeof(MessagePage));
break;
case "Nav_Print":
contentFrame.Navigate(typeof(PrintPage));
break;
}
}
}
}
<Grid>
<NavigationView x:Name="nvTopLevelNav"
Loaded="NvTopLevelNav_Loaded"
Margin="0,12,0,0"
SelectionChanged="NvTopLevelNav_SelectionChanged"
ItemInvoked="NvTopLevelNav_ItemInvoked"
IsTabStop="False"
Header="Lets Go Shopping">
<NavigationView.MenuItems>
<NavigationViewItem Icon="Home" Content="Home" Tag="Nav_Home" />
<NavigationViewItem Icon="Shop" Content="Shop" Tag="Nav_Shop" />
<NavigationViewItem Content="Shopping Cart" Tag="Nav_Cart">
<NavigationViewItem.Icon>
<FontIcon Glyph="[Insert Hex Decimal Value Here. See Segoe MDL2 below for details.]"/>
</NavigationViewItem.Icon>
</NavigationViewItem>
<NavigationViewItem Icon="Message" Content="Message" Tag="Nav_Message" />
<NavigationViewItem Icon="Print" Content="Print" Tag="Nav_Print" />
</NavigationView.MenuItems>
<Frame x:Name="contentFrame"></Frame>
</NavigationView>
</Grid>
在 Windows UWP 中,NavigationView
的 ItemInvoked
和 SelectionChanged
事件有什么区别? API reference 状态
ItemInvoked
Occurs when an item in the menu receives an interaction such as a click or tap.SelectionChanged
Occurs when the currently selected item changes.
在我看来 SelectionChanged
甚至可以通过单击 NavigationView.MenuItem
以外的其他方法检测导航何时发生,所以使用更好、更全面的选项会更好吗?
或者每个都有不同的用例?
主要区别在于 SelectionChanged
事件仅执行一次,但如果您重复单击所选项目,则不会触发。 ItemInvoked
另一方面,每次单击一个项目时都会执行,即使它已经被选中。
此外 - 当您在代码中手动设置 SelectedItem
时,将执行 SelectionChanged
事件。
另一件可能有用的注意事项是当您单击 NavigationView
项时 ItemInvoked
在 SelectionChanged
之前触发
我最近一直在努力解决这个问题,并且在使用 SelectionChanged 事件时遇到了问题。连续点击同一个菜单项没有产生任何结果。
然后我发现这个博客 post (https://blogs.msdn.microsoft.com/appconsult/2018/05/06/using-the-navigationview-in-your-uwp-applications/) 建议使用 ItemInvoked
事件,原因与答案 #1 中给出的原因相同。
这对我来说不起作用,因为在博客中 posted 但是在更改代码以使用 InvokedItemContainer
标签 属性 之后,解决方案工作得很好。
我已经包含了我用来验证解决方案的测试代码。
private void NvTopLevelNav_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
if (args.IsSettingsInvoked)
{
contentFrame.Navigate(typeof(SettingsPage));
}
else
{
string navTo = args.InvokedItemContainer.Tag.ToString();
if ( navTo != null)
{
switch (navTo)
{
case "Nav_Home":
contentFrame.Navigate(typeof(HomePage));
break;
case "Nav_Shop":
contentFrame.Navigate(typeof(ShopPage));
break;
case "Nav_ShopCart":
contentFrame.Navigate(typeof(CartPage));
break;
case "Nav_Message":
contentFrame.Navigate(typeof(MessagePage));
break;
case "Nav_Print":
contentFrame.Navigate(typeof(PrintPage));
break;
}
}
}
}
<Grid>
<NavigationView x:Name="nvTopLevelNav"
Loaded="NvTopLevelNav_Loaded"
Margin="0,12,0,0"
SelectionChanged="NvTopLevelNav_SelectionChanged"
ItemInvoked="NvTopLevelNav_ItemInvoked"
IsTabStop="False"
Header="Lets Go Shopping">
<NavigationView.MenuItems>
<NavigationViewItem Icon="Home" Content="Home" Tag="Nav_Home" />
<NavigationViewItem Icon="Shop" Content="Shop" Tag="Nav_Shop" />
<NavigationViewItem Content="Shopping Cart" Tag="Nav_Cart">
<NavigationViewItem.Icon>
<FontIcon Glyph="[Insert Hex Decimal Value Here. See Segoe MDL2 below for details.]"/>
</NavigationViewItem.Icon>
</NavigationViewItem>
<NavigationViewItem Icon="Message" Content="Message" Tag="Nav_Message" />
<NavigationViewItem Icon="Print" Content="Print" Tag="Nav_Print" />
</NavigationView.MenuItems>
<Frame x:Name="contentFrame"></Frame>
</NavigationView>
</Grid>