4

我在页面上放置了一张图片,如下所示

<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 从底部裁剪图像的内容,因为我期望它从顶部和底部均匀地裁剪它并保持图像内容在图像控件中居中。

希望我已经说清楚了,如果不是,请考虑从提供的代码中制作一个示例。非常感谢。

4

2 回答 2

8

主要问题是图像控件上的高度或宽度设置,我现在很清楚不要在图像控件或媒体元素上给出高度或宽度。如果你需要一个固定的高度,例如在纵向模式下,你可以把它放在一个网格控件中并设置它的高度或宽度。以下是对我有用的代码。

<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>

如果您想在横向模式下将此图片更改为全屏,请使用以下代码

    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;
    }
于 2012-03-09T07:07:08.807 回答
3
<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>

试试这个然后你不需要在 PhoneApplicationPage_OrientationChanged 事件中做任何事情。

于 2011-12-26T07:21:31.153 回答