【问题标题】:How to handle NavigationView PaneFooter Settings item click如何处理 NavigationView PaneFooter 设置项单击
【发布时间】:2020-06-02 13:45:58
【问题描述】:

添加NavigationView 时,“设置”项会自动添加到NavigationView 的底部。由于似乎无法从 XAML 访问,如何将单击事件添加到此项目?

<Page
    x:Class="My_Project.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <NavigationView>
            <NavigationView.PaneFooter>
            </NavigationView.PaneFooter>
        </NavigationView>
    </Grid>
</Page>

【问题讨论】:

    标签: xaml uwp uwp-xaml


    【解决方案1】:

    如何将点击事件添加到此项目,因为它似乎无法从 XAML 访问?

    设置按钮单击事件已由NavigationView.ItemInvoked Event 处理。这在文档NavigationView 中提到“当用户点击导航项时,导航视图会显示该项目已被选中并引发 ItemInvoked 事件。”

    所以你只需要处理 NavigationView.ItemInvoked 事件并检查NavigationViewItemInvokedEventArgs.IsSettingsInvoked Property

    下面是你可以参考的代码:

     <Grid>
        <NavigationView ItemInvoked="NavigationView_ItemInvoked">
            <NavigationView.PaneFooter>
            </NavigationView.PaneFooter>
        </NavigationView>
    </Grid>
    

    在后面的代码中:

     private void NavigationView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
        {
            if (args.IsSettingsInvoked == true) 
            {
               //do something
    
            }
        }
    

    更新

    如果要检查项目是否为特定项目,可以在 Xaml 中为导航视图项目设置标签属性,然后在 ItemInvoked 事件中检查此属性。

    在 xaml 中:

     <NavigationViewItem Tag="home" Icon="Home" Content="Home"/>
    

    在后面的代码中:

      private void NavigationView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
        {
            var navItemTag = args.InvokedItemContainer.Tag.ToString();
            if (navItemTag.Equals("home")) 
            {
                // do something
                Debug.WriteLine("test");
            }
        }
    

    【讨论】:

    • 如何使用if 语句来检查单击了哪个特定项目?例如如果点击的项目包含字符串资源“项目A”,做一些事情。
    • @MacaronLover,当然可以,我已经更新了我的答案,你可以检查一下。
    • @MacaronLover 您还有其他问题吗?
    • 你们听说过 MVVM 吗?我们应该如何从 View Model 访问这个属性???
    猜你喜欢
    • 2018-12-16
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    相关资源
    最近更新 更多