在我的UWP应用程序中,我将在后面的代码中创建所有控件(绝不使用XAML)。需要使我的应用程序响应窗口大小的变化。偶然发现各种视觉状态,并尝试根据窗口的宽度尝试更改Grid控件的背景颜色。这是我到目前为止所获得的,但是什么也没有发生:
Dim visualStates = New VisualStateGroup()
Dim horiVisualState = New VisualState()
horiVisualState.Setters.Add(New Setter() With {.Target = New TargetPropertyPath(BackgroundProperty) With {.Target = grid2}, .Value = New SolidColorBrush(Windows.UI.Colors.Orange)})
horiVisualState.StateTriggers.Add(New AdaptiveTrigger() With {.MinWindowWidth = 800})
visualStates.States.Add(horiVisualState)
VisualStateManager.GetVisualStateGroups(Me).Add(visualStates)
grid2是一个网格,是另一个网格的子级,“ Me”是指主网格。
答案 0 :(得分:0)
您需要确保“ Me”网格是XAML页面上所有其他子UI元素的根面板,如下所示:
<Page
x:Class="AppAdaptive.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppAdaptive"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid x:Name="Me">
</Grid>
Dim grid2 As Grid = New Grid() With {.Width = 200, .Background = New SolidColorBrush(Colors.Black)}
[Me].Children.Add(grid2)
Dim visualStateGroup = New VisualStateGroup()
Dim horiVisualState = New VisualState()
horiVisualState.Setters.Add(New Setter() With {.Target = New TargetPropertyPath() With {.Target = grid2, .Path = New PropertyPath("Background")}, .Value = New SolidColorBrush(Windows.UI.Colors.Orange)})
horiVisualState.StateTriggers.Add(New AdaptiveTrigger() With {.MinWindowWidth = 800})
visualStateGroup.States.Add(horiVisualState)
VisualStateManager.GetVisualStateGroups(Me).Add(visualStateGroup)