|
概述
Silverlight 2 Beta 1版本發(fā)布了,無(wú)論從Runtime還是Tools都給我們帶來(lái)了很多的驚喜,如支持框架語(yǔ)言Visual Basic, Visual C#, IronRuby, IronPython,對(duì)JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步學(xué)Silverlight 2系列》文章帶您快速進(jìn)入Silverlight 2開(kāi)發(fā)。
本文為系列文章第七篇,介紹如何在Silverlight 2中使用全屏模式。
實(shí)現(xiàn)全屏模式
全屏模式有時(shí)候是非常有用的,在Silverlight中,提供了很好的支持。實(shí)現(xiàn)起來(lái)也非常的簡(jiǎn)單,其實(shí)只有一行代碼,編寫一個(gè)簡(jiǎn)單的XAML。
<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="image" Source="smile_6.png" Canvas.Top="100" Canvas.Left="40"></Image></Canvas>
引入命名空間
using System.Windows.Interop;
在按鈕單擊事件中添加實(shí)現(xiàn)代碼。
private void toggleButton_Click(object sender, RoutedEventArgs e){ Content contentObject = Application.Current.Host.Content; contentObject.IsFullScreen = !contentObject.IsFullScreen;}
獲取當(dāng)前的Silverlight插件“Content”對(duì)象,并設(shè)置IsFullScreen屬性。運(yùn)行后單擊按鈕將會(huì)變?yōu)槿聊J剑俅螁螕舭粹o(或者按Esc鍵)返回普通模式。
捕獲相關(guān)事件
有時(shí)候,我們需要在全屏模式和普通模式之間切換時(shí),添加一個(gè)其它的代碼,這時(shí)可以使用事件FullScreenChanged。
public Page(){ InitializeComponent(); Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);}
private void Content_FullScreenChanged(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"; }}
在普通模式和全屏模式之間切換時(shí),改變按鈕的背景色和文字。運(yùn)行后點(diǎn)擊按鈕:
切換為普通模式:
完整的代碼如下:
public partial class Page : UserControl{ public Page() { InitializeComponent(); Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged); } private void toggleButton_Click(object sender, RoutedEventArgs e) { Content contentObject = Application.Current.Host.Content; contentObject.IsFullScreen = !contentObject.IsFullScreen; } private void Content_FullScreenChanged(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"; } }}
結(jié)束語(yǔ)
本文簡(jiǎn)單介紹了Silverlight 2中對(duì)于全屏模式的支持,你可以從這里下載本文示例代碼。
NET技術(shù):一步一步學(xué)Silverlight :全屏模式支持,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。