简体   繁体   English

使用 NavigationView 控件,如何从后面的代码导航?

[英]With a NavigationView control, how can I navigate from the code behind?

is it possible to navigate from code-behind in a NavigationView?是否可以从 NavigationView 中的代码隐藏导航? If I am one one page in the NavigationView's frame, and I want to leave that page and display another valid page, is this possible.如果我是 NavigationView 框架中的一页,并且我想离开该页面并显示另一个有效页面,这是否可能。 My project is not in MVVM structure.我的项目不是 MVVM 结构。 I appreciate your help.我感谢您的帮助。

Here is my NavigationView:这是我的导航视图:

<NavigationView x:Name="MasterNavigation"
                Header="Main"
                ItemInvoked="MasterNavigation_ItemInvoked" 
                PaneTitle="Menu"
                IsBackButtonVisible="Collapsed"
                IsSettingsVisible="False"
                PaneDisplayMode="Auto"
                OpenPaneLength="200"
                IsTabStop="True">
    
    <NavigationView.MenuItems>

        <NavigationViewItem Icon="Home"       Content="Home"                 Tag="home" IsSelected="True"/>
        <NavigationViewItem Icon="Globe"      Content="Data Collection"      Tag="datacollection"/>
        <NavigationViewItem Icon="Globe"      Content="Data Collection (v2)" Tag="collectdata"/>
        <NavigationViewItem Icon="Document"   Content="Goals"                Tag="goals"/>
        <NavigationViewItem Icon="Manage"     Content="Approvals"            Tag="approvals"/>
        <NavigationViewItem Icon="Print"      Content="Reports"              Tag="reports"/>
        <NavigationViewItem Icon="Admin"      Content="Admin"                Tag="admin"/>
        <NavigationViewItem Icon="PostUpdate" Content="Metric Staging"       Tag="metricstaging"/>
        <NavigationViewItem Icon="PostUpdate" Content="Ref Metric Main"      Tag="refmetricmain"/>
        <!--<NavigationViewItem Icon="Calculator" Content="Dashboard" Tag="dashboard" />-->

    </NavigationView.MenuItems>
    
    <Frame x:Name="MasterContentFrame" Margin="0,0,0,0"/>

</NavigationView>

This is the code I used to navigate via the UI:这是我用来通过 UI 导航的代码:

        private void MasterNavigation_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
        {
            if (args.IsSettingsInvoked)
            {
                MasterContentFrame.Navigate(typeof(pageSettings));
            }
            else
            {
                // find NavigationViewItem with Content that equals InvokedItem
                NavigationViewItem item = sender.MenuItems.OfType<NavigationViewItem>().First(x => (string)x.Content == (string)args.InvokedItem);
                NavView_Navigate(item);
            }
        }

        private void NavView_Navigate(NavigationViewItem item)
        {
            switch (item.Tag)
            {
                case "home":
                    MasterContentFrame.Navigate(typeof(pageHome));
                    MasterNavigation.Header = item.Content;
                    break;

                case "datacollection":
                    MasterContentFrame.Navigate(typeof(pageDataCollection));
                    MasterNavigation.Header = item.Content;
                    break;

                case "collectdata":
                    MasterContentFrame.Navigate(typeof(PageCollectMetricData));
                    MasterNavigation.Header = item.Content;
                    break;

                case "goals":
                    MasterContentFrame.Navigate(typeof(pageGoals));
                    MasterNavigation.Header = item.Content;
                    break;

                case "approvals":
                    MasterContentFrame.Navigate(typeof(pageComments));
                    MasterNavigation.Header = item.Content;
                    break;

                case "reports":
                    MasterContentFrame.Navigate(typeof(pageReports));
                    MasterNavigation.Header = item.Content;
                    break;

                case "admin":
                    MasterContentFrame.Navigate(typeof(pageAdmin));
                    MasterNavigation.Header = item.Content;
                    break;

                case "metricstaging":
                    MasterContentFrame.Navigate(typeof(pageMetricStaging));
                    MasterNavigation.Header = item.Content;
                    break;

                case "refmetricmain":
                    MasterContentFrame.Navigate(typeof(pageRefMetricMain));
                    MasterNavigation.Header = item.Content;
                    break;
            }
        }

I have a button on the 'Metric Staging' page, where if clicked I would like it to open the 'Data Collection' page in the MasterContentFrame.我在“Metric Staging”页面上有一个按钮,如果单击该按钮,我希望它在 MasterContentFrame 中打开“Data Collection”页面。

Here is the button event code, that is triggered when the button is clicked on the 'Metric Staging' page:这是按钮事件代码,当在“Metric Staging”页面上单击按钮时触发:

private void ButtonListViewEdit_Click(object sender, RoutedEventArgs e)
{
    Debug.WriteLine($"PAGE METRIC STAGING: BUTTON WAS CLICKED");
    // Below are my attempts to implement 'code-behind' navigation 
    //MetricReporting.MainWindow MasterContentFrame.Navigate(typeof(pageDataCollection));
    //MasterNavigation.Header = "Data Collection";
    //var navigation = (Application.Current as App).MasterNavigation;
}

You could set the x:FieldModifier attribute of the Frame element in the window to internal or public to be able to access the Frame from antother class:您可以将 window 中Frame元素的x:FieldModifier属性设置为internalpublic ,以便能够从另一个 class 访问该Frame

<Frame x:Name="MasterContentFrame" x:FieldModifier="internal" Margin="0,0,0,0"/>

Also change the modifier for the m_window field in App.xaml.cs :同时更改 App.xaml.cs 中m_window字段的App.xaml.cs

internal Window m_window;

You can then access the Frame from the event handler in the Page like this:然后,您可以从Page中的事件处理程序访问Frame ,如下所示:

private void ButtonListViewEdit_Click(object sender, RoutedEventArgs e)
{
    MainWindow window = (App.Current as App)?.m_window as MainWindow;
    if (window != null)
    {
        window.MasterContentFrame.Navigate(typeof(typeof(pageDataCollection)));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以从(ResourceDictionary)后面的代码访问命名控件吗? - Can I get access from code behind (of a ResourceDictionary) to a named control? 如何在代码中包装我的用户控件? - How can I wrap my user control in code behind? 如何从后面的代码刷新自定义WPF用户控件? - How can I refresh a custom wpf user control from code behind? 如何从 xaml 中的 ItemTemplate 访问在代码中定义的用户控件的依赖项属性? - How can I access a dependency property of a user control that was defined in code behind from a ItemTemplate in xaml? 如何使用XAML和后面的代码序列化WPF用户控件 - How can I serialize wpf user control with xaml and code behind 如何从代码后面找到span并为其添加用户控件? - How can I find span from code behind and add user control to it? 我如何使用反射从文件后面的asp.net代码访问服务器端控件? - How can i access a server side control from asp.net code behind file using reflection? 如何从后面的代码访问和设置跨度控件的属性? - How can I access and set the properties of span control from code behind? 我怎样才能从asp.net后面的代码中包装一个带有div的控件? - how can I wrap a control with a div from the code behind in asp.net? 如何导航到Windows生成的控件? - How can I navigate to a windows generated control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM