2013-11-01 106 views
2

我在轉移到VS2013和Windows Store應用程序8.1後很難解決視覺狀態問題。我有一個AppBar「關於」按鈕的應用程序,將用戶帶到關於頁面。工作得很好。當視圖更改爲縱向時,我需要關於頁面上的堆疊面板更改方向。一直在這工作幾個小時,閱讀無數的網站與此代碼的確切副本,但我的行不通。有任何想法嗎?Windows Store應用程序8.1和VisualStateManager

這裏有AboutPage.cs C#代碼:

private void AboutPage_SizeChanged(object sender, SizeChangedEventArgs e) 
    { 
     if (e.NewSize.Height/e.NewSize.Width >= 1) 
     { 
      VisualStateManager.GoToState(this, "Portrait", true); 
     } 
     else 
     { 
      VisualStateManager.GoToState(this, "DefaultLayout", true); 
     } 
    } 

這裏有AboutPage.xaml的XAML:

<Page 
x:Name="pageRoot" 
x:Class="xxxxxxxxxxxxxxxxx.AboutPage" 
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:xxxxxxxxxxxxxxxxxx" 
xmlns:common="using:xxxxxxxxxxxxxxx.Common" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" SizeChanged="AboutPage_SizeChanged"> 

<Page.Resources> 
    <!-- TODO: Delete this line if the key AppName is declared in App.xaml --> 
    <!--<x:String x:Key="AppName">xxxxxxxxxxxxxxxxxxx</x:String>--> 
</Page.Resources> 

<!-- 
    This grid acts as a root panel for the page that defines two rows: 
    * Row 0 contains the back button and page title 
    * Row 1 contains the rest of the page layout 
--> 
<Grid x:Name="pageMainGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <Grid.ChildrenTransitions> 
     <TransitionCollection> 
      <EntranceThemeTransition/> 
     </TransitionCollection> 
    </Grid.ChildrenTransitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="140"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <!-- Back button and page title --> 
    <Grid x:Name="pageHeaderGrid"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="120"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 

     <StackPanel 
      x:Name="headerStackPanel" 
      Grid.ColumnSpan="3" 
      Background="#FF4617B4" 
      Orientation="Horizontal" > 
      <Button 
       x:Name="backButton" 
       Margin="39,59,39,0" 
       Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}" 
       Style="{StaticResource NavigationBackButtonNormalStyle}" 
       VerticalAlignment="Top" 
       AutomationProperties.Name="Back" 
       AutomationProperties.AutomationId="BackButton" 
       AutomationProperties.ItemType="Navigation Button"/> 
      <TextBlock 
       x:Name="pageTitle" 
       Style="{StaticResource HeaderTextBlockStyle}" 
       Grid.Column="1" 
       IsHitTestVisible="false" 
       TextWrapping="NoWrap" 
       VerticalAlignment="Bottom" 
       Margin="0,0,30,40" 
       Text="{StaticResource AppName}"/> 
     </StackPanel>    
    </Grid> 

    <Grid 
     x:Name="pageContentGrid" 
     Grid.Row="1" 
     Visibility="Visible"> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="20*"/> 
      <RowDefinition Height="142*"/> 
     </Grid.RowDefinitions> 

     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="150*"/> 
      <ColumnDefinition Width="372*"/> 
      <ColumnDefinition Width="150*"/> 
     </Grid.ColumnDefinitions> 

     <TextBlock 
      Grid.Column="1" 
      HorizontalAlignment="Stretch" 
      VerticalAlignment="Stretch" 
      Margin="0,0,0,0" 
      TextAlignment="Center" 
      Text="About" 
      FontSize="48" 
      FontFamily="Segoe UI"/> 

     <ScrollViewer 
      BorderThickness="0,2,0,0" 
      BorderBrush="DarkGray" 
      Grid.Row="1" 
      Grid.Column="1" 
      ScrollViewer.VerticalScrollBarVisibility="Hidden" 
      Margin="0"> 

      <StackPanel 
       Margin="40"> 

       <TextBlock 
        Text="xxxxxxxxxxxxxxxxxx" 
        FontSize="34" /> 

       <TextBlock 
        Text="by xxxxxxxxxx" 
        FontSize="24"/> 

       <StackPanel 
        Orientation="Vertical" 
        Margin="0,25,0,0"> 

        <TextBlock 
         Margin="0,6,0,0" 
         Padding="0,0,0,0" 
         Text="Website:" 
         FontSize="24" 
         VerticalAlignment="Center"/> 

        <HyperlinkButton 
         Margin="0,0,0,0" 
         Padding="-5,0,0,0" 
         FontSize="20" 
         NavigateUri="http://www.xxxxxxxxxxxxxx.com" 
         HorizontalAlignment="Left" 
         VerticalAlignment="Center" 
         Content="www.xxxxxxxxxxxxxxxxxx.com" /> 

       </StackPanel> 

       <StackPanel 
        x:Name="emailStack" 
        Orientation="Vertical" 
        Margin="0,25,0,0"> 

        <TextBlock 
         Margin="0,4,0,0" 
         Padding="0,0,0,0" 
         Text="Support Email:" 
         FontSize="24" 
         VerticalAlignment="Center"/> 

        <HyperlinkButton 
         Margin="0,0,0,0" 
         Padding="-5,0,0,0" 
         FontSize="20" 
         NavigateUri="mailto:[email protected]" 
         HorizontalAlignment="Left" 
         VerticalAlignment="Center" 
         Content="[email protected]" /> 

       </StackPanel> 

       <TextBlock 
        Margin="0,30,0,0" 
        Text="Feedback:" 
        FontSize="24"/> 

       <TextBlock 
        TextWrapping="Wrap" 
        Margin="0,10,0,10" 
        FontSize="20" > 

        Please take a few moments to rate and review my application. 
        Every little bit of encouragement and/or constructive feedback 
        is appreciated. 
        <LineBreak /><LineBreak /> 
        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 

       </TextBlock> 
      </StackPanel> 
     </ScrollViewer> 

     <Rectangle 
      Grid.Row="0" 
      Grid.Column="0" 
      Grid.RowSpan="2" 
      HorizontalAlignment="Stretch" 
      Fill="DarkGray" /> 

     <Rectangle 
      Grid.Row="0" 
      Grid.Column="2" 
      Grid.RowSpan="2" 
      HorizontalAlignment="Stretch" 
      Fill="DarkGray" />   
    </Grid> 

    <VisualStateManager.VisualStateGroups> 
     <VisualStateGroup> 
      <VisualState x:Name="DefaultLayout"> 
       <Storyboard> 
       </Storyboard> 
      </VisualState> 

      <VisualState x:Name="Portrait"> 
       <Storyboard> 
        <ObjectAnimationUsingKeyFrames 
         Storyboard.TargetProperty="(StackPanel.Orientation)" 
         Storyboard.TargetName="emailStack"> 
         <DiscreteObjectKeyFrame KeyTime="0"> 
          <DiscreteObjectKeyFrame.Value> 
           <Orientation>Horizontal</Orientation> 
          </DiscreteObjectKeyFrame.Value> 
         </DiscreteObjectKeyFrame> 
        </ObjectAnimationUsingKeyFrames> 
       </Storyboard> 
      </VisualState> 
     </VisualStateGroup> 
    </VisualStateManager.VisualStateGroups> 

</Grid>  

回答

1

VisualStateManager主控內的內容您的Page(通常爲GridStackPanel)應更改您的AppBar的佈局。試一試。

<Page 
    ... 
    SizeChanged="Page_SizeChanged"> 

    <Page.BottomAppBar> 
     <AppBar> 
      <StackPanel 
       x:Name="emailStack" 
       Orientation="Vertical" 
       Margin="0,25,0,0"> 

       <TextBlock 
        Margin="0,4,0,0" 
        Padding="0,0,0,0" 
        Text="Support Email:" 
        FontSize="24" 
        VerticalAlignment="Center"/> 

       <HyperlinkButton 
        Margin="0,0,0,0" 
        Padding="-5,0,0,0" 
        FontSize="20" 
        NavigateUri="mailto:[email protected]" 
        HorizontalAlignment="Left" 
        VerticalAlignment="Center" 
        Content="[email protected]" /> 
      </StackPanel> 
     </AppBar> 
    </Page.BottomAppBar> 

    <Grid> 
     <VisualStateManager.VisualStateGroups> 
      <VisualStateGroup> 
       <VisualState x:Name="DefaultLayout"> 
        <Storyboard> 
        </Storyboard> 
       </VisualState> 

       <VisualState x:Name="Portrait"> 
        <Storyboard> 
         <ObjectAnimationUsingKeyFrames 
          Storyboard.TargetProperty="(StackPanel.Orientation)" 
          Storyboard.TargetName="emailStack"> 
          <DiscreteObjectKeyFrame KeyTime="0"> 
           <DiscreteObjectKeyFrame.Value> 
            <Orientation>Horizontal</Orientation> 
           </DiscreteObjectKeyFrame.Value> 
          </DiscreteObjectKeyFrame> 
         </ObjectAnimationUsingKeyFrames> 
        </Storyboard> 
       </VisualState> 
      </VisualStateGroup> 
     </VisualStateManager.VisualStateGroups> 
    </Grid> 
</Page> 
+0

我使用您建議的更改後的代碼編輯了我的原始帖子。非常感謝你幫助,但你的建議並沒有辦法。 – dmiannay

+0

我忘了'StackPanel'是在AppBar中。我更新了我的答案。 – kiewic

+0

其實,我的StackPanel不在AppBar內。我已經更新了我上面的原始帖子,以顯示AboutPage.xaml文件的全部內容,以便您可以看到整個安排。改變這個StackPanel的方向只是一個測試,看看我能否得到這個工作。實際上我想要做更多的事情,但我只想讓VisualStateManager首先正常工作。謝謝你的幫助! – dmiannay

3

此需要強調,因爲這就是答案:

好了,終於...問題解決了!訣竅是將VisualStateManager XAML塊定位在包含要更改屏幕大小的控件時要使用的控件的網格之後。我修改了AboutPage XALM塊在我原來的職位,以顯示正確的配置

這是.NET 4.5.1重大更改,您可以在被影響的控件之前放置VisualStateManager。

相關問題