【问题标题】:Can one set visual state for PhoneApplicationPage?可以为 PhoneApplicationPage 设置一个视觉状态吗?
【发布时间】:2012-09-24 19:40:51
【问题描述】:

我有一个关于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);
        }
    }

有什么想法可能是错的吗?

【问题讨论】:

标签: windows-phone-7 visualstatemanager


【解决方案1】:

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>

【讨论】:

  • 是的,我还发现将 VisualStateManager 放在页面的某个子实体中可以使事情正常进行,即您可以说 VisualStateManager.GoToState(phone_app_page, "state", true)乙>。但是,我发现这非常具有误导性。当您在 Control 实例(PhoneApplicationPage 是什么)上设置属性时,它不起作用,但当您将其设置为非 Control(Grid 继承 Panel,而不是 Control)时,它会起作用。有人知道这个设计背后的逻辑吗?
  • 好的,这个答案至少给出了解决方法,所以我接受它。但是,最好能对这种逻辑背后的某些原因进行一些解释。
猜你喜欢
  • 1970-01-01
  • 2021-11-29
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
  • 2021-12-19
  • 1970-01-01
相关资源
最近更新 更多