Even a chimp can write code

Tuesday, March 11, 2008

How to tell the screen resolution from within a Silverlight app

There isn't a direct API for detecting the screen width and height from within a Silverlight app. Fortunately, JavaScript provides you a way, viz. via the screen.width and screen.height properties. So with a nifty use of Silverlight's HTML DOM Bridge, these values can be read with little issue.

int screenWidth = Int32.Parse(

HtmlPage.Window.Eval("screen.width").ToString()

);

int screenHeight = Int32.Parse(

HtmlPage.Window.Eval("screen.height").ToString()

);


Then when your app runs in fullscreen mode, you'd get the screen resolution using the Silverlight plug-in's ActualWidth and ActualHeight.

int screenWidth =

Application.Current.Host.Content.ActualWidth;

int screenHeight =
Application.Current.Host.Content.ActualHeight;

Labels:

Email this | Bookmark this

2 Comments:

Post a Comment | Home | Inference: my personal blog