Skip to content

mattregul/Xamarin_Screensize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

Xamarin_Screensize

Here are examples for storing the specific OS's screen details in static vars, located in the Xamarin Forms App class

Android (MainActivity.cs) - Jump to Code Page

// Store off the device sizes, so we can access them within Xamarin Forms
//  Screen Width = WidthPixels / Density
//  Screen Height = HeightPixels / Density

App.DisplayScreenWidth  = (double)Resources.DisplayMetrics.WidthPixels / (double)Resources.DisplayMetrics.Density;
App.DisplayScreenHeight = (double)Resources.DisplayMetrics.HeightPixels / (double)Resources.DisplayMetrics.Density; 
App.DisplayScaleFactor  = (double)Resources.DisplayMetrics.Density;

iOS (AppDelegate.cs) - Jump to Code Page

// Store off the device sizes, so we can access them within Xamarin Forms
App.DisplayScreenWidth  = (double)UIScreen.MainScreen.Bounds.Width;
App.DisplayScreenHeight = (double)UIScreen.MainScreen.Bounds.Height;
App.DisplayScaleFactor  = (double)UIScreen.MainScreen.Scale;

UWP (App.xaml.cs) - Jump to Code Page

// You decided which is best for you...
//  Do you want Size of the App's View
//      or
//  Do you want the Display's resolution 
// ######################################

// Size of App's view
screensize.App.DisplayScreenHeight = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Height;
screensize.App.DisplayScreenWidth = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Width;

// Size of screen's resolution
//screensize.App.DisplayScreenWidth = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels;
//screensize.App.DisplayScreenHeight = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;

// Pixels per View Pixel
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/design-and-ui-intro#effective-pixels-and-scaling
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/screen-sizes-and-breakpoints-for-responsive-design
screensize.App.DisplayScaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;

Windows Phone 8.1 (App.xaml.cs) - Jump to Code Page

// Size of App's view
screensize.App.DisplayScreenHeight = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Height;
screensize.App.DisplayScreenWidth = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Width;

// Pixels per View Pixel
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/design-and-ui-intro#effective-pixels-and-scaling
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/screen-sizes-and-breakpoints-for-responsive-design
screensize.App.DisplayScaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;

Windows 8.1 (App.xaml.cs) - Jump to Code Page

// Size of App's view
screensize.App.DisplayScreenHeight = Window.Current.Bounds.Height;
screensize.App.DisplayScreenWidth = Window.Current.Bounds.Width;
screensize.App.DisplayScaleFactor = 1; // No scaling here?  If you find a scaling for Windows 8.1, please let me know :)

Xamarin Forms Page (App.cs) - Jump to Code Page

namespace screensize
{
    public class App : Application
    {
        public static double DisplayScreenWidth = 0f;
        public static double DisplayScreenHeight = 0f;
        public static double DisplayScaleFactor = 0f;

        public App()
        {

            string ScreenDetails = Device.OS.ToString() + " Device Screen Size:\n" +
                $"Width: {DisplayScreenWidth}\n" +
                $"Height: {DisplayScreenHeight}\n" +
                $"Scale Factor: {DisplayScaleFactor}";

            // The root page of your application
            var content = new ContentPage
            {
                Title = "Xamarin_GetDeviceScreensize",
                Content = new StackLayout
                {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
                        new Label {
                            HorizontalTextAlignment = TextAlignment.Center,
                            FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
                            Text = ScreenDetails
                        }
                    }
                }
            };

            MainPage = new NavigationPage(content);
        }

    }
}

Additional Goodies

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages