我第一次使用VisualStateManager工作。不幸的是,我没有让它运转起来。
这是我的XAML代码:
<SplitView x:Name="spltvw" IsPaneOpen="False" DisplayMode="CompactInline"
CompactPaneLength="48" Grid.Row="1" PaneBackground="#FFDADADA">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="lowerThan7State">
<VisualState.Setters>
<Setter Target="spltvw.DisplayMode" Value="Overlay" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="defaultState">
<VisualState.Setters>
<Setter Target="spltvw.DisplayMode" Value="CompactInline" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
....
</SplitView>
这是我的代码隐藏:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
bool test = false;
if(IsScreenSizeLessThan7inches())
test = VisualStateManager.GoToState(this, lowerThan7State.Name, true);
else
test = VisualStateManager.GoToState(this, defaultState.Name, true);
}
我已经尝试更改&#34; useTransitions&#34; -parameter。我已经调试并测试了&#34; IsScreenSizeLessThan7inches()&#34;方法。但这种方法的结果是正确的。
GoToState()方法的返回值始终为false,并且SplittView-Control的状态也不会更改。
我没有发现问题...
请帮我找错。
谢谢!
答案 0 :(得分:0)
AVK的评论非常有帮助。
我已将VisualStateManager放在SplitView之外,现在它可以正常工作。
这就是我所做的:
<Grid Background="LightGray">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="lowerThan7State">
<VisualState.Setters>
<Setter Target="spltvw.DisplayMode" Value="Overlay" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="defaultState">
<VisualState.Setters>
<Setter Target="spltvw.DisplayMode" Value="CompactInline" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition Height="48"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Background="#FFC31727" Orientation="Horizontal">
<Button Name="btn_hamburger" Width="48" VerticalAlignment="Stretch" Click="btn_hamburger_Click">
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph="" Foreground="White" />
</Button>
<TextBlock Margin="10,0,80,0" FontSize="25" VerticalAlignment="Center" Foreground="White" FontWeight="Bold">
***-App
</TextBlock>
</StackPanel>
<Grid Grid.Row="0" HorizontalAlignment="Right" Margin="0,0,4,0" VerticalAlignment="Center">
<Image Source="ms-appx:///Assets/***_Logo_weiss.png" Height="44" />
</Grid>
<SplitView x:Name="spltvw" IsPaneOpen="False" DisplayMode="CompactInline" OpenPaneLength="250"
CompactPaneLength="48" Grid.Row="1" PaneBackground="#FFDADADA" SizeChanged="spltvw_SizeChanged">
...
</SplitView>