XAML 如何在应用程序标题栏中放置菜单栏?在UWP中

btxsgosb  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(189)

我在Visual Studio中做了一个UWP项目。我在MainPage上有一个MenuBar。xaml文件。

<muxc:MenuBar>
   <muxc:MenuBarItem Title="Item1">
      <MenuFlyoutItem Text="Flyout Item1"/>
   </muxc:MenuBarItem>
</muxc:MenuBar>

我想把MenuBar放在应用程序的标题栏中。我该如何用C#来实现呢?

vsmadaxz

vsmadaxz1#

根据docs窗口标题栏定制
必须调用SetTitleBar来定义一个区域作为可拖动的标题栏区域。
使用ExtendViewIntoTitleBar = true隐藏默认的标题栏,您的MenuBar将显示在标题栏的顶部。

主页.xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="48"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid x:Name="AppTitleBar" Background="Transparent">
        <!-- Width of the padding columns is set in LayoutMetricsChanged handler. -->
        <!-- Using padding columns instead of Margin ensures that the background
    paints the area under the caption control buttons (for transparent buttons). -->
        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="LeftPaddingColumn" Width="0"/>
            <ColumnDefinition/>
            <ColumnDefinition x:Name="RightPaddingColumn" Width="0"/>
        </Grid.ColumnDefinitions>

    </Grid>

    <!-- This control has a higher z-order than AppTitleBar, 
so it receives user input. -->
    <MenuBar HorizontalAlignment="Left">
        <MenuBarItem Title="Item1">
            <MenuFlyoutItem Text="Flyout Item1"/>
        </MenuBarItem>
    </MenuBar>

    <AutoSuggestBox QueryIcon="Find"
        PlaceholderText="Search"
        HorizontalAlignment="Center"
        Width="260" Height="32"/>
</Grid>

主页.xaml.cs

public MainPage()
{
    this.InitializeComponent();

    // Hide default title bar.
    CoreApplicationViewTitleBar coreTitleBar =
        CoreApplication.GetCurrentView().TitleBar;
    coreTitleBar.ExtendViewIntoTitleBar = true;

    // Set caption buttons background to transparent.
    ApplicationViewTitleBar titleBar =
        ApplicationView.GetForCurrentView().TitleBar;
    titleBar.ButtonBackgroundColor = Colors.Transparent;

    // Set XAML element as a drag region.
    Window.Current.SetTitleBar(AppTitleBar);

}

相关问题