使用带有 Stretch = UniformToFill 的图像控制 - WP7
我在页面上放置了一个图像,如下所示,
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Name="im" Source="/images/hqdefault.jpg" Height="250" Stretch="UniformToFill" VerticalAlignment="Center"/>
</Grid>
这是整个页面 XAML,图像可以从 http://i.ytimg.com/vi/wNKKCHv-oOw/hqdefault.jpg
后面的代码包含一些处理 PageOrientation_Change 的逻辑
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
if (Orientation == PageOrientation.Landscape ||
Orientation == PageOrientation.LandscapeLeft ||
Orientation == PageOrientation.LandscapeRight)
{
im.Height = Application.Current.Host.Content.ActualWidth;
im.Width = Application.Current.Host.Content.ActualHeight;
im.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
im.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
}
else
{
im.Height = 250;
im.Width = Application.Current.Host.Content.ActualWidth;
}
}
如果有人可能尝试这样做,他/她可能会发现 StrechToFill 从底部裁剪图像的内容,正如我期望它从顶部和底部均匀裁剪图像内容一样,并将图像内容保持在图像控件的中心。
希望我已经说清楚了,如果没有,请考虑根据提供的代码制作一个示例。多谢。
I have an image placed on a Page as follow
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Name="im" Source="/images/hqdefault.jpg" Height="250" Stretch="UniformToFill" VerticalAlignment="Center"/>
</Grid>
this is the whole page XAML, image can be downloaded from http://i.ytimg.com/vi/wNKKCHv-oOw/hqdefault.jpg
Code behind contains some logic to handle the PageOrientation_Change
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
if (Orientation == PageOrientation.Landscape ||
Orientation == PageOrientation.LandscapeLeft ||
Orientation == PageOrientation.LandscapeRight)
{
im.Height = Application.Current.Host.Content.ActualWidth;
im.Width = Application.Current.Host.Content.ActualHeight;
im.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
im.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
}
else
{
im.Height = 250;
im.Width = Application.Current.Host.Content.ActualWidth;
}
}
If some one may try this he/she may find that StrechToFill crops the content of the image from bottom where as I am expecting it to crop it from top and bottom equally and keep the image content centered within image control.
HOpe I have made myself clear if not please consider making a sample from provided code. Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主要问题是图像控件上的高度或宽度的设置,我现在很清楚不要在图像控件或媒体元素上给出高度或宽度。如果您需要固定高度(例如在纵向模式下),您可以将其放入网格控件中并设置其高度或宽度。以下是对我有用的代码。
如果您想将此图片更改为横向模式全屏,请执行以下代码
Main problem was the setting of height or width on the Image control, i am now well aware not to give heigh or width on the image control nor on media element. if you need a a fixed height for example in portrait mode you may put it in a grid control and set its height or width. following is the code which worked for me.
And following code if you want to change this picture to full screen in landscape mode
试试这个,然后你不需要在 PhoneApplicationPage_OrientationChanged 事件中执行任何操作。
try this then the u need not do anything in PhoneApplicationPage_OrientationChanged event.