我正在使用WPF实现轮播用户界面。 当滑动轮播时,我使用VisualStateManager来更新轮播中元素的位置。
将元素移动到其目标兄弟位置的位置后,我想重新分配元素引用,以便VisualStateManager可以继续将项目移动到下一个位置的行为,而无需维护每个元素的运动。
我遇到的问题是,我认为我需要重新分配元素引用到兄弟元素的引用,该元素刚刚重新定位了轮播中的一个元素的位置(类似于链接列表)
问题:
我可以在XAML中将元素的对象引用分配给另一个元素的对象引用吗? 或者更好的是,有没有更好的方法来使用XAML实现轮播?
XAML:
<Grid x:Name="MyGrid">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SwipeLeftGroup">
<VisualState x:Name="SwipeLeftState">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Canvas.Left)"
From="290" To="159"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Canvas.Top)"
From="20" To="73"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
To="1"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
To="1"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)"
From="0" To="-25"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)"
To="0"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
From="0" To="-25"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"
From="0" To="-40"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"
From="0" To="20"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="Height"
From="246" To="160"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="FrontRectangle"
Storyboard.TargetProperty="Width"
From="400" To="350"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidLeftRectangle"
Storyboard.TargetProperty="(Canvas.Left)"
From="159" To="99"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidLeftRectangle"
Storyboard.TargetProperty="(Canvas.Top)"
From="73" To="191"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidLeftRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
From="0" To="1"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidLeftRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
From="0" To="2.5"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidLeftRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)"
From="-25" To="-60"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidLeftRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
From="125" To="145"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidLeftRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"
From="-40" To="0"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidLeftRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"
From="20" To="0"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidLeftRectangle"
Storyboard.TargetProperty="Height"
From="160" To="100"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidLeftRectangle"
Storyboard.TargetProperty="Width"
From="350" To="100"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidRightRectangle"
Storyboard.TargetProperty="(Canvas.Left)"
From="559" To="290"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidRightRectangle"
Storyboard.TargetProperty="(Canvas.Top)"
From="73" To="20"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidRightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
To="1"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidRightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
To="1"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidRightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)"
To="0"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidRightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)"
To="0"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidRightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
To="0"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidRightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"
To="0"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidRightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"
To="0"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidRightRectangle"
Storyboard.TargetProperty="Height"
To="246"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="MidRightRectangle"
Storyboard.TargetProperty="Width"
To="400"
Duration="0:0:.15">
</DoubleAnimation>
<Int32Animation Storyboard.TargetName="MidRightRectangle"
Storyboard.TargetProperty="(Panel.ZIndex)"
To="4"
Duration="0:0:.15">
</Int32Animation>
<DoubleAnimation Storyboard.TargetName="RightRectangle"
Storyboard.TargetProperty="(Canvas.Left)"
From="774" To="559"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="RightRectangle"
Storyboard.TargetProperty="(Canvas.Top)"
From="191" To="73"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="RightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
To="1"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="RightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
To="1"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="RightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)"
To="25"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="RightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
From="45" To="25"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="RightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"
To="-40"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="RightRectangle"
Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"
To="20"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="RightRectangle"
Storyboard.TargetProperty="Height"
From="100" To="160"
Duration="0:0:.15">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="RightRectangle"
Storyboard.TargetProperty="Width"
From="100" To="350"
Duration="0:0:.15">
</DoubleAnimation>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Canvas>
<Rectangle x:Name="FrontRectangle" Width="400" Height="246" Fill="Purple"
Canvas.Left="290" Canvas.Top="20" Panel.ZIndex="4" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="RightRectangle" Width="100" Height="100" Fill="LightBlue"
RenderTransformOrigin="0.5,0.5" Canvas.Left="774" Canvas.Top="191" Panel.ZIndex="2">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="1" ScaleX="2.5"/>
<SkewTransform AngleX="60"/>
<RotateTransform Angle="-145"/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="LeftRectangle" Width="100" Height="100" Fill="LightGreen"
RenderTransformOrigin="0.5,0.5" Canvas.Left="99" Canvas.Top="191" Panel.ZIndex="2" >
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="1" ScaleX="2.5"/>
<SkewTransform AngleX="-60"/>
<RotateTransform Angle="145"/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="MidLeftRectangle" Width="350" Height="160" Fill="LightSeaGreen"
Canvas.Left="159" Canvas.Top="73" RenderTransformOrigin="0.5,0.5" Panel.ZIndex="3" >
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform AngleX="-25"/>
<RotateTransform Angle="-25"/>
<TranslateTransform X="-40" Y="20"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="MidRightRectangle" Width="350" Height="160" Fill="LightCoral"
Canvas.Left="559" Canvas.Top="73" RenderTransformOrigin="0.5,0.5" Panel.ZIndex="3" >
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform AngleX="25"/>
<RotateTransform Angle="25"/>
<TranslateTransform X="-40" Y="20"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
</Grid>
代码:
private void OnSwipedLeft()
{
var visualState = "SwipeLeftState";
var success = VisualStateManager.GoToElementState(MyGrid, visualState, true);
Debug.Assert(success);
}