使silverlight适应IE窗口大小的方法

原文 http://10rem.net/blog/2008/07/04/how-to-resize-a-silverlight-2-app-and-keep-the-same-aspect-ratio

< UserControl  x:Class ="PeteBrown.SilverlightScalingExample.Page"

    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"

    
>

   

     < Grid  x:Name ="LayoutRoot"

          Background
="Cornsilk"  ShowGridLines ="True"

          Width
="400"  Height ="300"

          RenderTransformOrigin
="0.5 0.5" >

       

         < Grid.RenderTransform >

             < ScaleTransform  x:Name ="PageScale"  ScaleX ="1"  ScaleY ="1" />

         </ Grid.RenderTransform >

       



     </ Grid >

</ UserControl >

 


public  partial  class Page : UserControl

{

     //  this is the aspect ratio we want to maintain

    
//  you can specify this all sorts of ways, but the

    
//  easiest is to take the original size and divide

    
//  X by Y (4:3 or 1.333 in this case)

     private  const  double _originalWidth =  400;

     private  const  double _originalHeight =  300;

     private  const  double _originalAspectRatio =

        _originalWidth / _originalHeight;



     public Page()

    {

        InitializeComponent();



         //  wire up the event handler. This is a great addition

        
//  to silverlight, as you used to have to hook into the

        
//  browser event yourself

        SizeChanged +=  new SizeChangedEventHandler(Page_SizeChanged);

    }



     void Page_SizeChanged( object sender, SizeChangedEventArgs e)

    {

         if (e.NewSize.Width < _originalWidth ||

            e.NewSize.Height < _originalHeight)

        {

             //  don't shrink

            PageScale.ScaleX =  1.0;

            PageScale.ScaleY =  1.0;

        }

         else

        {

             //  resize keeping aspect ratio the same

             if (e.NewSize.Width / e.NewSize.Height > _originalAspectRatio)

            {

                 //  height is our constraining property

                PageScale.ScaleY = e.NewSize.Height / _originalHeight;

                PageScale.ScaleX = PageScale.ScaleY;

            }

             else

            {

                 //  either width is our constraining property, or the user

                
//  managed to nail our aspect ratio perfectly.

                PageScale.ScaleX = e.NewSize.Width / _originalWidth;

                PageScale.ScaleY = PageScale.ScaleX;

            }

        }

    }

}

 

转载于:https://www.cnblogs.com/Kingly/archive/2012/07/11/2586748.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值