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 に答える