Implementing a Full-Screen
<UserControl x:Class='SilverlightApplication3.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:DesignWidth='640'
d:DesignHeight='480'
xmlns:System="clr-namespace:System;assembly=mscorlib" >
<UserControl.Resources>
<ScaleTransform ScaleX="1" ScaleY="1" x:Key="ReduceScaleTransform" />
</UserControl.Resources>
<UserControl.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1" x:Name="ScaleToFullScreen" />
</UserControl.RenderTransform>
<StackPanel x:Name="MediaPlayerPanel">
<MediaElement x:Name="mediaElement" Source="/Video/Video.wmv"
Margin="4,2,4,2" MediaEnded="mediaElement_MediaEnded">
<MediaElement.Clip>
<RectangleGeometry Rect="0,0,260,170" RadiusX="20" RadiusY="20"/>
</MediaElement.Clip>
</MediaElement>
<StackPanel x:Name="ButtonPanel" Orientation="Horizontal" RenderTransform="{StaticResource ReduceScaleTransform}">
<Button x:Name="PlayPauseButton" Content="Pause" Click="PlayPauseButton_Click"/>
<Button Content="Stop" x:Name="StopButton" Click="StopButton_Click"/>
<Button Content="Full" x:Name="FullScreenButton" Click="FullScreenButton_Click"/>
</StackPanel>
</StackPanel>
</UserControl>
//File: Page.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace SilverlightApplication3
{
public partial class MainPage : UserControl
{
private double _embeddedWidth;
private double _embeddedHeight;
public MainPage()
{
InitializeComponent();
Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
_embeddedWidth = this.Width;
_embeddedHeight = this.Height;
}
void Content_FullScreenChanged(object sender, EventArgs e)
{
if (!Application.Current.Host.Content.IsFullScreen)
{
ScaleToFullScreen.ScaleX = 1.0d;
ScaleToFullScreen.ScaleY = 1.0d;
((ScaleTransform)this.Resources["ReduceScaleTransform"]).ScaleX = 1.0d;
((ScaleTransform)this.Resources["ReduceScaleTransform"]).ScaleY = 1.0d;
ButtonPanel.HorizontalAlignment = HorizontalAlignment.Center;
}
else
{
double pluginWidth = Application.Current.Host.Content.ActualWidth;
double pluginHeight = Application.Current.Host.Content.ActualHeight;
double scaleX = pluginWidth / _embeddedWidth;
double scaleY = pluginHeight / _embeddedHeight;
ScaleToFullScreen.ScaleX = scaleX;
ScaleToFullScreen.ScaleY = scaleY;
((ScaleTransform)this.Resources["ReduceScaleTransform"]).ScaleX = scaleX * .10d;
((ScaleTransform)this.Resources["ReduceScaleTransform"]).ScaleY = scaleY * .10d;
ButtonPanel.HorizontalAlignment = HorizontalAlignment.Right;
}
}
private void FullScreenButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Host.Content.IsFullScreen =
!Application.Current.Host.Content.IsFullScreen;
if (Application.Current.Host.Content.IsFullScreen)
{
FullScreenButton.Content = "Emb";
}
else
{
FullScreenButton.Content = "Full";
}
}
private void PlayPauseButton_Click(object sender, RoutedEventArgs e)
{
if ((mediaElement.CurrentState == MediaElementState.Stopped) ||
(mediaElement.CurrentState == MediaElementState.Paused))
{
mediaElement.Play();
PlayPauseButton.Content = "Pause";
}
else if (mediaElement.CurrentState == MediaElementState.Playing)
{
mediaElement.Pause();
PlayPauseButton.Content = "Play";
}
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
mediaElement.Stop();
PlayPauseButton.Content = "Play";
}
private void mediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
mediaElement.Position = new TimeSpan(0);
PlayPauseButton.Content = "Play";
}
}
}
Related examples in the same category