Pete brown在它的BLOG中提到了一个按自定义比例使用silverlight适应IE窗口大小的方法
原文如下:How to Resize a Silverlight 2 App and Keep the Same Aspect Ratio
其核心代码如下( Xaml ):
原文如下:How to Resize a Silverlight 2 App and Keep the Same Aspect Ratio
其核心代码如下( Xaml ):
<
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 ="PageScale" ScaleX ="1" ScaleY ="1" />
</
…
Grid
UserControl >
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 ="PageScale" ScaleX ="1" ScaleY ="1" />
</
…
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 ;
_originalHeight 300 _originalAspectRatio
_originalWidth / _originalHeight;
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 < ||
e.NewSize.Height _originalHeight)
{
don't shrink PageScale.ScaleX 1.0 ;
PageScale.ScaleY ;
}
else
{
resize keeping aspect ratio the same e.NewSize.Height > _originalAspectRatio)
{
height is our constraining property PageScale.ScaleY _originalHeight;
PageScale.ScaleX PageScale.ScaleY;
}
{
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;
}
}
}
}
这是一个很有用的小技巧,所以在这里做一个记号,看看将来是否能用得上:)
{
// 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 ;
_originalHeight 300 _originalAspectRatio
_originalWidth / _originalHeight;
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 < ||
e.NewSize.Height _originalHeight)
{
don't shrink PageScale.ScaleX 1.0 ;
PageScale.ScaleY ;
}
else
{
resize keeping aspect ratio the same e.NewSize.Height > _originalAspectRatio)
{
height is our constraining property PageScale.ScaleY _originalHeight;
PageScale.ScaleX PageScale.ScaleY;
}
{
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;
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。