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

时间:2019-07-31 15:17:07

标签: c# xaml button uwp titlebar

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

Exemple

我已经看到了一些“类似”的帖子,但是答案并不清楚,并且缺乏细节,这让我很难理解他们的所作所为。

1 个答案:

答案 0 :(得分:1)

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

开始隐藏标题栏视图

CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;

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

Window.Current.SetTitleBar(UserLayout);

创建TitleBar并订阅LayoutMetricsChanged事件,该事件用于动态创建保证金,因为使用不同数量的系统按钮,事件将有所不同。

var tBar = CoreApplication.GetCurrentView().TitleBar;
tBar.LayoutMetricsChanged += OnTitleBarLayoutMetricsChanged;

并添加功能

public void OnTitleBarLayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
{
    var bar = sender as CoreApplicationViewTitleBar;
    RightPanel.Margin = new Thickness(0, 0, bar.SystemOverlayRightInset, 0);
}

将页面框架导航到主页

Content.Navigate(typeof(Home), null, new SuppressNavigationTransitionInfo()); // Navigate to Home page with null args and null animation

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

if (e.PrelaunchActivated == false) {
    if (rootFrame.Content == null) {
        rootFrame.Navigate(typeof(AniMiru.Windows10.Views.AppCustomWindow), e.Arguments);
    }
    Window.Current.Activate();
}

页面xaml:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="32"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid x:Name="TopBar" >
            <Grid x:Name="UserLayout" Background="#00000000" />
            <Grid Canvas.ZIndex="1">
                <StackPanel x:Name="LeftPanel" Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Left">
                    <AutoSuggestBox QueryIcon="Find" PlaceholderText="Search" Width="300" />
                </StackPanel>
                <StackPanel x:Name="RightPanel" Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Right">
                    <Button Content="" FontFamily="Segoe MDL2 Assets" FontSize="13" VerticalAlignment="Stretch" />
                    <Button Content="" FontFamily="Segoe MDL2 Assets" FontSize="13" VerticalAlignment="Stretch" />
                </StackPanel>
            </Grid>
        </Grid>
        <Grid Grid.Row="1">
            <Frame x:Name="Content" />
        </Grid>
    </Grid>

页面C#:

public AppCustomWindow()
{
    this.InitializeComponent();

    // Hide titlebar panel and add new layout to title bar
    CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
    Window.Current.SetTitleBar(UserLayout);

    // Add LayoutMetricsChanged Event to TitleBar
    var tBar = CoreApplication.GetCurrentView().TitleBar;
    tBar.LayoutMetricsChanged += OnTitleBarLayoutMetricsChanged;

    // Navigate
    Content.Navigate(typeof(Home), null, new SuppressNavigationTransitionInfo()); // Navigate to Home page with null args and null animation
}

public void OnTitleBarLayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
{
    var bar = sender as CoreApplicationViewTitleBar;
    RightPanel.Margin = new Thickness(0, 0, bar.SystemOverlayRightInset, 0);
}

屏幕截图:

Screenshot

网址:

Title bar customization

Layout panels

Handling and raising events


我的英语很抱歉。

最诚挚的问候。