简体   繁体   English

使用带Stretch的图像控制= UniformToFill - WP7

[英]Using Image Control with Stretch = UniformToFill - WP7

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 这是整页XAML,图片可以从http://i.ytimg.com/vi/wNKKCHv-oOw/hqdefault.jpg下载

Code behind contains some logic to handle the PageOrientation_Change 代码隐藏包含一些处理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. 如果有人尝试这样做,他/她可能会发现StrechToFill从底部裁剪图像的内容,因为我期望它从顶部和底部均匀地裁剪,并保持图像内容在图像控制中居中。

HOpe I have made myself clear if not please consider making a sample from provided code. HOpe我已经明确表示,如果不是,请考虑从提供的代码中提取样本。 Thanks a lot. 非常感谢。

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. 主要问题是在Image控件上设置高度或宽度,我现在很清楚不会在图像控件上或媒体元素上给出高度或宽度。 if you need aa 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. 以下是适合我的代码。

<Grid Name="grdMedia"
      Grid.Row="1" 
      VerticalAlignment="Stretch" 
      HorizontalAlignment="Stretch"
      Height="250">
<Image Name="imThumbnail" 
       Grid.Row="1" 
       Stretch="UniformToFill" 
       HorizontalAlignment="Center"
       VerticalAlignment="Center"/>
</Grid>

And following code if you want to change this picture to full screen in landscape mode 如果您想在横向模式下将此图片更改为全屏,请按照以下代码操作

    private void SetUIInLandscape()
    {
        SystemTray.IsVisible = false;

       //Do not change the height or width of image control nor its alignments 
       //Hide every thing else

        grdMedia.Height = Application.Current.Host.Content.ActualWidth;
        grdMedia.Width = Application.Current.Host.Content.ActualHeight;

    }

    private void SetUIInPortrait()
    {
        SystemTray.IsVisible = true;

        //Do not change the height or width of image control nor its alignments
        //Make every thing else visible

        grdMedia.Height = 250;
        grdMedia.Width = Application.Current.Host.Content.ActualWidth;
    }
<Grid x:Name="LayoutRoot" Background="Transparent">
   <Grid.RowDefinitions>
     <RowDefinition Height="Auto"/>
     <RowDefinition Height="*"/>
  </Grid.RowDefinitions>

<Image Name="im" Source="/images/hqdefault.jpg" Height="Auto" HorizontalAlignment="Stretch"  VerticalAlignment="Center"/>
</Grid>

try this then the u need not do anything in PhoneApplicationPage_OrientationChanged event. 试试这个,然后你不需要在PhoneApplicationPage_OrientationChanged事件中做任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM