How to get the resolution of screen? For a WinRT app?

前端 未结 5 1412
南笙
南笙 2020-12-08 02:47

I want to know the screen resolution so that I can set the height of an element according to the resolution in a Windows 8 app.

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 02:52

    Getting the bounds of current window is easy. But say if you want to set a large font size for bigger screen (resolution is same as 10" device, but screen is 27"), this wont help. Refer Scaling to different screens I used the following method to detect screen size & change text block font style appropriately.

             void detectScreenType()
        {
            double dpi = DisplayProperties.LogicalDpi;
            var bounds = Window.Current.Bounds;
            double h;
            switch (ApplicationView.Value)
            {
                case ApplicationViewState.Filled:
                    h = bounds.Height;
                    break;
    
                case ApplicationViewState.FullScreenLandscape:
                    h = bounds.Height;
                    break;
    
                case ApplicationViewState.Snapped:
                    h = bounds.Height;
                    break;
    
                case ApplicationViewState.FullScreenPortrait:
                    h = bounds.Width;
                    break;
    
                default:
                    return;
            }
            double inches = h / dpi ;
            string screenType = "Slate";
            if (inches < 10)
            {
                screenType = "Slate";
            } else if (inches < 14) {
                screenType = "WorkHorsePC";
            }
            else 
            {
                screenType = "FamilyHub";
            }
            ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            localSettings.Values["screenType"] = screenType;
        }
    

提交回复
热议问题