2012-10-17 55 views
7

我已經在Expression Blend中定義了以下兩種狀態。我一直在嘗試遵循this指南,但當我需要有關如何以及何時更改狀態的信息時,我覺得它會讓我懸掛。如何更改WP7中的VisualState

enter image description here

據我附上行爲指南(我假設「GotoState函數」)我UserControl - 不幸的是,我不認爲我其實有User Control - 即使我做了,我會必須將行爲附加到我的PortraitState和我的LandscapeState

看來我可以附加GotoState到我的LayoutRoot元素。那麼,我是否將自己的行爲與這兩種狀態聯繫在一起?任何幫助將不勝感激。

*編輯:我在玩我的xaml.cs文件,並認爲這可能是以編程方式執行它的方式。當調試和改變方向時,我輸入我的開關盒並找到正確的方向。但是,國家從未轉換。我究竟做錯了什麼?

protected override void OnOrientationChanged(OrientationChangedEventArgs e) 
    { 
     switch (e.Orientation) 
     { 
      case PageOrientation.Landscape: 
       ExtendedVisualStateManager.GoToElementState(root:this.LayoutRoot, stateName: "LandscapeState", useTransitions: true); 
       break; 
      case PageOrientation.LandscapeRight: 
       ExtendedVisualStateManager.GoToElementState(root:this.LayoutRoot, stateName: "LandscapeState", useTransitions: true); 
       break; 
      case PageOrientation.LandscapeLeft: 
       ExtendedVisualStateManager.GoToElementState(root:LayoutRoot, stateName: "LandscapeState", useTransitions: true); 
       break; 
      case PageOrientation.Portrait: 
       ExtendedVisualStateManager.GoToElementState(root:this.LayoutRoot, stateName: "PortraitState", useTransitions: true); 
       break; 
      case PageOrientation.PortraitUp: 
       ExtendedVisualStateManager.GoToElementState(root:this.LayoutRoot, stateName: "PortraitState", useTransitions: true); 
       break; 
      case PageOrientation.PortraitDown: 
       ExtendedVisualStateManager.GoToElementState(root:this.LayoutRoot, stateName: "PortraitState", useTransitions: true); 
       break; 
      default: 
       break; 
     } 
    } 

EDIT2:在嘗試上述看來,GotoElementState返回false,並根據MSDN:「如果控制成功過渡到新的狀態返回true;否則爲false。」

現在我留下了一個問題:什麼會導致我的狀態轉換失敗?

回答

1

我設法通過執行以下操作,以找到一個解決我自己的問題。

事實證明,使用ExtendedVisualStateManager.GotoElementState(UIElement,String,bool)目前不能很好地工作,所以我不得不尋找一種使用VisualStateManager.GotoState的方法。

我解決我的問題通過簡單的包裝我的用戶控件LayoutRoot這樣:現在

<UserControl x:Name="userControl"> 
    <Grid x:Name="LayoutRoot" Background="Transparent"> 
     <VisualStateManager.VisualStateGroups> 
    ... 
</UserControl> 

開關狀態是簡單地調用VisualStateManager.GotoState的問題,因爲我最初試圖這樣做。

protected override void OnOrientationChanged(OrientationChangedEventArgs e) 
    { 
     base.OnOrientationChanged(e); 

     switch (e.Orientation) 
     { 
      case PageOrientation.Landscape: 
      case PageOrientation.LandscapeRight: 
      case PageOrientation.LandscapeLeft: 
       VisualStateManager.GoToState(control: userControl, 
                 stateName: "LandscapeState", 
                 useTransitions: true); 
       break; 
      case PageOrientation.Portrait: 
      case PageOrientation.PortraitUp: 
      case PageOrientation.PortraitDown: 
       VisualStateManager.GoToState(control: userControl, 
                 stateName: "PortraitState", 
                 useTransitions: true); 
       break; 
      default: 
       VisualStateManager.GoToState(control: userControl, 
            stateName: "PortraitState", 
            useTransitions: true); 
       break; 
     } 
    } 
1

變化的VisualState在WP7這樣的:

switch (e.Orientation) 
    { 
     case PageOrientation.Landscape: 

      VisualStateManager.GoToState(this, "LandscapeState", true); 

      break; 

     case PageOrientation.Portrait: 

      VisualStateManager.GoToElementState(this,"PortraitState", true); 

      break; 

     default: 

      break; 
    } 

相關問題