如何在WinUI 3中移除关闭按钮?

3
我该如何在WinUI 3中移除关闭按钮?
WinUI 3应用程序截图: 1

1
标题栏自定义 https://learn.microsoft.com/zh-cn/windows/apps/develop/title-bar 仅适用于 Windows 11+。 - Simon Mourier
1
据我所知,目前无法自定义标题按钮。该区域是保留区域。我能想到的最接近的方法是通过将窗口设置为全屏模式来隐藏标题按钮。 - Andrew KeepCoding
2个回答

2
似乎WinUI框架没有提供相关的API。但我们可以间接地实现这个功能。
this.ExtendsContentIntoTitleBar = true;时,右上角的菜单按钮(最大化、最小化和关闭)会使用常见的WinUI元素进行模拟。这可以在Debug->Windows->Live Visual Tree工具窗口中观察到。这是我的程序的屏幕截图。 Visual Tree ScreenShot 请注意,在Window xaml文件中声明的根元素是ClientAreaPresenter的子元素。模拟的按钮位于TitleBarMinMaxCloseContainer下面。
因此,我们需要做的是:
  1. 使用Microsoft.UI.Xaml.Media.VisualTreeHelper.GetParent()获取关闭按钮元素
  2. 将其Visibility属性设置为Visibility.Collapsed。(但这可能看起来不太好。我个人建议同时隐藏所有按钮)。
以下是适用于我的示例代码。
public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
        this.ExtendsContentIntoTitleBar = true; //Hides the default system titlebar.
    }

   //Visual tree may not have been loaded yet at Ctor. 
   //It's suggested registering Loaded event of the root element of the window.
    private void RootGrid_Loaded(object sender, RoutedEventArgs e)
    {
        //Gets the close button element. Refer to the Visual Tree.
        var contentPresenter = VisualTreeHelper.GetParent(this.Content);
        var layoutRoot = VisualTreeHelper.GetParent(contentPresenter);
        var titleBar = VisualTreeHelper.GetChild(layoutRoot, 1) as Grid;
        var buttonContainer = VisualTreeHelper.GetChild(titleBar, 0) as Grid;
        var closeButton = VisualTreeHelper.GetChild(buttonContainer,2) as Button;
        if (closeButton != null)
        {
            closeButton.Visibility = Visibility.Collapsed; //Hides the button.
        }
    }
}

如果你想隐藏所有的菜单按钮,那么改变buttonContainerVisibility属性即可。还可以通过设置RenderTransform使其超出窗口渲染区域来实现隐藏效果。

带有隐藏关闭按钮效果的窗口

然而,这种方法没有文档记录,所以它的兼容性取决于框架的可视树实现。在某些情况下,应用程序在关闭窗口时可能会抛出异常。


1
当隐藏所有标题栏按钮时,建议您使用RenderTransform而不是Visibility,因为Visibility.Collapsed有时无法阻止点击按钮。titleBar.RenderTransform = new TranslateTransform() { X = 200 }(这里的titleBar是先前获取的元素)。 - undefined

0
在我有限的WinUI 3经验中,我不能仅仅移除关闭按钮,但是可以隐藏整个标题栏区域。

MainWindow.xaml.cs

using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Windowing;

namespace WinUI3
{
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();

            GetAppWindowAndPresenter();
            _apw.IsShownInSwitchers = false;
            _presenter.SetBorderAndTitleBar(false, false);
        }

        private void myButton_Click(object sender, RoutedEventArgs e)
        {
            myButton.Content = "Clicked";
            this.Close();
        }

        public void GetAppWindowAndPresenter()
        {
            var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
            WindowId myWndId = Win32Interop.GetWindowIdFromWindow(hWnd);
            _apw = AppWindow.GetFromWindowId(myWndId);
            _presenter = _apw.Presenter as OverlappedPresenter;
        }
        private AppWindow _apw;
        private OverlappedPresenter _presenter;
    }
}

这段代码将创建一个没有标题栏的窗口(包括最小化按钮、最大化按钮和关闭按钮)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接