如何在页面方向更改时以全屏方式显示XAML元素?

在我的Windows Phone 8.1 RT应用程序中,我在透视控制中有一个图像。

当页面方向改变为垂直时,我想在全屏中显示元素。

任何帮助将不胜感激。

我的XAML如下所示。

<Grid x:Name="ContentPanel"> <Pivot Title="{Binding ItemTitle}"> <PivotItem Header="Overview"> <StackPanel Orientation="Vertical"> <Image x:Name="MainImage" Source="{Binding ImageURL}"/> <TextBlock x:Name="TitleTextBlock" Text={Binding Title}"/> </StackPanel> </PivotItem> <PivotItem Header="Details"> <ScrollViewer> <TextBlock x:Name="DetailsTextBlock" Text="{Binding Details}"/> </ScrollViewer> </PivotItem> </Pivot> </Grid> 

我试图在页面方向改变为横向时在全屏幕中显示图像控件MainImage ,并且当方向改变为纵向时将其恢复到正常的透视图。

您无法将透视视图内容展开为完整区域,以便占据整个区域。 一个解决方案是使用一个单独的图像控件,并使其隐藏,并在方向变化显示此图像,隐藏网格副视图肖像的诗句。 这里是如何

你的页面Xaml

 <Grid Name="MainPage"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="OrientationManager"> <VisualState x:Name="Portrate"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ContentPanel"> <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="MainImage1"> <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Landscape"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ContentPanel"> <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="MainImage1"> <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Grid x:Name="ContentPanel"> <Pivot Title="{Binding ItemTitle}"> <PivotItem Header="Overview"> <StackPanel Orientation="Vertical"> <Image x:Name="MainImage" Source="{Binding ImageURL}"/> <TextBlock x:Name="TitleTextBlock" Text="asfsadf"/> </StackPanel> </PivotItem> <PivotItem Header="Details"> <ScrollViewer> <TextBlock x:Name="DetailsTextBlock" Text="{Binding Details}"/> </ScrollViewer> </PivotItem> </Pivot> </Grid> <Image Source="Assets\WideLogo.scale-14012.png" Visibility="Collapsed" Name="MainImage1"></Image> </Grid> 

确保VisualStateManager元素位于页面的根控件/网格中

代码在你的页面构造函数中。

  DisplayInformation di = DisplayInformation.GetForCurrentView(); di.OrientationChanged += di_OrientationChanged; if (di.CurrentOrientation == DisplayOrientations.Landscape || di.CurrentOrientation == DisplayOrientations.LandscapeFlipped) { VisualStateManager.GoToState(this, "Landscape", true); } else { VisualStateManager.GoToState(this, "Portrate", true); } 

方向更改了事件处理程序

  void di_OrientationChanged(DisplayInformation sender, object args) { if (sender.CurrentOrientation == DisplayOrientations.Landscape || sender.CurrentOrientation == DisplayOrientations.LandscapeFlipped) { VisualStateManager.GoToState(this, "Landscape", true); } else { VisualStateManager.GoToState(this, "Portrate", true); } } 

编辑:

 <Grid Name="MainPage"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="OrientationManager"> <VisualState x:Name="Portrate"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="PivotHeaderText1"> <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="PivotHeaderText2"> <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Landscape"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="PivotHeaderText1"> <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="PivotHeaderText2"> <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Grid x:Name="ContentPanel"> <Pivot Title="{Binding ItemTitle}"> <PivotItem> <PivotItem.Header> <TextBlock Text="Overview" x:Name="PivotHeaderText1" ></TextBlock> </PivotItem.Header> <StackPanel Orientation="Vertical"> <MediaElement AutoPlay="True" x:Name="player" /> <TextBlock x:Name="TitleTextBlock" Text="asfsadf"/> </StackPanel> </PivotItem> <PivotItem> <PivotItem.Header> <TextBlock Text="Overview" x:Name="PivotHeaderText2" ></TextBlock> </PivotItem.Header> <ScrollViewer> <TextBlock x:Name="DetailsTextBlock" Text="{Binding Details}"/> </ScrollViewer> </PivotItem> </Pivot> </Grid> </Grid> 

希望能帮助到你。