首页 > 解决方案 > 我希望除 UWP 中的“LoginPage”之外的所有页面都可以访问“SplitViewRoot”,如何使其工作?

问题描述

我是一个从事 uwp 项目的初学者,我创建了一个类似这样的“SplitViewRootControl”:

    <Grid>
    <Image Source="ms-appx:///Assets/Background.jpg" Stretch="Fill"/>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <SplitView x:Name="SplitViewRootControl" Grid.Row="1" DisplayMode="CompactInline"  OpenPaneLength="150" CompactPaneLength="0" PaneBackground="Black" Opacity=".8">
            <SplitView.Pane>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <RelativePanel>
                        <Image x:Name="userIcon" Source="ms-appx:///Assets/user.png" Width="35" RelativePanel.AlignLeftWithPanel="True" Margin="10,20,0,0"/>
                        <TextBlock x:Name="user" RelativePanel.RightOf="userIcon" RelativePanel.AlignVerticalCenterWith="userIcon" RelativePanel.AlignTopWith="userIcon" Text="User:" FontSize="13" Margin="5,18,0,0" Foreground="Ivory" />
                        <TextBlock x:Name="userName" RelativePanel.RightOf="userIcon" RelativePanel.Below="user" Text="Chen Yinjue" FontSize="13" Margin="5,0,0,0" Foreground="Ivory" />
                    </RelativePanel>

                    <GridView Grid.Row="1" HorizontalAlignment="Center" Margin="0,50,0,0" ScrollViewer.VerticalScrollBarVisibility="Auto">
                        <StackPanel>
                            <Image Source="ms-appx:///Assets/messenger.png" Width="70" />
                            <TextBlock Text="Messenger" FontSize="18" Foreground="Ivory" HorizontalAlignment="Center" Margin="0,0,0,50"/>
                            <Image Source="ms-appx:///Assets/warning.png" Width="70" />
                            <TextBlock Text="Alert" FontSize="18" Foreground="Ivory" HorizontalAlignment="Center" Margin="0,0,0,50"/>
                            <Image Source="ms-appx:///Assets/cctv.png" Width="70" />
                            <TextBlock Text="Camera Setup" FontSize="18" Foreground="Ivory" HorizontalAlignment="Center" Margin="0,0,0,50"/>
                            <Image Source="ms-appx:///Assets/more.png" Width="70" />
                            <TextBlock Text="More" FontSize="18" Foreground="Ivory" HorizontalAlignment="Center" />
                        </StackPanel>
                    </GridView>

                    <Button Grid.Row="2" 
                            Style="{StaticResource ResourceKey=appButtonStyle}"
                            HorizontalAlignment="Center"
                            Content="Log Out" 
                            FontSize="20" 
                            Foreground="White"
                            Background="#dd514c"/>
                </Grid>
            </SplitView.Pane>

            <SplitView.Content>
                <Grid>
                    <Frame x:Name="rootFrame"/>
                    <Button Name="HamburgerBtn"
                            Style="{StaticResource ResourceKey=appButtonStyle}"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Left"
                            Background="Black"
                            Opacity=".4"
                            Click="HamburgerBtn_Click">
                        <Image Source="ms-appx:///Assets/paneBtn.png" Height="300" Width="20"/>
                    </Button>
                </Grid>
            </SplitView.Content>
        </SplitView>
    </Grid>
</Grid>

我设法通过覆盖 app.xaml.cs 中的“OnLaunched”,使应用程序中的所有页面都可以访问这个“splitview”:

    var rootControl = Window.Current.Content as SplitViewRoot;

        if (rootControl == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootControl = new SplitViewRoot();

            rootControl.RootFrame.NavigationFailed += OnNavigationFailed;

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application
            }

            // Place the frame in the current Window
            Window.Current.Content = rootControl;
        }

        if (rootControl.RootFrame.Content == null)
        {
            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter
            rootControl.RootFrame.Navigate(typeof(LoginPage), e.Arguments);
        }
        Window.Current.Activate();

问题是应用程序中有一个“登录页面”,它的页面中不应该有这个“拆分视图”。所以基本上我希望我的所有页面都可以访问“splitview”,但“LoginPage”除外。有人知道该怎么做吗?任何建议表示赞赏!

标签: c#uwp

解决方案


推荐阅读