How to get screen size on Windows Phone 7 Series?
Asked Answered
P

5

18

How do I programatically get the screen resolution on WP7? Here are a bunch of links that get the same job done in desktop WPF and Silverlight, but none of them are in the Phone SDK.

Any ideas?

http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/f0639904-a368-44db-9ddd-efcaf8fc736e
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6b6b832f-0dfd-428c-84cd-b1b9e7f236cf
How can I get the active screen dimensions?
http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/f0639904-a368-44db-9ddd-efcaf8fc736e

Paraffin answered 8/4, 2010 at 0:8 Comment(2)
You've got one of those phones? Wow--I didn't know they were available for anyone to develop with them yet.Shuttering
No, just the emulator :(Paraffin
C
14

It looks like Application.Current.RootVisual.RenderSize will give you that information.

Cosmography answered 8/4, 2010 at 12:23 Comment(2)
Handy to know as even though there are going to be two resolutions for Windows Phone 7 (480×800) and (320×480) they don't have quite the same aspect ratio, which I'm guessing is the reason for the question.Rocca
Please see Luke Puplett's answer below if you are trying to do this. RootVisual.RenderSize isn't likely to give you exactly what you want.Trenna
R
66

I use this:

this.ScreenWidth = System.Windows.Application.Current.Host.Content.ActualWidth;
this.ScreenHeight = System.Windows.Application.Current.Host.Content.ActualHeight;

Many ways to skin an app. If its for XAML, you could bind to the properties of the LayoutRoot.

Height="{Binding ElementName=LayoutRoot,Path=ActualHeight}"
Rite answered 8/12, 2010 at 15:51 Comment(2)
This is the better answer. As Quetzlcoatl pointed out the accepted answer has problems.Trenna
This answer is better. Using this method you can also get the screen dimensions in the constructor. Using the accepted answer I got a null-reference exception.Championship
C
14

It looks like Application.Current.RootVisual.RenderSize will give you that information.

Cosmography answered 8/4, 2010 at 12:23 Comment(2)
Handy to know as even though there are going to be two resolutions for Windows Phone 7 (480×800) and (320×480) they don't have quite the same aspect ratio, which I'm guessing is the reason for the question.Rocca
Please see Luke Puplett's answer below if you are trying to do this. RootVisual.RenderSize isn't likely to give you exactly what you want.Trenna
S
8

I cannot down/upvote yet(actually, now I can and I did so), but I'd like to point out that "Luke Puplett"s answer including Application.Current.Host.Content is the correct one, not "Andréas Saudemont"s one that advises RenderSize.

I'll use names that are commonly used in tutorials or sample applications from MSDN.

I say that Host.Current is more adequate, because on the very very start of the application, especially on the WP7 (I dont know how it is on "regular" SL3/SL4 on PC) - that is, for example, in the very first page's constructor - the RenderSize property is NOT YET SET correctly, as the "RootVisual" of the application is being constructed and have not yet been assigned in the "App.xaml.cs". At least in that one point, the RenderSize=Size{0,0}

On the other hand, if only the App starts correctly, the Host.Content is set to some phoneframe, that is correctly full-screen-sized and rotated to the actual screen position. I'd guess that on the very start it is the starting splash screen (empty or static from JPG file)

Stempien answered 20/4, 2011 at 19:1 Comment(0)
D
4

If you want to access the display size from within an XNA Game, use this:

graphics.GraphicsDevice.DisplayMode.Width

and

graphics.GraphicsDevice.DisplayMode.Height

where graphics is the current game's GraphicDeviceManager !

Diocletian answered 20/1, 2012 at 2:14 Comment(0)
S
1

Entire screen:

(App.Current.RootVisual as FrameworkElement).ActualHeight
(App.Current.RootVisual as FrameworkElement).ActualWidth

Current page:

If you want to know the visible size (taking AppBar / SystemTray into account) then just ask for it off the page instead.

Supen answered 10/4, 2010 at 0:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.