views:

51

answers:

2

Problem:

I'm using the Silverlight Application.Current.Host.Content.FullScreenChanged event to detect when a user switches back and forth between fullscreen mode. Unfortunately it seems that this event fires BEFORE anything on the screen is actually resized.

I need to know the ActualWidth/ActualHeight of various FrameworkElements AFTER the change to and from full screen is complete... Any ideas?

+1  A: 

You should be able to get the correct size by handling the SizeChanged event of the main application window. If you explicitly need to know if the application is changing from / to full screen mode, perhaps you could set a flag in the FullScreenChanged event handler - e.g. a bool property called IsFullScreenChanging - you could then check this property in the SizeChanged event handler on the main window, do whatever you need to do and reset the flag in anticipation of the next FullScreenChanged event.

cs:

using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            SizeChanged += MainPageSizeChanged;
        }

        private static void MainPageSizeChanged(object sender, SizeChangedEventArgs e)
        {
            Debug.WriteLine("Size is now " + e.NewSize);
        }

        private void ToggleFullScreenButtonClick(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
        }
    }
}

xaml:

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">


    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="79,110,0,0" Name="FullScreenButton" VerticalAlignment="Top" Width="75" Click="ToggleFullScreenButtonClick" />
    </Grid>
</UserControl>
Steve Willcock
Were you referring to the App.Current.Host.Content.Resized event? If so it has a different problem:It is ONLY triggered on exiting fullscreen mode, it does does NOT get called when the app enters fullscreen mode.(This smells like a bug to me, but I suspect somebody will jump in and explain why it is "by design".)
Scrappydog
I just found an interesting reproducible bug in Silverlight 4 while testing to try to resolve this question that crashes your browser!http://blog.scrappydog.com/2010/07/repo-fullscreen-bug-in-silverlight-4.html
Scrappydog
Yes, that crashed my browser - nice :) As for the events, I tried it with the SizeChanged event on the main Xaml.cs file in my app, not the App.Current.Host.Content.Resized event. The SizeChanged event seems to fire reliably both when entering and leaving full screen mode. Adding code to the main answer to demonstrate.
Steve Willcock
As usual I was defeated by the simple answer in plan sight. I totally missed the SizeChangedEventArgs.NewSize property! Doh! This seems to work as expected with ANY of the various SizeChanged event scenarios...
Scrappydog
A: 

The simple answer demonstrated in Steve's correct response above is to use:

SizeChangedEventArgs.NewSize

(Adding as a seperate concise answer for the convenience of future readers...)

Scrappydog