.Net 6 Maui - 以全屏模式打开应用程序 (Windows)

问题描述 投票:0回答:1

是否可以在 Windows 上以全屏模式打开 .NET 6 Maui 下的应用程序?这可以最初在 App.xaml.cs 中定义。谷歌没有得到任何有用的结果

我已经尝试过以下方法:

1.

protected override Window CreateWindow(IActivationState activationState)
{
    var window = new Window(activationState)
    {
        Content = new MainPage()
    };

    // Set the window to full-screen
    window.WindowState = WindowState.FullScreen; <= WindowState not found on property "window"

    return window;
}

2.

public App()
{
    InitializeComponent();
    MainPage = new MainPage();

    if (Device.RuntimePlatform == Device.Windows)
    {
        var applicationView = ApplicationView.GetForCurrentView();

        applicationView.TryEnterFullScreenMode();
    }
}
.net windows .net-6.0 maui fullscreen
1个回答
0
投票

您可以尝试将以下代码放入MauiProgram.cs中:

var builder = MauiApp.CreateBuilder();
            builder
           
                  .UseMauiApp<App>()
                  .ConfigureFonts(fonts =>
                  {
                        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                  });
#if WINDOWS
        builder.ConfigureLifecycleEvents(events =>  
        {  
            events.AddWindows(wndLifeCycleBuilder =>  
            {  
                wndLifeCycleBuilder.OnWindowCreated(window =>  
                {  
                //    window.ExtendsContentIntoTitleBar = false;  
                    IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
                    Microsoft.UI.WindowId myWndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);  
                    var _appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(myWndId);  
                    _appWindow.SetPresenter(Microsoft.UI.Windowing.AppWindowPresenterKind.FullScreen);   
                 //if you want to full screen, you can use this line
               // (_appWindow.Presenter as Microsoft.UI.Windowing.OverlappedPresenter).Maximize();   
                //if you want to Maximize the window, you can use this line                    
                });  
            });  
        });
#endif

或者您可以尝试将以下代码放入MainPage.cs中:

protected override void OnHandlerChanged()
      {
            base.OnHandlerChanged();
#if WINDOWS
                  var window = App.Current.Windows.FirstOrDefault().Handler.PlatformView as Microsoft.UI.Xaml.Window;
                  IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                  Microsoft.UI.WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle);
                  Microsoft.UI.Windowing.AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
                appWindow.SetPresenter(Microsoft.UI.Windowing.AppWindowPresenterKind.FullScreen);
            //   (appWindow.Presenter as Microsoft.UI.Windowing.OverlappedPresenter).Maximize();
            // this line can maximize the window
#endif
      }
© www.soinside.com 2019 - 2024. All rights reserved.