如何在 UWP 中的最小化按钮旁边的标题栏中添加新按钮?

Yox*_*Yox 3 c# xaml titlebar button uwp

我想在 UWP 应用程序的标题栏中添加一个新按钮,如下所示:

示例

我已经看到了一些“类似”的帖子,但答案并不明确,而且缺乏细节,我很难理解他们做了什么。

小智 9

默认情况下,UWP 无法向标题栏添加按钮。但uwp支持自定义标题栏布局。

\n\n

用于启动隐藏标题栏视图

\n\n
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;\n
Run Code Online (Sandbox Code Playgroud)\n\n

创建新的网格布局并将其附加到标题栏后

\n\n
Window.Current.SetTitleBar(UserLayout);\n
Run Code Online (Sandbox Code Playgroud)\n\n

创建TitleBar并订阅LayoutMetricsChanged用于动态创建 Margin 的事件,因为系统按钮数量不同,情况也会有所不同。

\n\n
var tBar = CoreApplication.GetCurrentView().TitleBar;\ntBar.LayoutMetricsChanged += OnTitleBarLayoutMetricsChanged;\n
Run Code Online (Sandbox Code Playgroud)\n\n

并添加功能

\n\n
public void OnTitleBarLayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)\n{\n    var bar = sender as CoreApplicationViewTitleBar;\n    RightPanel.Margin = new Thickness(0, 0, bar.SystemOverlayRightInset, 0);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

将页面框架导航到主页

\n\n
Content.Navigate(typeof(Home), null, new SuppressNavigationTransitionInfo()); // Navigate to Home page with null args and null animation\n
Run Code Online (Sandbox Code Playgroud)\n\n

结束于 app.xaml.cs 将标准导航框架设置为此页面

\n\n
if (e.PrelaunchActivated == false) {\n    if (rootFrame.Content == null) {\n        rootFrame.Navigate(typeof(AniMiru.Windows10.Views.AppCustomWindow), e.Arguments);\n    }\n    Window.Current.Activate();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

页面xaml:

\n\n
<Grid>\n        <Grid.RowDefinitions>\n            <RowDefinition Height="32"/>\n            <RowDefinition />\n        </Grid.RowDefinitions>\n        <Grid x:Name="TopBar" >\n            <Grid x:Name="UserLayout" Background="#00000000" />\n            <Grid Canvas.ZIndex="1">\n                <StackPanel x:Name="LeftPanel" Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Left">\n                    <AutoSuggestBox QueryIcon="Find" PlaceholderText="Search" Width="300" />\n                </StackPanel>\n                <StackPanel x:Name="RightPanel" Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Right">\n                    <Button Content="\xee\x84\x9d" FontFamily="Segoe MDL2 Assets" FontSize="13" VerticalAlignment="Stretch" />\n                    <Button Content="\xee\x85\xa1" FontFamily="Segoe MDL2 Assets" FontSize="13" VerticalAlignment="Stretch" />\n                </StackPanel>\n            </Grid>\n        </Grid>\n        <Grid Grid.Row="1">\n            <Frame x:Name="Content" />\n        </Grid>\n    </Grid>\n
Run Code Online (Sandbox Code Playgroud)\n\n

C#页面:

\n\n
public AppCustomWindow()\n{\n    this.InitializeComponent();\n\n    // Hide titlebar panel and add new layout to title bar\n    CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;\n    Window.Current.SetTitleBar(UserLayout);\n\n    // Add LayoutMetricsChanged Event to TitleBar\n    var tBar = CoreApplication.GetCurrentView().TitleBar;\n    tBar.LayoutMetricsChanged += OnTitleBarLayoutMetricsChanged;\n\n    // Navigate\n    Content.Navigate(typeof(Home), null, new SuppressNavigationTransitionInfo()); // Navigate to Home page with null args and null animation\n}\n\npublic void OnTitleBarLayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)\n{\n    var bar = sender as CoreApplicationViewTitleBar;\n    RightPanel.Margin = new Thickness(0, 0, bar.SystemOverlayRightInset, 0);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

截图:

\n\n

截屏

\n\n

网址:

\n\n

标题栏定制

\n\n

布局面板

\n\n

处理和引发事件

\n\n
\n\n

对不起我的英语。

\n\n

此致。

\n

  • 只是添加一些:最小化按钮旁边有一个小的默认可拖动区域,您无法选择覆盖它。当您尝试制作自定义标题栏时请考虑这一点。 (2认同)