2

迁移到 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>    

4

2 回答 2

3

这需要强调,因为它是答案:

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

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

于 2014-05-14T19:17:33.620 回答
1

VisualStateManager主控件放在您的内容中Page(通常是 aGrid或 a StackPanel)应该会改变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:xxxxxxxxxx@gmail.com"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center"
                    Content="xxxxxxxxxxx@gmail.com" />
            </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>
于 2013-11-02T00:03:08.700 回答