UWP Frame PageNavigation

UWP Frame PageNavigation

我在 UWP 中遇到导航系统问题。

我有 3 个不同的页面,其中包含 BackRequested 事件。 问题是框​​架无法返回到第二页。

例如:

Page 1 -> Page 2 -> Page 3 -> BACKBUTTON -> Page 1

我想访问第 2 页而不是第 1 页。

因为我想要一个导航栏,所以我决定创建一个母版页,其中包含一个框架控件。例如调用此控件 "Layer".

我可以使用此代码导航到第二页

Layer.Navigate(typeof(BlankPage2), this);

"this" 包含我需要从第二页访问图层的整个第一页。

当我要调用第3页时,代码是这样的

_FirstPage.Layer.Navigate(BlankPage2),this);

我可以使用

获取 FirstPage 实例
 protected async override void OnNavigatedTo(NavigationEventArgs e)
    {FirstPage _FirstPage = e.Parameter as FirstPage;}

我认为问题在于我的 FirstPage 实例与第二页中给定的参数不同。

我可以尝试调用 FirstPage(比如

WinForm -> (Form1) master = (Form1)Application.OpenForms["Form1"];) 

但我不知道如何获得打开的页面...

这个的标准是什么?

如何在一个页面中包含不同页面的导航栏??

我对语法感到抱歉。由于 Windows 10 支持键盘输入,它会尝试将任何 enter code hered 从英语改成德语。

*编辑

我尝试创建一个测试项目以查看它在没有 UIElement(框架)的情况下如何工作 我能够获得正确的 BackStack。但不幸的是,问题并没有解决!

   protected async override void OnNavigatedTo(NavigationEventArgs e)
    {


        try
        {
            int count = 0;
            StringBuilder sb = new StringBuilder();
            foreach (var item in this.Frame.BackStack)
            {

                count++;
                sb.AppendLine(item.SourcePageType.FullName + " " + count.ToString());


            }
            MessageDialog md = new MessageDialog(sb.ToString());
            await md.ShowAsync();
        }
        catch
        {

        }


        var currentView = SystemNavigationManager.GetForCurrentView();
        currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
        currentView.BackRequested += CurrentView_BackRequested;



    }

    private void CurrentView_BackRequested(object sender, BackRequestedEventArgs e)
    {
        if (this.Frame.CanGoBack == true) { this.Frame.GoBack();}
    }
    public BlankPage3()
    {
        this.InitializeComponent();
    }

这是我的主要代码

  Sites.ContentPage cp = new Sites.ContentPage();
    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        Classes.Transmission.Passwort.CreateTransmissionPasswort ctp = e.Parameter as Classes.Transmission.Passwort.CreateTransmissionPasswort;
        this.PW = ctp.PW;
        this.cp = ctp.CP;

        try
        {
            int count = 0;
            StringBuilder sb = new StringBuilder();
            foreach (var item in cp.Layer.BackStack)
            {

                count++;
                sb.AppendLine(item.SourcePageType.FullName + " " + count.ToString());


            }
            MessageDialog md = new MessageDialog(sb.ToString());
            await md.ShowAsync();
        }
        catch
        {

        }







        var currentView = SystemNavigationManager.GetForCurrentView();
        currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
        currentView.BackRequested += CurrentView_BackRequested1;
        if(Classes.CheckScreenState.GetFactorType() == Classes.CheckScreenState.DeviceForFactorType.Phone)
        {
            HardwareButtons.BackPressed += HardwareButtons_BackPressed;
        }
        if (Classes.CheckScreenState.GetFactorType() != Classes.CheckScreenState.DeviceForFactorType.Phone)
        {
            Window.Current.CoreWindow.PointerPressed += CoreWindow_PointerPressed;
        }

        }

    private void CoreWindow_PointerPressed(CoreWindow sender, PointerEventArgs args)
    {
        if (args.CurrentPoint.Properties.IsXButton1Pressed)
        {
            if (cp.Layer.CanGoBack == true) { cp.Layer.GoBack(); }
            var currentView = SystemNavigationManager.GetForCurrentView();
            currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;

        }
        if (args.CurrentPoint.Properties.IsXButton2Pressed)
        {
            if (cp.Layer.CanGoForward == true) { cp.Layer.GoForward(); }
            var currentView = SystemNavigationManager.GetForCurrentView();
            currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;

        }
    }

    private void CurrentView_BackRequested1(object sender, BackRequestedEventArgs e)
    {
        if (cp.Layer.CanGoBack == true) {cp.Layer.GoBack(); e.Handled = true; }
        var currentView = SystemNavigationManager.GetForCurrentView();
        currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;

    }
    private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {

        if (cp.Layer.CanGoBack == true) {cp.Layer.GoBack(); e.Handled = true; }
        var currentView = SystemNavigationManager.GetForCurrentView();
        currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;

    }

CP = ContentPage(主页)

我做了一个简单的应用程序,可以显示导航。

应用程序的主页面是(我们称之为根页面):FramePage.xaml 还有其他 3 xaml 个页面(我们称它们为子页面):Frame1.xaml、Frame2.xaml、Frame3.xaml

所以,我的根页面中有一个框架,底部有一个命令栏,带有 2 个按钮 'back' 和 'next' 用于在框架中导航。

所以当我们点击 'next' 按钮时,它会变成这样

Frame1 -> Frame2 -> Frame3

当你点击'back'按钮时,它会检查Frame是否可以返回。 如果可以,它将回到上一帧。

为了跟踪帧,我在每个帧中保留了一个显示帧名称的 TextBlock。

这是代码。

对于FramePage.xaml

XAML

<Page.BottomAppBar>
    <CommandBar>
        <AppBarButton x:Name="back" HorizontalAlignment="Left" Icon="Back" Label="back" VerticalAlignment="Top" Click="back_Click"/>
        <AppBarButton x:Name="next" HorizontalAlignment="Left" Icon="Forward" Label="forward" VerticalAlignment="Top" Click="next_Click"/>
    </CommandBar>
</Page.BottomAppBar>


<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Frame x:Name="mainFrame">

    </Frame>
</Grid>

XAML.CS

public FramePage()
    {
        this.InitializeComponent();
        mainFrame.Navigate(typeof(Frame1));
    }

    private void back_Click(object sender, RoutedEventArgs e)
    {
        if (mainFrame.CanGoBack)
            mainFrame.GoBack();
    }

    private void next_Click(object sender, RoutedEventArgs e)
    {
        Type current = mainFrame.SourcePageType;

        if (current.Name == "Frame1")
            mainFrame.Navigate(typeof(Frame2));

        if (current.Name == "Frame2")
            mainFrame.Navigate(typeof(Frame3));
    }

Frame1.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Text="Frame 1" FontSize="36"/>
</Grid>

Frame2.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Text="Frame 2" FontSize="36"/>
</Grid>

Frame3.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Text="Frame 3" FontSize="36"/>
</Grid>

希望对您有所帮助。