Windows runtime “后退”按钮放置在AppBar中时不起作用

Windows runtime “后退”按钮放置在AppBar中时不起作用,windows-runtime,windows-8.1,Windows Runtime,Windows 8.1,我正在开发一个Windows8.1应用程序。我在项目中添加了一个基本页面,该页面会自动添加一个后退按钮: <Button x:Name="backButton" Margin="39,59,39,20" Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}" Style="{StaticResource NavigationBackButtonNorma

我正在开发一个Windows8.1应用程序。我在项目中添加了一个基本页面,该页面会自动添加一个后退按钮:

<Button x:Name="backButton"
        Margin="39,59,39,20"
        Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
        Style="{StaticResource NavigationBackButtonNormalStyle}"
        VerticalAlignment="Top" />

这个按钮很好用。但是,当我将此按钮移动到应用程序栏时,它不起作用。视图不会返回到上一页。

后一种情况下出现了什么问题?

AppBar与页面不在同一命名空间中,因此绑定到页面NavigationHelper属性的命令无法解析。AppBar与页面的任何绑定都是如此

您可以通过将AppBar的DataContext设置为page.Loaded中的页面来修复此问题

XAML


--罗布为我工作。我首先在
navigationHelper\u LoadState
中尝试了它,因为这是我从导航参数构建数据上下文的地方,但没有成功。它必须进入加载的
事件处理程序。请注意,您不必命名应用程序栏,只需使用页面属性
this.BottomAppBar
。re:命名应用程序栏,我发现有时需要命名特定控件,例如“backButton”,并显式分配数据上下文,如前所述,即
this.backButton.DataContext=this。我还没有弄清楚这件事的真相。
<Page.BottomAppBar>
    <AppBar x:Name="bottomAppBar" IsOpen="True">
        <Button x:Name="backButton"
            Margin="39,59,39,20"
            Command="{Binding NavigationHelper.GoBackCommand}"
            Style="{StaticResource NavigationBackButtonNormalStyle}"
            VerticalAlignment="Top" />
    </AppBar>
</Page.BottomAppBar>
public BasicPage1()
{
    this.InitializeComponent();
    this.navigationHelper = new NavigationHelper(this);
    this.navigationHelper.LoadState += navigationHelper_LoadState;
    this.navigationHelper.SaveState += navigationHelper_SaveState;

    this.Loaded += BasicPage1_Loaded;
}

async void BasicPage1_Loaded(object sender, RoutedEventArgs e)
{
    bottomAppBar.DataContext = this;
}