2012-09-25 54 views
1

我想寫一個自定義控件,並希望它具有不同的填充,如果該頁面是肖像或捕捉。我注意到,網頁從LayoutAwarePage繼承它創建以下視圖狀態支持:自定義佈局感知控件

  • FullScreenLandscape
  • 填充
  • FullScreenPortrait

我是否需要類似的代碼添加到我的新的控件(它繼承自Control)。如果沒有,爲什麼LayoutAwarePage必須這樣做?另外,我可以將下面的VisualStateManager粘貼到控件的ControlTemplate中,並讓它尊重頁面佈局,或者這太容易了。

<VisualStateGroup x:Name="ApplicationViewStates"> 
    <VisualState x:Name="FullScreenLandscape"/> 
    <VisualState x:Name="Filled"/> 
    <VisualState x:Name="FullScreenPortrait"> 
     <Storyboard> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Padding"> 
       <DiscreteObjectKeyFrame KeyTime="0" Value="1,2,3,4"/> 
      </ObjectAnimationUsingKeyFrames> 
     </Storyboard> 
    </VisualState> 
    <VisualState x:Name="Snapped"> 
     <Storyboard> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Padding"> 
       <DiscreteObjectKeyFrame KeyTime="0" Value="5,6,7,8"/> 
      </ObjectAnimationUsingKeyFrames> 
     </Storyboard> 
    </VisualState> 
</VisualStateGroup> 

編輯:它看起來像控件默認情況下不支持這些國家,他們必須增加。它也表明ButtonBase確實支持這些狀態,因爲它在它的樣式中使用它們。

回答

1

我用我的用戶控件做的是讓他們繼承LayoutAwarePage。接下來將內容放置在網格中,並將VisualStateGroup移動到此網格中。希望這有助於,它對我有用。

2

如果您在Windows 8項目中查看默認的「詳細信息」頁面,您會發現它希望您訂閱頁面上的事件。你會發現內flipview的ItemTemplate如下:

<FlipView.ItemTemplate> 
    <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates"> 
     <ScrollViewer x:Name="scrollViewer" Style="{StaticResource HorizontalScrollViewerStyle}" 
      <!-- "Child Controls Here" --> 
       <VisualStateManager.VisualStateGroups> 
        <!-- "Visual states manuiplating the child controls above" --> 
      </VisualStateManager.VisualStateGroups> 
     </ScrollViewer> 
    </UserControl> 
</FlipView.ItemTemplate> 
+1

尼斯一個 - 這實際上看起來是正確的答案。 –