在Visual Studio 2012中,Windows 8.0应用程序曾经有过非常棒的设计器支持。在Device Panel中,您可以移动到页面上VisualStates
内的不同VisualStateManager.VisualStateGroups
将在设计时更新适当的属性。
在8.1中,VisualState
为no longer based on specific snapped layouts,但您仍然可以使用VisualStateManager
并根据屏幕width
进行更新。
Q :如何在设计时更新这些更改?
这是一个在运行时工作的变化的简单示例,但不是设计时。
MainView.xaml.vb :
Private Sub MainView_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
AddHandler Me.SizeChanged, AddressOf WindowSizeChanged
ApplyViewState(Me.Width, Me.Height)
End Sub
Private Sub WindowSizeChanged(sender As Object, e As SizeChangedEventArgs)
ApplyViewState(e.NewSize.Width, e.NewSize.Height)
End Sub
Private Sub ApplyViewState(ByVal viewWidth As Double, ByVal viewHeight As Double)
If viewWidth < 350 Then
VisualStateManager.GoToState(Me, "SmallLayout", True)
Else
VisualStateManager.GoToState(Me, "RegularLayout", True)
End If
End Sub
MainView.xaml :
<Page x:Name="PageRoot"
x:Class="WinStoreMvvmTemplate.View.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Background="Red" x:Name="ContentGrid">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="RegularLayout"/>
<VisualState x:Name="SmallLayout">
<Storyboard>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="ContentGrid"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="Blue"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</Page>
以下是设计时的视图:(不正确)
以下是运行时的视图:(正确)