Windows应用商店应用8.1和VisualStateManager

时间:2013-11-01 21:43:28

标签: visual-studio windows-8.1

迁移到VS2013和Windows Store Apps for 8.1后,我很难解决视觉状态问题。我有一个带有AppBar“关于”按钮的应用程序,可以将用户带到“关于”页面。工作得很好。我希望“关于”页面上的堆栈面板在视图更改为“纵向”时更改方向。已经工作了好几个小时,并阅读了无数的网站与这些代码的精确复制品,但我的工作将无法正常工作。有什么想法吗?

这是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:xxxxxxxxxxxxxx@gmail.com"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Center"
                        Content="xxxxxxxxxxxxxxxxx@gmail.com" />

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

2 个答案:

答案 0 :(得分:3)

这需要突出显示,因为答案是:

  

好的,终于......问题解决了!诀窍是在更改屏幕大小时,在包含要修改的控件的Grid之后找到VisualStateManager XAML块。我在原始帖子中修改了AboutPage XALM块以显示正确的配置

这是对.NET 4.5.1的重大更改,您可以将VisualStateManager置于受影响的控件之前。

答案 1 :(得分:1)

VisualStateManager放在主要内容中Page(通常为GridStackPanel)的内容应更改{{1}的布局}。试一试。

AppBar