簡體   English   中英

UWP 在開始時設置為最大化視圖似乎不起作用

[英]UWP Set to maximized view on start seems to be not working

我已經嘗試了所有這些答案,但都沒有奏效:

  1. https://stackoverflow.com/a/44273903/2901207 -> 根本不做任何事情,試圖把它放在不同的地方,但大部分都和答案一樣。 沒有反應。 這段代碼:

     private void MaximizeWindowOnLoad() { var view = DisplayInformation.GetForCurrentView(); // Get the screen resolution (APIs available from 14393 onward). var resolution = new Size(view.ScreenWidthInRawPixels, view.ScreenHeightInRawPixels); // Calculate the screen size in effective pixels. // Note the height of the Windows Taskbar is ignored here since the app will only be given the maxium available size. var scale = view.ResolutionScale == ResolutionScale.Invalid? 1: view.RawPixelsPerViewPixel; var bounds = new Size(resolution.Width / scale, resolution.Height / scale); ApplicationView.GetForCurrentView().SetPreferredMinSize(bounds); ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height); ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize; }

    我嘗試在this.InitializeComponent();之前和之后執行的主頁。 我嘗試在App構造函數中執行此操作,但這會引發DisplayInformation.GetForCurrentView();異常。 . 我曾嘗試在創建框架后立即執行此操作,但也沒有運氣。

  2. https://stackoverflow.com/a/35250107/2901207 -> 調整大小但未最大化,因為 window 不在左上角,因此無法刪除-100 對於TryResizeView ,設置為非常大的值也會返回false

  3. 當然,我嘗試過使用: ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Maximized;

如何讓我的 UWP 應用程序最大化但不是全屏 我只想通過代碼按下那個最大化按鈕,太容易了。 還是那么難。

一切都很好,除了您需要在OnLaunched事件的App.xaml.cs中執行此操作。 正如@Raymond 在評論中所說,您需要在最后添加以下行

       Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryResizeView(bounds);

完整代碼如下

App.xaml.cs

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
        {
            rootFrame = new Frame();

            rootFrame.NavigationFailed += OnNavigationFailed;
            Window.Current.Content = rootFrame;
        }

        if (e.PrelaunchActivated == false)
        {
            if (rootFrame.Content == null)
            {
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
            MaximizeWindowOnLoad();
            Window.Current.Activate();
        }
    }
    private void MaximizeWindowOnLoad()
    {
        var view = DisplayInformation.GetForCurrentView();
        var resolution = new Size(view.ScreenWidthInRawPixels, view.ScreenHeightInRawPixels);
        var scale = view.ResolutionScale == ResolutionScale.Invalid ? 1 : view.RawPixelsPerViewPixel;
        var bounds = new Size(resolution.Width / scale, resolution.Height / scale);
        ApplicationView.GetForCurrentView().SetPreferredMinSize(bounds);
        ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
        ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
       Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryResizeView(bounds);

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM