C# 为页面上的一个框架返回全局命令栏
我的第一个问题是 我似乎无法插入代码块。 (我是UWP的新手) 我有一个带有命令栏和框架的WelcomePage=C# 为页面上的一个框架返回全局命令栏,c#,win-universal-app,frame,uwp-xaml,C#,Win Universal App,Frame,Uwp Xaml,我的第一个问题是 我似乎无法插入代码块。 (我是UWP的新手) 我有一个带有命令栏和框架的WelcomePage=MyFrame 我在命令栏中有一个后退、前进和主页按钮 <Frame Grid.Column="1" x:Name="MyFrame" Margin="30,50,30,30"> MyFrame有一个stackpanel,其中的按钮是导航到其他页面并在MyFrame 我只希望“后退和前进”按钮在帧内来回导航 我尝试了MyFrame.GoBack() 它可以识别MyF
MyFrame
我在
命令栏中有一个后退、前进和主页按钮
<Frame Grid.Column="1" x:Name="MyFrame" Margin="30,50,30,30">
MyFrame有一个stackpanel,其中的按钮是导航到其他页面并在MyFrame
我只希望“后退和前进”按钮在帧内来回导航
我尝试了MyFrame.GoBack()
它可以识别MyFrame,但它什么都不做。
我在UWP汉堡包教程中看到了这个例子,但我只想要一个commandbar
而不是汉堡包
我在谷歌上搜索了很多,但如果我把Frame
包括在内,它只指向基本的Frame.GoBack()
。
(也许我只需要学习如何使用谷歌…)你真的能回去吗
尝试导航到一个或多个页面,然后尝试返回。下面是一个测试代码。调用welcomepage构造函数上的MyFrameGoBackTest()
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();
}
}
我只希望“后退和前进”按钮在帧内来回导航。
我尝试了MyFrame.GoBack()而不是普通的Frame.GoBack(),它可以识别MyFrame,但它什么都不做
GoBack()和GoForward()仅在后退/前进导航历史记录中至少有一个条目时有效。因此,首先使用框架导航到其他页面,然后使用GoBack()或GoForward()向后或向前移动
根据您的描述,我为您制作了一个样本:
MainPage.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>
<StackPanel>
<TextBlock Text="Welcome Page" FontSize="50" Margin="0,20"/>
<Button x:Name="btn2" Content="Open Page Two" Click="btn2_Click"/>
</StackPanel>
<StackPanel>
<TextBlock Text="Page Two" FontSize="50" Margin="0,20"/>
<Button x:Name="btn2" Content="Open Page Three" Click="btn2_Click"/>
</StackPanel>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="Page Three" FontSize="50"/>
</Grid>
Page1.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>
<StackPanel>
<TextBlock Text="Welcome Page" FontSize="50" Margin="0,20"/>
<Button x:Name="btn2" Content="Open Page Two" Click="btn2_Click"/>
</StackPanel>
<StackPanel>
<TextBlock Text="Page Two" FontSize="50" Margin="0,20"/>
<Button x:Name="btn2" Content="Open Page Three" Click="btn2_Click"/>
</StackPanel>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="Page Three" FontSize="50"/>
</Grid>
Page2.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>
<StackPanel>
<TextBlock Text="Welcome Page" FontSize="50" Margin="0,20"/>
<Button x:Name="btn2" Content="Open Page Two" Click="btn2_Click"/>
</StackPanel>
<StackPanel>
<TextBlock Text="Page Two" FontSize="50" Margin="0,20"/>
<Button x:Name="btn2" Content="Open Page Three" Click="btn2_Click"/>
</StackPanel>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="Page Three" FontSize="50"/>
</Grid>
Page3.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>
<StackPanel>
<TextBlock Text="Welcome Page" FontSize="50" Margin="0,20"/>
<Button x:Name="btn2" Content="Open Page Two" Click="btn2_Click"/>
</StackPanel>
<StackPanel>
<TextBlock Text="Page Two" FontSize="50" Margin="0,20"/>
<Button x:Name="btn2" Content="Open Page Three" Click="btn2_Click"/>
</StackPanel>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="Page Three" FontSize="50"/>
</Grid>
而输出如下所示: