Light Up the Web with Silverlight by Jacek Ciereszko

Blog about programming in Silverlight

  Home  |   Contact  |   Syndication    |   Login
  20 Posts | 0 Stories | 28 Comments | 0 Trackbacks

News

My post about Deep Zoom Composer was recommended by Scott Guthrie! :) (see here)

Archives

About Me

News

Who was here

This time I would like to show, how to create full screen in Silverlight 2.0 RTW. Changing application to full screen using only "Application.Current.Host.Content.IsFullScreen" is nothing special because only web browser's window is changing. Our application, have still the same size like before switching to full screen state.

So how to resize application, so it will change simultaneously with web browser? Solution is very simple and all we have to do is use transformations.

Let's start with simple full screen. To switch to full screen mode we have to run only this code:

Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;

Now, we can add button to invoke code above.

Page.xaml:

<Button Content="Full Screen" Click="Button_Click" />

Page.xaml.cs:

private void Button_Click(object sender, RoutedEventArgs e)

{

 Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;

}

If you run your application now, you will see that full screen is already working and you can switch views by pushing the button.

So we have full screen but our application still have the same size.. Let's do something with that.

First we have to add ScaleTransform in Page.xaml code:

 <Grid.RenderTransform>
<
ScaleTransform ScaleX="1" ScaleY="1" x:Name="RootLayoutScaleTransform" />
 </Grid.RenderTransform>

and then some code in Page.xaml.cs. In constructor we have to remember original size

    double _oldHeight, oldWidth;

  _oldHeight = this.Height;
  _oldWidth = this.Width;

 and add some methods to work with resize and full screen events

Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);
Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_Resized);

void Content_Resized(object sender, EventArgs e)
{
double currentWidth = Application.Current.Host.Content.ActualWidth;
double currentHeight = Application.Current.Host.Content.ActualHeight;

double uniformScaleAmount = Math.Min((currentWidth / _oldWidth), (currentHeight / _oldHeight));
RootLayoutScaleTransform.ScaleX = uniformScaleAmount;
RootLayoutScaleTransform.ScaleY = uniformScaleAmount;

}

FIN, our application is ready! It can resize with Web Browser's window and also when we turn on/off full screen mode.

But what happen if we use mouse and we have to read coordinates? Mouse’s coordinates are now not scaled, so all we have to do, is divide these points by our variable ”uniformScaleAmount”.

double currentY = e.GetPosition(null).Y / uniformScaleAmount;
double currentX = e.GetPosition(null).X / uniformScaleAmount;

Constraints

I would like to add, that we have also few constraints. Namely, code above ( ..IsFullScreen != ..IsFullScreen) can be used only in event handler, invoked by user. So we can change application to full screen mode, only by clicking mouse on something or by pressing key on keyboard. This is because of security reasons. None of us, want to see advertisement, created in Silverlight, which will appear all the time and always in full screen mode ;)

Another constrains are keyboard and mouse functionalities in full screen mode, which are reduce to only:

  • arrows keys and space bar on keyboard
  • mouse buttons (even mouse wheel doesn't work!)
The last one is really "pain in my ass", because then applications written in Deep Zoom Composer in full screen mode, do not have zoom functionality. We can add keys to zoom but this is not the same fun like with mouse!

You can find more about constraints in MSDN link.

FIN

OK, now we have all that we can imagine. Our solution is simple and it can be easily applied to every application. It will be nice and very useful feature in every project.

Source code:   source code

Working demo:    play demo (Resize window and click on button!)

Resize me and click on me!

Working Example (Resize it or turn on Full Screen mode)

Silverlight Solitaire

You can play it in small window, so nobody will see you in work :]

Resources

  • Mike Snows's blog link
  • Jeff Blankenburg's blog link
Polish version of this article
 
Sorry for my English. I hope you can understand it. :P
Jacek Ciereszko
polish blog: http://jacekciereszko.pl/

kick it on DotNetKicks.com

posted on Tuesday, June 10, 2008 1:31 AM

Feedback

# re: Full Screen mode in Silverlight 2.0 Beta 2 applications 9/25/2008 10:31 PM Donavan
_oldHeight = this.Height;
_oldWidth = this.Width;

What is supposed to be done with this code?

# re: Full Screen mode in Silverlight 2.0 Beta 2 applications 9/26/2008 8:39 AM Jacek Ciereszko
Hi,
This part of code, remember original size of application. This size is used later to count new uniformScaleAmount (value to scale application).

double _oldHeight, oldWidth;

_oldHeight = this.Height;
_oldWidth = this.Width;

We need size (Height, Width) from initial stage of application to count scale value - uniformScaleAmount. Application is always scaled from original size.

I hope I helped You, but do not hesitate to ask me more.

Best Regards,
Jacek Ciereszko

# re: Full Screen mode in Silverlight 2.0 RTW applications 11/1/2008 1:25 AM Kym McGain
Great tutorial,
I've modified the code slightly to allow me to make an image full screen rather than the whole root grid...
I've got a bit of a problem though, my image gets resized correctly as I go full screen, but then it gets changed back to the original size instantly. It isn't my code changing it back so I'm a bit lost as to why it would be forced back to the original size.

Thanks for any clues you can give me.

# re: Full Screen mode in Silverlight 2.0 RTW applications 11/7/2008 12:31 PM Rukshan
Thank you very much , I've been looking for this stuff for a long time!

# re: Full Screen mode in Silverlight 2.0 RTW applications 11/7/2008 9:00 PM Jacek Ciereszko
I am glad that I could help you :)

If you find something that I pass over in my article, please write it in comment and I will add this.

"Thank you for your cooperation".

Jacek Ciereszko

# re: Full Screen mode in Silverlight 2.0 RTW applications 12/3/2008 11:50 AM Bharat
Really great tutorial, the button not getting resized when escape key is pressed. Please suggest me where am i going wrong.
Thanks in advance.


# re: Full Screen mode in Silverlight 2.0 RTW applications 1/12/2009 12:24 AM Meqdadi
Hi ...
is there any way to set the media element only to the width and heught of browser amd i'm say media element only not the hole silverlight application.

# re: Full Screen mode in Silverlight 2.0 RTW applications 1/12/2009 8:57 PM Jacek Ciereszko
Yes, you can add <ScaleTransform for media element:

<MediaElement ... >
<MediaElement.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1" x:Name="RootLayoutScaleTransform" />
</MediaElement.RenderTransform>
</MediaElement>

But i'm not sure is it gona work correct.

You can set media element size to size of your application so it will be the same and then resize it. Medial element should be in 100% on the screen.

# re: Full Screen mode in Silverlight 2.0 RTW applications 2/17/2009 1:29 AM Jorgesys
double _oldHeight, oldWidth

in your tutorial must be ::

double _oldHeight, _oldWidth

Cheers!


# re: Full Screen mode in Silverlight 2.0 RTW applications 3/11/2009 3:18 PM Jacek Ciereszko
Yes, you are absolutely right. There should be "double _oldWidth".

Sorry for that. :)

Bests,
Jacek Ciereszko

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: