I am new to xamarin.forms. I want to get height,width and resolution of my mobile programmatically in xamarin.forms. Please help to resolve this.
Asked
Active
Viewed 3,567 times
1
-
Possible duplicate of [Get current screen width in xamarin forms](https://stackoverflow.com/questions/38891654/get-current-screen-width-in-xamarin-forms) – Jason May 21 '18 at 13:13
-
hi jason, Thanks for the reply. But i am getting -1 as width using string wid = Application.Current.MainPage.Width.ToString(); – Manthiram May 21 '18 at 13:18
1 Answers
7
Recently Xamarin.Essentials NuGet pakage was released and there is a useful class DeviceDisplay
in there that should handle this task for you.
The documentation can be found here.
Usage example:
// Get Metrics
var metrics = DeviceDisplay.ScreenMetrics;
// Orientation (Landscape, Portrait, Square, Unknown)
var orientation = metrics.Orientation;
// Rotation (0, 90, 180, 270)
var rotation = metrics.Rotation;
// Width (in pixels)
var width = metrics.Width;
// Height (in pixels)
var height = metrics.Height;
// Screen density
var density = metrics.Density;

EvZ
- 11,889
- 4
- 38
- 76
-
when i add "using Xamarin.Essentials;", it shows error. any suggestions... – Manthiram May 21 '18 at 13:45
-
Without knowing your error it will be hard to help. Wild shot maybe you are not on .NET Standard? Check the documentation links that I provided in my answer, there is a very nice getting started section in the official docs. – EvZ May 21 '18 at 13:48
-
2This seems to have changed to `DeviceDisplay.MainDisplayInfo.Height` etc – Danny Beckett Oct 30 '20 at 15:13