3

我的第一个问题,到此为止。

我似乎无法插入代码块。(我对 UWP 很陌生)

我有一个带有 CommandBar 和 Frame 的 WelcomePage =MyFrame
我在CommandBar.

<Frame Grid.Column="1" x:Name="MyFrame" Margin="30,50,30,30">

MyFrame 有一个带有按钮的堆栈面板,作为内容开始导航到其他其他页面并在内部打开它们MyFrame

我只希望后退和前进按钮在我的框架内前后导航。

而不是正常的Frame.GoBack()我尝试过MyFrame.GoBack() 它识别MyFrame但它没有做任何事情......

我在 UWP Hamburger 教程中看到了这个例子,但我只想要一个命令栏
而不是汉堡包

我用谷歌搜索了很多,但如果我包括Frame它只是指向基本的Frame.GoBack(). (也许我只需要学习如何谷歌..)

4

2 回答 2

0

真的可以回去吗???

尝试导航到一个或多个页面,然后尝试返回。这是一个测试代码。MyFrameGoBackTest()在welcomepage 构造函数上调用。

    public async void MyFrameGoBackTest()
    {

        MyFrame.Navigate(typeof(SomePage));
        await Task.Delay(5000);//5 sec
        MyFrame.Navigate(typeof(SomePage));
        await Task.Delay(5000);//5 sec
        //add a breakpoint and check the CanGoBack
        if (MyFrame.CanGoBack)
        {
            MyFrame.GoBack();
        }
    }
于 2016-04-28T14:53:03.897 回答
0

我只希望后退和前进按钮在我的框架内前后导航。
而不是正常的 Frame.GoBack() 我试过 MyFrame.GoBack() 它识别 MyFrame 但它没有做任何事情....

GoBack() 和 GoForward() 仅在后退/前进导航历史记录中至少有一个条目时才有效。因此,首先使用您的框架导航到其他页面,然后使用 GoBack() 或 GoForward() 向后或向前移动。

根据您的描述,我为您制作了一个样品

主页.xaml

<StackPanel>
    <Frame x:Name="MyFrame" Margin="30,50,30,30" Width="500" Height="300" IsTapEnabled="True">
    </Frame>
    <CommandBar Background="Transparent" IsOpen="False">
        <AppBarButton Icon="Back" Label="Back" Click="AppBarButton_Click"/>
        <AppBarButton Icon="Home" Label="Home" Click="AppBarButton_Click"/>
        <AppBarButton Icon="Forward" Label="Forward" Click="AppBarButton_Click"/>
    </CommandBar>
</StackPanel>

MainPage.xaml.cs

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        MyFrame.Navigate(typeof(Page1));
    }

    private void AppBarButton_Click(object sender, RoutedEventArgs e)
    {
        var lable = (sender as AppBarButton).Label;
        if (lable == "Back")
        {
            if (MyFrame.CanGoBack)
            {
                MyFrame.GoBack();
            }
        }
        else if (lable == "Forward")
        {
            if (MyFrame.CanGoForward)
            {
                MyFrame.GoForward();
            }
        }
        else if (lable == "Home")
        {
            MyFrame.Navigate(typeof(Page1));
        }

    }
}

Page1.xaml

<StackPanel>
    <TextBlock Text="Welcome Page" FontSize="50" Margin="0,20"/>
    <Button x:Name="btn2" Content="Open Page Two" Click="btn2_Click"/>
</StackPanel>

Page1.xaml.cs

public sealed partial class Page1 : Page
{
    public Page1()
    {
        this.InitializeComponent();
    }

    private void btn2_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(Page2));
    }
}

Page2.xaml

<StackPanel>
    <TextBlock Text="Page Two" FontSize="50" Margin="0,20"/>
    <Button x:Name="btn2" Content="Open Page Three" Click="btn2_Click"/>
</StackPanel>

Page2.xaml.cs

public sealed partial class Page2 : Page
{
    public Page2()
    {
        this.InitializeComponent();
    }

    private void btn2_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(Page3));
    }
}

Page3.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Text="Page Three" FontSize="50"/>
</Grid>

输出如下所示:

在此处输入图像描述

于 2016-04-29T08:23:34.830 回答