[英]How to ensure C# UWP app is full screen on the FIRST launch after the installation?
我正在寫一個 UWP C# 應用程序。 我正在嘗試在應用程序啟動期間修復我的 window 大小。 為此,我正在調整 App.xaml.cs 文件中 window 的大小。 下面是調整 window 大小的方法。
private void MaximizeWindowOnLoad()
{
var displayInformation = DisplayInformation.GetForCurrentView();
var widthOfScreen = displayInformation.ScreenWidthInRawPixels;
var heightOfScreen = displayInformation.ScreenHeightInRawPixels;
var scaleFactor = displayInformation.RawPixelsPerViewPixel;
var bounds = new Size(widthOfScreen / scaleFactor, heightOfScreen / scaleFactor);
var currentApplicationView = ApplicationView.GetForCurrentView();
currentApplicationView.SetPreferredMinSize(bounds);
ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
currentApplicationView.TryResizeView(bounds);
}
我從App.xaml.cs 中的 OnLaunched方法調用MaximizeWindowOnLoad 。 在OnLaunched方法下方。
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (e.PrelaunchActivated == false)
{
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// Ensure the current window is active
MaximizeWindowOnLoad();
Window.Current.Activate();
}
}
除一種情況外,代碼工作正常。 安裝應用程序后,在第一次啟動時,代碼不起作用。 我檢查過,在調試過程中調用了該方法。 我在 stackoverflow 中使用以下線程中的方法。 鏈接。
我試圖調試以了解問題。 但是,該方法的行為對於應用程序打開情況是不同的(例如,第一次打開和隨后打開)。 我希望代碼可以正常工作,但我不明白這是 UWP 的問題還是方法不正確。
您在代碼中使用兩種方法來更改 windows 大小。 一個是ApplicationViewWindowingMode.PreferredLaunchViewSize
,另一個是TryResizeView
。
您得到的行為應該是ApplicationViewWindowingMode.PreferredLaunchViewSize
的效果。 我認為TryResizeView
返回 false 所以它實際上沒有效果。
此 API 用於設置一個值以指示應用程序啟動時使用的窗口模式。 但正如文檔的備注部分中已經提到的那樣 -對於應用程序的首次啟動,PreferredLaunchWindowingMode 將始終為 Auto,而 ApplicationView.PreferredLaunchViewSize 將由系統策略確定。 API 適用於應用程序的下一次啟動。
所以很清楚你的行為。 PreferredLaunchViewSize
僅在您第二次啟動應用程序時有效。 當您第一次啟動應用程序時,應用程序的大小由系統決定。
這個API是用來改變view的大小為指定大小的。 它可以在您啟動應用程序時第一次運行。 但是在你的場景中,它返回false
所以它什么都不做。
它失敗的原因也在文檔的備注部分中提到——調整大小請求無效,並且該方法在這些情況下返回 false:請求的大小大於可用工作區。
由於任務欄等原因,可用工作區通常小於全屏尺寸。 比如我的顯示器是1920*1080 ,縮放值為100%。 如果我想成功調用此方法,我可以使用的最大尺寸是1904*991 (已測試)。
概括
如果你想在第一次啟動時改變你的 window 的尺寸,你需要調用TryResizeView
方法並給它一個小於全屏尺寸的尺寸。 並將PreferredLaunchViewSize
設置為全屏大小。
第二次啟動時,停止調用TryResizeView
方法,讓PreferredLaunchViewSize
方法自動將windows尺寸變為全屏。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.