【问题标题】:UWP/C# Frame Navigation IssuesUWP/C# 框架导航问题
【发布时间】:2018-08-24 21:19:14
【问题描述】:

抱歉,标题很草率。 因此,我将以下处理程序链接到嵌套在 NavigationView 中的 AppbarButton 设置按钮

private void SettingsButton_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(Content_Pages.SettingsPage));
    }

它目前所做的只是导航到我预定义的设置页面。现在我遇到的问题是我上面也有这个处理导航视图选择

  private void NavView_SelectionChanged(Windows.UI.Xaml.Controls.NavigationView sender, Windows.UI.Xaml.Controls.NavigationViewSelectionChangedEventArgs args)
        {
            NavigationViewItem item = args.SelectedItem as NavigationViewItem;

            switch (item.Tag.ToString())
            {
                case "OverView_Page":
                    ContentFrame.Navigate(typeof(Content_Pages.Overview_Page));
                    break;
             }
        }

这里的明显区别是 ContentFrame 部分。它允许所需页面显示为导航视图的一部分。

我完成 SettingsButton_Click 的方式意味着当单击按钮时我会松开导航菜单,因为它调用 Frame.Navigate 而不是 Content.Navigate。

我尝试调整 SettingsButton_Click 以使用 ContentFrame.Navigate 无济于事,因为它实际上是嵌套在 NavigationView 中的 AppBarButton。

<AppBarButton Icon="Setting" Margin="1, 2, 0, 0" Tag="Settings_Page" HorizontalAlignment="Center" Width="56.5" Height="56.5" ClickMode="Press" Click="SettingsButton_Click"/>

我不确定如何将其更改为 ContentFrame.Navigate 而不是 Frame.Navigate。你能帮忙看看我摔倒在哪里吗?

编辑:

根据要求,MainPage.XAML

<Page
    x:Class="BudgetSheet.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:Microsoft.UI.Xaml.Controls"
    xmlns:local="using:BudgetSheet"
    xmlns:mux="using:Windows.UI.Xaml.Controls"
    xmlns:muxcontrols="using:Microsoft.UI.Xaml.Controls"
    RequestedTheme="Dark">

    <Page.Resources>

    </Page.Resources>
    <Grid>
        <Grid.Resources>
            <ResourceDictionary>
                <ResourceDictionary.ThemeDictionaries>
                    <ResourceDictionary x:Key="Dark">
                        <SolidColorBrush x:Key="DarkBrush" Color="{StaticResource SystemBaseHighColor}"/>
                        <AcrylicBrush x:Key="NavigationViewDefaultPaneBackground"
                          BackgroundSource="Backdrop"
                          TintColor="#262626"
                          TintOpacity="0.8"
                          FallbackColor="#262626"/>
                    </ResourceDictionary>
                </ResourceDictionary.ThemeDictionaries>
            </ResourceDictionary>
        </Grid.Resources>

        <mux:NavigationView IsSettingsVisible="False" 
                            PaneTitle=" Budget Sheet Menu "                            
                            x:Name="NavView"                             
                            IsBackButtonVisible="Collapsed" 
                            PaneDisplayMode="LeftMinimal" 
                            AlwaysShowHeader="True"        
                            SelectionChanged="NavView_SelectionChanged"
                            Background="{StaticResource CustomAcrylicDarkBackground}">

            <mux:NavigationView.MenuItems>
                <StackPanel Orientation="Horizontal" UseLayoutRounding="False">
                    <AppBarButton Icon="Page2" Margin="0, 2, 1, 0" Tag="New_Sheet" HorizontalAlignment="Center" Width="56.5" Height="56.5" ClickMode="Press" />
                    <AppBarButton Icon="OpenFile" Margin="1, 2, 0, 0" Tag="Open_Sheet" HorizontalAlignment="Center" Width="56.5" Height="56.5" ClickMode="Press" />
                    <AppBarButton Icon="Save" Margin="1, 2, 0, 0" Tag="Save_Sheet" HorizontalAlignment="Center" Width="56.5" Height="56.5" ClickMode="Press"/>
                    <AppBarButton Icon="Setting" Margin="1, 2, 0, 0" Tag="Settings_Page" HorizontalAlignment="Center" Width="56.5" Height="56.5" ClickMode="Press" Click="SettingsButton_Click"/>
                    <AppBarButton Icon="Calculator" Margin="1, 2, 0, 0" Tag="Calculator_Open" HorizontalAlignment="Center" Width="56.5" Height="56.5" ClickMode="Press" />
                </StackPanel>

                <mux:NavigationViewItemSeparator/>
                <mux:NavigationViewItem Name="HomeItem" 
                                        Content="HOME" 
                                        Tag="HOME_Page" 
                                        FontSize="22" 
                                        HorizontalAlignment="Stretch" 
                                        FontWeight="Bold" 
                                        Foreground="#b880fc"/>
                <NavigationViewItemSeparator/>

                <mux:NavigationViewItem Name="OverviewItem" 
                                        Content="OVERVIEW" 
                                        Tag="OverView_Page" 
                                        FontSize="22" 
                                        HorizontalAlignment="Stretch" 
                                        FontWeight="Bold" 
                                        Foreground="#b880fc"/>

                <mux:NavigationViewItem Name="BillsItem" 
                                        Content="BILLS" 
                                        Tag="Bills_Page" 
                                        FontSize="22" 
                                        HorizontalAlignment="Stretch" 
                                        FontWeight="Bold" 
                                        Foreground="#b880fc"/>

                <mux:NavigationViewItem Name="PeopleItem" 
                                        Content="PEOPLE" 
                                        Tag="BillPayer_Page" 
                                        FontSize="22" 
                                        HorizontalAlignment="Stretch" 
                                        FontWeight="Bold" 
                                        Foreground="#b880fc"/>

                <mux:NavigationViewItem Name="TransfersItem" 
                                        Content="TRANSFERS" 
                                        Tag="Transfers_Page" 
                                        FontSize="22" 
                                        HorizontalAlignment="Stretch" 
                                        FontWeight="Bold" 
                                        Foreground="#b880fc"/>

                <mux:NavigationViewItem Name="PayDatesItem" 
                                        Content="PAY DATES" 
                                        Tag="PayDates_Page" 
                                        FontSize="22" 
                                        HorizontalAlignment="Stretch" 
                                        FontWeight="Bold" 
                                        Foreground="#b880fc"/>
            </mux:NavigationView.MenuItems>

            <Frame x:Name="ContentFrame">
                <Frame.ContentTransitions>
                    <TransitionCollection>
                        <NavigationThemeTransition/>
                    </TransitionCollection>
                </Frame.ContentTransitions>
            </Frame>

            <NavigationView.PaneFooter>
                <Button x:Name="ChangeUser" Style="{StaticResource TextBlockButtonStyle}" Foreground="#b880fc" >
                    <StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal">
                        <SymbolIcon Symbol="Contact" Margin="8"/>
                        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Right">      
                                    Change User
                        </TextBlock>
                    </StackPanel>
                </Button>
            </NavigationView.PaneFooter>
        </mux:NavigationView>
      </Grid>
</Page>

【问题讨论】:

  • 添加主页 xaml :)

标签: c# uwp uwp-xaml


【解决方案1】:

如果您将属性添加到MainPage.xaml.cs

public Frame TheContentFrame
{
    get => ContentFrame;
}

..在使用 VisualTreeHelper 类获得对 MainPage 的引用后,您应该能够设置 ContentFrameContentFrameContent VisualTreeHelper 类:

private void SettingsButton_Click(object sender, RoutedEventArgs e)
{
    MainPage mainPage = FindParent<MainPage>(this);
    if (mainPage != null)
        mainPage.TheContentFrame.Navigate(typeof(Content_Pages.SettingsPage));
}

private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(dependencyObject);

    if (parent == null) return null;

    var parentT = parent as T;
    return parentT ?? FindParent<T>(parent);
}

【讨论】:

  • 谢谢,我试试看!
  • 不幸的是不起作用。它不会引发任何错误,但在按下按钮时也不会导航。我会逐步检查代码,看看我是否能弄清楚。感谢您的帮助:)
  • 您是否获得了对 MainPage 的引用? SettingsButton_Click 是否在 NavigationView.xaml.cs 中定义?
  • 它似乎在 FindParent 上循环并返回 Null。 SettingsButton_Click 是在 NavigationView 中定义的 :) 参考 MainPage 是什么意思,对不起。我对此很陌生
  • 如果 (mainPage != null) 进入行,然后完全跳过下一行。它根本不执行。
【解决方案2】:

所以,我设法通过以下方式进行导航:

 private void SettingsButton_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(typeof(Content_Pages.SettingsPage));
        }      

现在,导航成功。它只是不会在导航后最小化导航视图。我要为此制定解决方案并从那里开始

感谢mm8到目前为止所做的一切

基本上我最初尝试做的事情已经完成了一个完整的循环,现在它可以工作了:(

编辑:

使用以下解决了最小化导航视图:

 private void SettingsButton_Click(object sender, RoutedEventArgs e)
        {        
            ContentFrame.Navigate(typeof(Content_Pages.SettingsPage));
            NavView.IsPaneOpen = false;
       }

【讨论】:

  • 所以事件处理程序与 ContentFrame 在同一个视图中定义...?那为什么不直接调用 ContentFrame.Navigate 呢?
  • 我最初尝试过,但它根本没有用:/我现在把它扔进去了,它工作正常。我现在觉得自己好傻。我不知道为什么它现在工作。我仍然对 NavView 有问题,在选择后没有最小化,但我会为此写一个新帖子。我会保留你的答案,尽管我知道我稍后会需要它
  • 真的很抱歉 :(
猜你喜欢
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多