可以为PhoneApplicationPage设置可视状态吗?

时间:2012-09-22 20:44:25

标签: windows-phone-7 visualstatemanager

我对PhoneApplicationPage的可视状态管理有疑问。基本上,可以使用VisualStateManager方法来设置页面本身的状态吗?最后,它继承了Control类,所以这个东西应该适用。

我问,因为我尝试过但都失败了。这是我的代码:

<phone:PhoneApplicationPage 
x:Class="Encountered.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="Common">
        <VisualState x:Name="State1">
            <Storyboard>
                <DoubleAnimation Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.Opacity)" To="1" Duration="0"/>
            </Storyboard>
        </VisualState>
        <VisualState x:Name="State2">
            <Storyboard>
                <DoubleAnimation Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.Opacity)" To="0" Duration="0"/>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

<StackPanel Orientation="Vertical">
    <HyperlinkButton x:Name="button" NavigateUri="/Views/EditPage.xaml" Content="Go"/>
    <Button Click="Button_Click">state</Button>
</StackPanel>


</phone:PhoneApplicationPage>

在代码隐藏中:

public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            VisualStateManager.GoToState(this, "State1", true);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "State2", true);
        }
    }

任何想法可能出错?

1 个答案:

答案 0 :(得分:0)

VisualStateManager允许您指定控件何时进入特定状态。

因此,只需将所有内容包装在网格中:

<Grid>
    <VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="Common">
        <VisualState x:Name="State1">
            <Storyboard>
                <DoubleAnimation Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.Opacity)" To="1" />
            </Storyboard>
        </VisualState>
        <VisualState x:Name="State2">
            <Storyboard>
                <DoubleAnimation Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.Opacity)" To="0" />
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

<StackPanel Orientation="Vertical">
    <HyperlinkButton x:Name="button" NavigateUri="/Views/EditPage.xaml" Content="Go" />
    <Button Click="Button_Click" >state</Button>
</StackPanel>
</Grid>