2012-09-22 20 views
2

我有一個關於PhoneApplicationPage的可視化狀態管理的問題。基本上,可以使用VisualStateManager方法來設置頁面本身的狀態嗎?最後,它繼承了Control類,所以這個東西應該適用。可以爲PhoneApplicationPage設置可視狀態嗎?

我在問,因爲我試過了,失敗了。這裏是我的代碼:

<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); 
     } 
    } 

任何想法可能是錯誤的?

+0

從後嘗試下面 http://stackoverflow.com/questions/7139099/visualstatemanager-fails-to-start-animation一個類似的例子-on-usercontrol – Somnath

回答

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> 
+0

是的,我也發現把VisualStateManager放到頁面的某個子實體上是可以工作的,也就是說你可以說** VisualStateManager.GoToState(phone_app_page,「state」,true)**。不過,我覺得這很具誤導性。當您在Control實例(PhoneApplicationPage是什麼)上設置屬性時它不起作用,但在您將其設置爲非Control(Grid繼承Panel,而不是Control)時有效。有沒有人對這個設計背後的邏輯有所瞭解? – Haspemulator

+0

好的,這個答案至少給出瞭解決方法,所以我接受它。但是,對這個邏輯背後的某些原因進行解釋會很好。 – Haspemulator

相關問題