UWP Frame PageNavigation

Mic*_*ski 1 c# navigation controls frame uwp

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

我有3个不同的页面,其中包含BackRequested事件.问题是Frame无法返回第2页.

例如:

Page 1 -> Page 2 -> Page 3 -> BACKBUTTON -> Page 1
Run Code Online (Sandbox Code Playgroud)

我想访问第2页而不是第一页.

由于我想要一个导航栏,我决定创建一个包含内部Frame Control的Master页面.该控件被称为例如"层".

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

Layer.Navigate(typeof(BlankPage2), this);
Run Code Online (Sandbox Code Playgroud)

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

当我想打电话给第3页时,代码就是这样的

_FirstPage.Layer.Navigate(BlankPage2),this);
Run Code Online (Sandbox Code Playgroud)

我可以使用获取FirstPage实例

 protected async override void OnNavigatedTo(NavigationEventArgs e)
    {FirstPage _FirstPage = e.Parameter as FirstPage;}
Run Code Online (Sandbox Code Playgroud)

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

我可以尝试调用FirstPage(比如

WinForm -> (Form1) master = (Form1)Application.OpenForms["Form1"];) 
Run Code Online (Sandbox Code Playgroud)

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

这个标准是什么?

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

我很抱歉语法.由于Windows 10支持键盘输入,它会尝试纠正enter code here从英语到德语的任何工作.

*编辑

我试图创建一个测试项目,看看它是如何工作的,没有UIElement(Frame)我能够得到正确的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();
    }
Run Code Online (Sandbox Code Playgroud)

这是我的主要代码

  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;

    }
Run Code Online (Sandbox Code Playgroud)

CP = ContentPage(MainPage)

Rau*_*tel 6

我做了一个简单的应用程序,向您显示导航.

屏幕截图

应用程序的MainPage是(让我们调用这个rootpage):FramePage.xaml还有其他3个xaml页面(让我们称之为子页面):Frame1.xaml,Frame2.xaml,Frame3.xaml

所以,我的rootpage中有一个框架,底部有一个Command Bar,有两个按钮'back'和'next'来浏览框架.

因此,当我们点击"下一步"按钮时,它会像这样

Frame1 - > Frame2 - > Frame3

当您单击"后退"按钮时,它将检查Frame是否可以返回.如果可以,它将返回到前一帧.

为了跟踪Frame,我在每个帧中保留了一个TextBlock,显示了Frame Name.

这是代码.

对于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>
Run Code Online (Sandbox Code Playgroud)

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));
    }
Run Code Online (Sandbox Code Playgroud)

Frame1.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Text="Frame 1" FontSize="36"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

Frame2.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Text="Frame 2" FontSize="36"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

Frame3.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Text="Frame 3" FontSize="36"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.