由於在 win10 上,app 在運行的時候,默認不是佔滿全屏幕,而且 windows runtime 當前沒有提供獲取javascript
屏幕尺寸的 api。在桌面 win32 api 中,能夠獲取屏幕尺寸,不過由於是限制的 api,不能直接使用。html
我當前 PC 的屏幕尺寸:java
方法一:經過在 WebView 控件中調用 javascript 的方式,獲取屏幕分辨率:web
在頁面中放一個 WebView 控件:windows
<WebView x:Name="webView" LoadCompleted="WebView_LoadCompleted"/>
在頁面加載完成事件中,添加 js 代碼:api
private void WebView_test_Loaded(object sender, RoutedEventArgs e) { //webView.NavigateToString("<script>" + // "window.onload = function loaded() {" + // "document.write('屏幕分辨率爲:' + window.screen.width + ' X ' + window.screen.height ); }" + // " </script>");
// 在 WebView 控件中,添加獲取屏幕分辨率的方法 webView.NavigateToString("<html><head><script>function getScreenSize() { return window.screen.width + ' X ' + window.screen.height;}</script></head></html>"); } private async void WebView_LoadCompleted(object sender, NavigationEventArgs e) { // 在 C# 中調用 Javascript 方法,得到返回值 var str = await webView.InvokeScriptAsync("getScreenSize", null); Debug.WriteLine(str); }
運行 app:app
方式二:默認在 app 第一次啓動的時候,窗口爲全屏模式,而後獲取窗口的尺寸。async
全屏時,窗口尺寸爲屏幕的尺寸,能夠把得到的尺寸存儲在 Settings 裏,供ide
後面使用。缺點就是,默認第一次全屏啓動,可能不是用戶想要的行爲,不能強迫用戶。spa
調用 ApplicationView api:
Windows.UI.ViewManagement.ApplicationView view = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView(); // 方法名中有個 「try」 詞,也就是會出現 「顯示全屏」 失敗的狀況 view.TryEnterFullScreenMode(); // 若是當即讀取屏幕尺寸,還不是全屏的。在後面某個時刻全屏後,獲取屏幕尺寸 Debug.WriteLine(Window.Current.Bounds.Width + " X " + Window.Current.Bounds.Height);
(與本文無關的)其它 ApplicationView 的操做:
Windows.UI.ViewManagement.ApplicationView view = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView(); private void Button_full_Click(object sender, RoutedEventArgs e) { Debug.WriteLine("全屏操做:" + view.TryEnterFullScreenMode()); bool isFull = view.IsFullScreen; } private void Button_exitFull_Click(object sender, RoutedEventArgs e) { view.ExitFullScreenMode(); Debug.WriteLine("退出全屏"); } private void Button_resize_Click(object sender, RoutedEventArgs e) { Debug.WriteLine("改變視圖 : " + view.TryResizeView(new Size(1200, 900))); } private void Button_boundsMode_Click(object sender, RoutedEventArgs e) { if (view.DesiredBoundsMode == Windows.UI.ViewManagement.ApplicationViewBoundsMode.UseVisible) { view.SetDesiredBoundsMode(Windows.UI.ViewManagement.ApplicationViewBoundsMode.UseCoreWindow); Debug.WriteLine("設置爲 : ApplicationViewBoundsMode.UseCoreWindow"); } else { view.SetDesiredBoundsMode(Windows.UI.ViewManagement.ApplicationViewBoundsMode.UseVisible); Debug.WriteLine("設置爲 : ApplicationViewBoundsMode.UseVisible"); } } // 設定窗口大小 private void Button_mini_size_Click(object sender, RoutedEventArgs e) { view.SetPreferredMinSize(new Size(100,100)); } private void Button_standard_overlays_Click(object sender, RoutedEventArgs e) { view.ShowStandardSystemOverlays(); } // 獲取視圖的 尺寸和 位置 private void Button_VisibleBounds_Click(object sender, RoutedEventArgs e) { Rect rect = view.VisibleBounds; Debug.WriteLine(string.Format("VisibleBounds ---- Left:{0}, Right:{1}, Width:{2}, Height:{3}", rect.Left, rect.Right, rect.Width, rect.Height)); } // 下次 app 啓動時,但願窗口的尺寸 private void Button_PreferredLaunchViewSize_Click(object sender, RoutedEventArgs e) { Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize = new Size(800, 800); } // 過時了 private void Button_ApplicationViewState_Click(object sender, RoutedEventArgs e) { Debug.WriteLine("ApplicationViewState:" + Windows.UI.ViewManagement.ApplicationView.Value.ToString()); } // 顯示窗口左上角的 「back」 按鈕,在 phone 上與 back 按鈕關聯 private void Button_global_back_Click(object sender, RoutedEventArgs e) { var curView = Windows.UI.Core.SystemNavigationManager.GetForCurrentView(); if(curView.AppViewBackButtonVisibility == Windows.UI.Core.AppViewBackButtonVisibility.Visible) { curView.AppViewBackButtonVisibility = Windows.UI.Core.AppViewBackButtonVisibility.Collapsed; } else { curView.AppViewBackButtonVisibility = Windows.UI.Core.AppViewBackButtonVisibility.Visible; } }