本文为系列文章第七篇,介绍如何在Silverlight 2中使用全屏模式。
实现全屏模式
全屏模式有时候是非常有用的,在Silverlight中,提供了很好的支持。实现起来也非常的简单,其实只有一行代码,编写一个简单的XAML。
class="code"><Canvas Background="#46461F"> <Button x:Name="toggleButton" Background="Red" Width="200" Height="80" Canvas.Top="80" Canvas.Left="150" Content="Toggle Full Screen" FontSize="20" Click="toggleButton_Click"/> <Image x:Name="class="tags" href="/tags/IMAGE.html" title=image>image" Source="smile_6.png" Canvas.Top="100" Canvas.Left="40"></Image> </Canvas>
引入命名空间
class="code">using System.Windows.Interop;
在按钮单击事件中添加实现代码。
class="code">private void toggleButton_Click(class="tags" href="/tags/OBJECT.html" title=object>object sender, RoutedEventArgs e) { Content contentObject = Application.Current.Host.Content; contentObject.IsFullScreen = !contentObject.IsFullScreen; }
获取当前的Silverlight插件“Content”对象,并设置IsFullScreen属性。运行后单击按钮将会变为全屏模式,再次单击按钮(或者按Esc键)返回普通模式。
class="tags" href="/tags/IMAGE.html" title=image>images.cnblogs.com/cnblogs_com/Terrylee/WindowsLiveWriter/Silverlight27_A002/TerryLee_Silverlight2_0039_thumb.gif" border="0" alt="TerryLee_Silverlight2_0039" width="654" height="326" />
捕获相关事件
有时候,我们需要在全屏模式和普通模式之间切换时,添加一个其它的代码,这时可以使用事件FullScreenChanged。
class="code">public Page() { InitializeComponent(); Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged); }
class="code">private void Content_FullScreenChanged(class="tags" href="/tags/OBJECT.html" title=object>object sender, EventArgs e) { Content contentObject = Application.Current.Host.Content; if (contentObject.IsFullScreen) { toggleButton.Background = new SolidColorBrush(Colors.Green); toggleButton.Content = "Full Screen Mode"; } else { toggleButton.Background = new SolidColorBrush(Colors.Red); toggleButton.Content = "Normal Mode"; } }
在普通模式和全屏模式之间切换时,改变按钮的背景色和文字。运行后点击按钮:
class="tags" href="/tags/IMAGE.html" title=image>images.cnblogs.com/cnblogs_com/Terrylee/WindowsLiveWriter/Silverlight27_A002/TerryLee_Silverlight2_0040_thumb.gif" border="0" alt="TerryLee_Silverlight2_0040" width="506" height="245" />
切换为普通模式:
class="tags" href="/tags/IMAGE.html" title=image>images.cnblogs.com/cnblogs_com/Terrylee/WindowsLiveWriter/Silverlight27_A002/TerryLee_Silverlight2_0041_thumb.gif" border="0" alt="TerryLee_Silverlight2_0041" width="547" height="397" />
完整的代码如下:
class="code">public partial class Page : UserControl { public Page() { InitializeComponent(); Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged); } private void toggleButton_Click(class="tags" href="/tags/OBJECT.html" title=object>object sender, RoutedEventArgs e) { Content contentObject = Application.Current.Host.Content; contentObject.IsFullScreen = !contentObject.IsFullScreen; } private void Content_FullScreenChanged(class="tags" href="/tags/OBJECT.html" title=object>object sender, EventArgs e) { Content contentObject = Application.Current.Host.Content; if (contentObject.IsFullScreen) { toggleButton.Background = new SolidColorBrush(Colors.Green); toggleButton.Content = "Full Screen Mode"; } else { toggleButton.Background = new SolidColorBrush(Colors.Red); toggleButton.Content = "Normal Mode"; } } }
结束语
本文简单介绍了Silverlight 2中对于全屏模式的支持,你可以从这里下载本文示例代码。