silverlight 全屏后 元素跟着放大

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

第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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值