如何在WinUI3中将默认窗口模式设置为全屏?

Bil*_*obs 1 c# windows winui winui-3

namespace MyApp
{
    public sealed partial class MainWindow : Window
    {
        AppWindow m_appWindow;
        
        public MainWindow()
        {
            this.InitializeComponent();
            m_appWindow = GetAppWindowForCurrentWindow();
        }
               
        private AppWindow GetAppWindowForCurrentWindow()
        {
            IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
            WindowId myWndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
                    
            return AppWindow.GetFromWindowId(myWndId);
        }

        private void SwitchPresenter_FullScreen(object sender, RoutedEventArgs e)
        {
            m_appWindow.SetPresenter(AppWindowPresenterKind.FullScreen);
        }  
    }
}
Run Code Online (Sandbox Code Playgroud)

SwitchPresenter_FullScreen 功能有效,但如何将应用程序的默认窗口模式设置为全屏?我可以在应用程序启动时调用 SwitchPresenter_FullScreen 吗?

And*_*ing 5

不,不要SwitchPresenter_FullScreen直接打电话。此方法适用于您的 UI 控件(例如CheckBoxToggleButton)。

只需将此行添加到您的MainWindow构造函数中即可。

public MainWindow()
{
    this.InitializeComponent();
    m_appWindow = GetAppWindowForCurrentWindow();
    m_appWindow.SetPresenter(AppWindowPresenterKind.FullScreen);  // This line
}
Run Code Online (Sandbox Code Playgroud)

  • 我确实升级了 NuGet 包(Windows APP SDK 1.0.3 到 1.1.3),现在可以使用了! (2认同)