全屏模式下处理Silverlight控件的两种方式

开发 后端 前端
文章作者介绍在Silverlight中全屏处理的两种方式,第1种方式,即应用图片的Stretch属性;第2种方式则在后台进行处理。

Silverlight插件支持全屏模式,这个没什么好说的,只需要用设置IsFullScreen属性即可,问题在于全屏模式中,尽管屏幕变大了,但是页面中的控件并未相应的变大。

51CTO推荐专题:走向银光 一步一步学Silverlight

第1种方式,即应用图片的Stretch属性:

  1. <Grid x:Name="LayoutRoot" Background="White"> 
  2. <Image Stretch="UniformToFill" Source="/FullScreenModel;component/Koala.jpg" /> 
  3. <Button Content="全屏"  Name="button1"  Click="button1_Click" /> 
  4. </Grid> 

Click事件代码:

  1. private void button1_Click(object sender, RoutedEventArgs e)  
  2.      {  
  3.          Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;  
  4.      } 

这里主要是将Image的Stretch属性设置为UniformToFill,这样图片就可以根据浏览器分辨率的变化而变化,这种方式在处理图片,视频等资源时比较方便,不过使用这种方式在插入模式下使用图片时,你需要进行一些处理,因为若你在Image中指定Width或Height,图片在全屏模式下会保持这个固定的大小。

第2种方式则在后台进行处理

当处于全屏模式时,该页面上的控件也进行变化,以Button为例。这种方式或许更贴近我们平常接触的全屏,我们看看这部分的实现:

Button 

Button

  1. <Grid.RenderTransform> 
  2.             <ScaleTransform ScaleX="1" ScaleY="1" x:Name="RootLayoutScaleTransform"> 
  3.             </ScaleTransform> 
  4.         </Grid.RenderTransform> 
  5.        <Button  Name="button1" Content="全屏" Height="30" Width="50" Click="button1_Click" Margin="70,170,72,100">           
  6.         </Button> 

这里在UI中添加了一个名为RootLayoutScaleTransform的放大转换,后台代码主要是根据插件的Resized,FullScreenChanged事件进行处理的,所以我们在构造函数中声明。

  1. Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);  
  2. Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_Resized);
  3.  

完整的代码:

  1. private double width;  
  2.         private double height;  
  3.         public double uniformScaleAmount = 1;  
  4.         public MainPage()  
  5.         {  
  6.             InitializeComponent();  
  7.              height = this.Height;  
  8.              width = this.Width;  
  9.             Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);  
  10.             Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_Resized);  
  11.         }  
  12.         private void button1_Click(object sender, RoutedEventArgs e)  
  13.         {  
  14.             Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;  
  15.         }  
  16.         void Content_Resized(object sender, EventArgs e)  
  17.         {  
  18.             double currentWidth = Application.Current.Host.Content.ActualWidth;  
  19.             double currentHeight = Application.Current.Host.Content.ActualHeight;  
  20.             uniformScaleAmount = Math.Min((currentWidth / width), (currentHeight /height));  
  21.             RootLayoutScaleTransform.ScaleX = uniformScaleAmount;  
  22.             RootLayoutScaleTransform.ScaleY = uniformScaleAmount;  
  23.         }  

页面初始化后我们先将当前插件的大小保存了下来,当单击Button发生全屏事件时,会进行相关事件的处理,这种方式我觉得处理的更为妥善一些,程序运行的时候,如果你的界面上什么都没有,需要设置UserControl的Width,Height属性。

原文地址:http://www.cnblogs.com/626498301/archive/2010/08/26/1808883.html

【编辑推荐】

  1. 简单Silverlight应用程序五步走
  2. Silverlight基础属性:依赖与附加
  3. 细数Silverlight 4的十二大引人注目新特性
  4. Silverlight 4中XAML解析的变化
  5. F#终于支持Silverlight 4 四大新特性一览
责任编辑:王晓东 来源: 博客园
相关推荐

2009-08-17 17:28:23

C#转义字符

2009-08-19 17:30:38

C#转义字符

2011-03-03 10:26:04

Pureftpd

2021-05-27 10:57:01

TCP定时器网络协议

2010-08-06 09:38:11

Flex读取XML

2023-03-29 13:06:36

2009-06-29 18:11:40

JSP设计模式

2010-09-07 11:09:59

2009-06-25 13:43:00

Buffalo AJA

2010-10-21 16:24:18

sql server升

2009-09-08 15:22:20

Spring依赖注入

2011-04-02 09:48:38

深拷贝

2010-07-15 14:38:55

Perl eval函数

2016-11-07 09:02:02

Malloc内存syscall

2011-06-16 10:02:08

JAVA静态载入

2010-10-20 15:48:56

SQL Server许

2010-08-03 13:27:04

FlexBuilder

2024-02-04 09:24:45

MyBatisSQL语句Spring

2021-12-08 10:47:35

RabbitMQ 实现延迟

2009-12-30 14:22:12

Silverlight
点赞
收藏

51CTO技术栈公众号