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

发布于 2025-01-11 06:19:29 字数 4864 浏览 0 评论 0原文

是否可以从 NavigationView 中的代码隐藏进行导航? 如果我是 NavigationView 框架中的一页,并且我想离开该页面并显示另一个有效页面,这可能吗?我的项目不是MVVM结构。我很感激你的帮助。

这是我的 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>

这是我用来通过 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;
            }
        }

我在“Metric Staging”页面上有一个按钮,如果单击该按钮,我希望它在 MasterContentFrame 中打开“Data Collection”页面。

以下是按钮事件代码,当单击“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;
}

is it possible to navigate from code-behind in a 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. My project is not in MVVM structure. 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:

        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.

Here is the button event code, that is triggered when the button is clicked on the 'Metric Staging' page:

private void ButtonListViewEdit_Click(object sender, RoutedEventArgs e)
{
    Debug.WriteLine(
quot;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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠佳期 2025-01-18 06:19:29

您可以将窗口中 Frame 元素的 x:FieldModifier 属性设置为 internalpublic 以便能够从antother类访问Frame

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

还要更改App.xaml.csm_window字段的修饰符:

internal Window m_window;

然后您可以访问< code>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)));
    }
}

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:

<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:

internal Window m_window;

You can then access the Frame from the event handler in the Page like this:

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)));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文