如何自定义 XAML 导航视图窗格的外观

问题描述 投票:0回答:2

我有一个带有“Top”PaneDisplayMode 的 XAML Navigation View 控件。

我想增加窗格高度并居中对齐菜单项。

增加 NavigationViewItem 高度不会使窗格更高。更改对齐设置不会影响窗格中的菜单项定位。

我该怎么做?

<Page ... xmlns:muxc="using:Microsoft.UI.Xaml.Controls" ... >
<Grid>
    <muxc:NavigationView x:Name="NavView"
                         Loaded="NavView_Loaded"
                         ItemInvoked="NavView_ItemInvoked"
                         BackRequested="NavView_BackRequested"
                         PaneDisplayMode="Top">

        <muxc:NavigationView.MenuItems>
            <muxc:NavigationViewItem Tag="home" Icon="Home" Content="Home"/>
            <muxc:NavigationViewItemSeparator/>
            <muxc:NavigationViewItemHeader x:Name="MainPagesHeader"
                                           Content="Main pages"/>
            <muxc:NavigationViewItem Tag="apps" Content="Apps">
                <muxc:NavigationViewItem.Icon>
                    <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xEB3C;"/>
                </muxc:NavigationViewItem.Icon>
            </muxc:NavigationViewItem>
            <muxc:NavigationViewItem Tag="games" Content="Games">
                <muxc:NavigationViewItem.Icon>
                    <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE7FC;"/>
                </muxc:NavigationViewItem.Icon>
            </muxc:NavigationViewItem>
            <muxc:NavigationViewItem Tag="music" Icon="Audio" Content="Music"/>
        </muxc:NavigationView.MenuItems>

    

        <ScrollViewer>
            <Frame x:Name="ContentFrame" Padding="12,0,12,24" IsTabStop="True"
                   NavigationFailed="ContentFrame_NavigationFailed"/>
        </ScrollViewer>
    </muxc:NavigationView>


</Grid>
</Page>
c# xaml uwp
2个回答
0
投票

找不到答案,所以通过删除导航视图并自己实现类似的东西来解决它,在这里分享以防其他人处于同一个泡菜中。所以这更像是一种解决方法而不是答案。如果有人提供更好的答案,我会接受它。

在 NavigationRoot.xaml 中,我有一个网格,其中包含用于“窗格”的堆栈面板和用于页面的框架。请注意,按钮的标签与我在导航中使用的页面标签相匹配(不要判断)。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="4*"/>
    </Grid.RowDefinitions>

    <StackPanel x:Name="Pane" Orientation="Horizontal" HorizontalAlignment="Center" Margin="10">
        <ToggleButton  x:Name="HomeButton" Content="Home"  Tag="home" Click="Button_Click" Width="100" Margin="10"/>
        <ToggleButton  x:Name="SettingsButton" Content="Settings" Tag="settings" Click="Button_Click" Width="100" Margin="10"/>
    </StackPanel>
    <Frame Grid.Row="1" x:Name="RootContentFrame"/>
</Grid>

在后面的代码中,我跟踪“CurrentPageTag”并且我有一个页面列表。

     private string CurrentPageTag;
    
    // List of ValueTuple holding the Navigation Tag and the relative Navigation Page
    private readonly List<(string Tag, Type Page)> _pages = new List<(string Tag, Type Page)>
    {
        ("home", typeof(MainPage)),
        ("settings", typeof(SettingsPage))
    };

在切换按钮的button_click事件中,我得到了按钮标签并导航到该页面:

 string TargetPageTag = (sender as ToggleButton).Tag.ToString();
 //cancel navigation of current and target are the same
 if (TargetPageTag == CurrentPageTag)
 {
     return;
 }
 else
 {
       /*
       you can check that page can be navigated away from here (form 
       validation and the like) and exit early if validation fails. 
       I actually use a global "cancel navigation" variable 
       that pages can set if they are in an incomplete state 
       and should not be navigated away from.
       */
 
   //navigate to target page
   Type targetPage = _pages.FirstOrDefault(p => p.Tag.Equals(targetPageTag)).Page;
   RootContentFrame.Navigate(targetPage);
   CurrentPageTag = targetPageTag;
   return;
 }

这为您提供了基本的“导航视图”功能,实际上堆代码更少(但无法即时更改窗格布局)。

为简洁起见,我在这里省略的是我必须设置切换按钮的样式以更适合此目的。此外,我让它们更像单选按钮,一次只能选中一个。也许单选按钮应该使用更多的样式(但我讨厌样式所以走这条路)?


0
投票

您可以通过VisualTreeHelper类获取子网格来自定义NavigationView的外观。

private void NavigationView_Loaded(object sender, RoutedEventArgs e)

{ // 使用 muxc = Microsoft.UI.Xaml.Controls;

var navView = sender as muxc.NavigationView;
var rootGrid = VisualTreeHelper.GetChild(navView, 0) as Grid;
// From the root grid, you can get child elements to customize them.
// You can view the NavigationView diagram from the NavigationView link above.

// Find the top navigation pane and set properties.
var grid = VisualTreeHelper.GetChild(rootGrid, 1) as Grid;
var topNavArea = VisualTreeHelper.GetChild(grid, 0) as StackPanel;
var topNavGrid = VisualTreeHelper.GetChild(topNavArea, 1) as Grid;

topNavGrid.Height = ...;
topNavGrid.RowDefinitions = ...;

}

你必须找到导航视图子项的方式,并且你应该有一种方法来自定义导航视图项,如上所示。

© www.soinside.com 2019 - 2024. All rights reserved.