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

C# 如何在UWP中最小化按钮旁边的标题栏中添加新按钮?,c#,xaml,button,uwp,titlebar,C#,Xaml,Button,Uwp,Titlebar,我想在UWP应用程序的标题栏中添加一个新按钮,如下所示: 我已经看到过一些“类似”的帖子,但答案并不十分清楚,也缺乏细节,我很难理解他们做了什么。默认情况下,UWP无法在标题栏中添加按钮。但uwp支持自定义标题栏布局 用于启动隐藏标题栏视图 CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true; 创建新网格布局并将其附加到标题栏后 Window.Current.SetTitleBar(UserLayou

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


我已经看到过一些“类似”的帖子,但答案并不十分清楚,也缺乏细节,我很难理解他们做了什么。

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

用于启动隐藏标题栏视图

CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
创建新网格布局并将其附加到标题栏后

Window.Current.SetTitleBar(UserLayout);
创建用于动态创建页边距的
标题栏
和订阅
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>
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);
}
屏幕截图:

<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>
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);
}

URL:

<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>
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);
}


对我的英语很抱歉


致以最诚挚的问候。

只需添加一些:最小化按钮旁边有一个小的默认可拖动区域,您没有替代它的选项。当您尝试制作自定义标题栏时,请考虑这一点。