SilverLight实现全屏模式与嵌入模式的切换
时间:2011-04-11 来源:悠閒
Silverlight 有两种模式:第一种为嵌入式,第二种为全屏模式。
前台代码:
<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" HorizontalAlignment="Center">
<Grid x:Name="LayoutRoot" Background="White">
<Button Height="150" Content="嵌入模式" Name="button1" Width="300" Click="button1_Click" FontSize="20"></Button>
</Grid>
</UserControl>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interop; //注意要引入这个命名空间
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
//在全屏或嵌入之间切换时,会触发一个事件,可以利用这个事件进行一些操作
Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//实现全屏模式需要引用其页面宿主程序,并设置为true
//Application.Current.Host.Content.IsFullScreen = true;
//如果想实现单按钮的两种模式之间切换,如下设置就可以了
Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
//或这样写
//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)
{
button1.Content = "全屏模式";
}
else
{
button1.Content = "嵌入模式";
}
}
}
}