全屏模式下处理Silverlight控件的两种方式
2010-08-31 12:46
351 查看
Silverlight插件支持全屏模式,这个没什么好说的,只需要用设置IsFullScreen属性即可,问题在于全屏模式中,尽管屏幕变大了,但是页面中的控件并未相应的变大。
第1种方式,即应用图片的Stretch属性:
<Grid x:Name="LayoutRoot" Background="White">
<Image Stretch="UniformToFill" Source="/FullScreenModel;component/Koala.jpg" />
<Button Content="全屏" Name="button1" Click="button1_Click" />
</Grid>
Click事件代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
}
这里主要是将Image的Stretch属性设置为UniformToFill,这样图片就可以根据浏览器分辨率的变化而变化,这种方式在处理图片,视频等资源时比较方便,不过使用这种方式在插入模式下使用图片时,你需要进行一些处理,因为若你在Image中指定Width或Height,图片在全屏模式下会保持这个固定的大小。
第2种方式则在后台进行处理
当处于全屏模式时,该页面上的控件也进行变化,以Button为例。这种方式或许更贴近我们平常接触的全屏,我们看看这部分的实现:
<Grid.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1" x:Name="RootLayoutScaleTransform">
</ScaleTransform>
</Grid.RenderTransform>
<Button Name="button1" Content="全屏" Height="30" Width="50" Click="button1_Click" Margin="70,170,72,100">
</Button>
这里在UI中添加了一个名为RootLayoutScaleTransform的放大转换,后台代码主要是根据插件的Resized,FullScreenChanged事件进行处理的,所以我们在构造函数中声明。
Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);
Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_Resized);
完整的代码:
private double width;
private double height;
public double uniformScaleAmount = 1;
public MainPage()
{
InitializeComponent();
height = this.Height;
width = this.Width;
Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);
Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_Resized);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
}
void Content_Resized(object sender, EventArgs e)
{
double currentWidth = Application.Current.Host.Content.ActualWidth;
double currentHeight = Application.Current.Host.Content.ActualHeight;
uniformScaleAmount = Math.Min((currentWidth / width), (currentHeight /height));
RootLayoutScaleTransform.ScaleX = uniformScaleAmount;
RootLayoutScaleTransform.ScaleY = uniformScaleAmount;
}
页面初始化后我们先将当前插件的大小保存了下来,当单击Button发生全屏事件时,会进行相关事件的处理,这种方式我觉得处理的更为妥善一些,程序运行的时候,如果你的界面上什么都没有,需要设置UserControl的Width,Height属性。
第1种方式,即应用图片的Stretch属性:
<Grid x:Name="LayoutRoot" Background="White">
<Image Stretch="UniformToFill" Source="/FullScreenModel;component/Koala.jpg" />
<Button Content="全屏" Name="button1" Click="button1_Click" />
</Grid>
Click事件代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
}
这里主要是将Image的Stretch属性设置为UniformToFill,这样图片就可以根据浏览器分辨率的变化而变化,这种方式在处理图片,视频等资源时比较方便,不过使用这种方式在插入模式下使用图片时,你需要进行一些处理,因为若你在Image中指定Width或Height,图片在全屏模式下会保持这个固定的大小。
第2种方式则在后台进行处理
当处于全屏模式时,该页面上的控件也进行变化,以Button为例。这种方式或许更贴近我们平常接触的全屏,我们看看这部分的实现:
<Grid.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1" x:Name="RootLayoutScaleTransform">
</ScaleTransform>
</Grid.RenderTransform>
<Button Name="button1" Content="全屏" Height="30" Width="50" Click="button1_Click" Margin="70,170,72,100">
</Button>
这里在UI中添加了一个名为RootLayoutScaleTransform的放大转换,后台代码主要是根据插件的Resized,FullScreenChanged事件进行处理的,所以我们在构造函数中声明。
Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);
Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_Resized);
完整的代码:
private double width;
private double height;
public double uniformScaleAmount = 1;
public MainPage()
{
InitializeComponent();
height = this.Height;
width = this.Width;
Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);
Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_Resized);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
}
void Content_Resized(object sender, EventArgs e)
{
double currentWidth = Application.Current.Host.Content.ActualWidth;
double currentHeight = Application.Current.Host.Content.ActualHeight;
uniformScaleAmount = Math.Min((currentWidth / width), (currentHeight /height));
RootLayoutScaleTransform.ScaleX = uniformScaleAmount;
RootLayoutScaleTransform.ScaleY = uniformScaleAmount;
}
页面初始化后我们先将当前插件的大小保存了下来,当单击Button发生全屏事件时,会进行相关事件的处理,这种方式我觉得处理的更为妥善一些,程序运行的时候,如果你的界面上什么都没有,需要设置UserControl的Width,Height属性。
相关文章推荐
- MSComm两种方式处理通信控件
- python文件处理,b模式的读写,rb, wb,编码的两种方式
- 处理事件的方式:两种类的覆盖处理(自己管理,覆盖专用事件函数;自己统一管理,覆盖QWidget::Event通用函数),一种对象的处理(父控件统一管理,即安装过滤器),两种全局处理(QCoreApplication安装过滤器;覆盖notify方法)
- 活动被回收的处理、活动的启动模式、一键退出所有活动、UI控件、四种布局方式、自定义控件
- JSP错误页面处理的两种方式
- (82)IO概述、字符流写入、异常处理、续写、两种读取方式、读写练习
- 事件处理之一:两种方式:监听器与回调
- sqlserver 处理输入的xml数据的两种方式
- Spring 单例模式的两种注入方式
- 【Android游戏开发之十】(优化处理)详细剖析Android Traceview 效率检视工具!分析程序运行速度!并讲解两种创建SDcard方式!
- 问题记录 横屏模式输入法全屏遮挡问题处理
- 两种高效的事件处理模式 reacotr模式和proactor模式
- 服务器程序框架之3.两种高效的事件处理模式
- <单例模式的两种方式>
- 一步一步学Silverlight 2系列(7):全屏模式支持
- (已测试)在本地处理模式下将业务对象数据源与 ReportViewer Web 服务器控件一起使用
- Silverlight显示控件换行的几种方式
- (字符串的模式模式匹配4.7.9)POJ 1782 Run Length Encoding(具有重复字符的字符串的处理方式)
- listview中的radiobutton实现单选的两种方式而且可以控制另一控件button的是否可见
- MFC中怎样将位图写入指定的Picture控件中,两种方式:1 加载已有位图 2读取位图文件;另外CreateDIBitmap的用法和StretchDIBits用法举例