【问题标题】:Frame navigation issue on Back button in Drawer Layout Windows Phone 8.1(Universal) WinRT抽屉布局 Windows Phone 8.1(通用)WinRT 中的后退按钮上的框架导航问题
【发布时间】:2016-06-17 19:35:16
【问题描述】:

我想在我的应用程序中为汉堡包使用抽屉布局,但由于我的应用程序有 30 多个页面,在每个页面上声明抽屉模板是不可行的,因此我希望将抽屉布局作为带有子/内容的母版页页面内部,我在下面编写了带有框架标签的代码,以便在单击抽屉布局中的菜单时导航到页面。

下面是我的 XAML

<Grid x:Name="Rootlayout">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <!-- title bar-->
    <Grid x:Name="TitleBar" Height="50" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Background="SkyBlue">
        <Image  Source="/Assets/fs-logo.png" Height="50" HorizontalAlignment="Left" Margin="10,0,0,0"/>
    </Grid>
    <!--Drawer Layout-->
    <drawer:DrawerLayout x:Name="DrawerLayout" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
        <Grid>
            <Frame x:Name="contentFrame" />
        </Grid>
        <Grid x:Name="listFragment" FlowDirection="LeftToRight" >
            <ListView x:Name="listItem" SelectionChanged="listItem_SelectionChanged">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" FontSize="20" Foreground="Black" Margin="10" VerticalAlignment="Center" HorizontalAlignment="Left"/>      
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </drawer:DrawerLayout>
</Grid>

虽然行为完全符合我的要求,但硬件后退按钮存在问题,因为按下它时应用程序会退出。即使我使用以下代码处理后退按钮按下事件也是 app.xaml.cs

//Below code of line in app constructor & it's method defination
#if WINDOWS_PHONE_APP
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
#endif

#if WINDOWS_PHONE_APP    
void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (frame != null && frame.CanGoBack)
        {
            frame.GoBack();
            e.Handled = true;
        }
}
#endif

谁能帮我解决这个后退按钮问题,或者有没有其他方法可以利用抽屉布局,我不必在每个页面上编写抽屉布局 xaml 代码。一种方法是使用用户控件,但由于内容网格是抽屉布局的一部分,我无法弄清楚如何组合我的用户控件和我的普通页面 xaml。请提前帮助thanx

【问题讨论】:

    标签: c# windows-runtime windows-phone-8.1 win-universal-app winrt-xaml


    【解决方案1】:

    您在错误的帧上检查CanGoBack

    Window.Current.Content 是包含 DrawerLayout 的基本框架。你真正想要解决的是它里面的contentFrame

    所以你真正想看的是:

    (Window.Current.Content as RootPage)?.Frame.CanGoBack
    

    RootPage 是您在问题中发布的 Xaml 的主页名称。

    【讨论】:

    • 谢谢我会尝试同样的。还想知道这是使用drawerlayout或制作drawerlayout用户控件的正确方法吗?如果是,我该怎么做。
    • 对我来说看起来不错。取决于您的项目和要求。
    • 这不起作用,因为页面没有 CanGoBack 和 GoBack()
    • 啊,错过了 *.Frame" ^^
    猜你喜欢
    • 1970-01-01
    • 2020-12-20
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多