【问题标题】:Navigation View doesn't open different forms导航视图不会打开不同的表单
【发布时间】:2019-07-22 10:53:50
【问题描述】:

我正在尝试在我的 UWP 应用中实现 NavigationView。我按照 Microsoft 教程进行操作。

这是我遵循的教程:https://blogs.msdn.microsoft.com/appconsult/2018/05/06/using-the-navigationview-in-your-uwp-applications/

我检查了每个视图页面中的文本块与 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 页面之间导航。但是,当我按下导航视图中的每个按钮时,所选项目会突出显示,但没有任何反应。

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

    NavigationViewItemInvokedEventArgs 中的InvokeItem 指的是NavigationViewItemContent 属性。

    对于不起作用的ItemContent,原因是他没有将TextBlock放入您的NavigationViewItem。这就是为什么它总是为空。

    如果您需要 Tag 属性,请像这样修改 XAML:

    <NavigationView.MenuItems>
        <NavigationViewItem Icon="Edit"  x:Name="logFood_NavViewItem">
            <TextBlock Tag="logFood">Log Food</TextBlock>
        </NavigationViewItem>
        ...
    </NavigationView.MenuItems>
    

    如果您不打算修改 XAML,您可以像这样修改您的 nvTopLevelNav_ItemInvoked 函数:

    if (args.InvokedItem is string ItemContent)
    {
        switch (ItemContent)
        {
            case "Log Food":
                ContentFrame.Navigate(typeof(LogFood));
                break;
    
            case "Calculate Nutrition Goals":
                ContentFrame.Navigate(typeof(MyNewPage));
                break;
    
            case "Set Nutrition Goals":
                ContentFrame.Navigate(typeof(SetGoals));
                break;
        }
    }    
    

    最好的问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      • 2014-08-05
      • 2016-04-24
      相关资源
      最近更新 更多