我有一个简单的菜单,现在隐藏/显示鼠标悬停事件,但我想做的是根据滑动事件触发隐藏/显示的特定动画。我在窗口上使用manipdelta捕获了向上滑动事件,但我似乎无法触发动画。我在尝试:
VisualStateManager.GoToState(ShowControlBar,“ShowNav”,true);
但它似乎根本没有做任何事情。
感谢您的帮助!
编辑:如果有人在看这个,我设法通过跑步来解决它 ExtendedVisualStateManager.GoToElementState(this.LayoutRoot,“HideNav”,true); 其中LayoutRoot是定义状态的网格。奇怪的。如果您传递应用它的元素的名称,它将不起作用。
完成XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
x:Class="HideShowTest.MainWindow"
Title="MainWindow" Height="350" Width="525"
ManipulationStarting="Window_ManipulationStarting"
ManipulationDelta="Window_ManipulationDelta"
ManipulationStarted="MainWindow_OnManipulationStarted"
ManipulationInertiaStarting="Window_InertiaStarting"
IsManipulationEnabled="True">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="HideShowNav" ei:ExtendedVisualStateManager.UseFluidLayout="True">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.3"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="ShowNav">
<Storyboard x:Name="ShowNavStoryboard">
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"
Storyboard.TargetName="ControlPanel">
<EasingDoubleKeyFrame KeyTime="0" Value="-105.231"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="HideNav"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<VisualStateManager.CustomVisualStateManager>
<ei:ExtendedVisualStateManager/>
</VisualStateManager.CustomVisualStateManager>
<Grid x:Name="ControlPanel" Margin="0,303,-0.333,-132.333"
Background="#FF8F99EA" RenderTransformOrigin="0.5,0.5"
Height="150" Opacity="0.6">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<ToggleButton x:Name="PowerOffButton" Content="test"
HorizontalAlignment="Left" Margin="0,0,0,34" Width="80" Height="74"
BorderThickness="0" Command="{Binding PowerButtonClick}"
VerticalAlignment="Bottom"/>
<Rectangle x:Name="ShowControlBar" Fill="#FFF4F4F5"
HorizontalAlignment="Left" Height="37.505"
Stroke="Black" VerticalAlignment="Top"
Width="518.666" Opacity="0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<ei:GoToStateAction StateName="ShowNav"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
</Grid>
<Rectangle x:Name="HideControlBar" Fill="#FFF4F4F5"
HorizontalAlignment="Left" Height="95.205" Margin="0,137.404,-0.333,0"
Stroke="Black" VerticalAlignment="Top" Width="518.666" Opacity="0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<ei:GoToStateAction StateName="HideNav"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
</Grid>
</Window>
完成MainWindow.xaml.cs
public partial class MainWindow : Window
{
private Point _initialpoint;
public MainWindow()
{
InitializeComponent();
}
private void Window_ManipulationStarting(object sender,
ManipulationStartingEventArgs manipulationStartingEventArgs)
{
manipulationStartingEventArgs.ManipulationContainer = this;
manipulationStartingEventArgs.Handled = true;
}
private void Window_ManipulationDelta(object sender, ManipulationDeltaEventArgs manipulationDeltaEventArgs)
{
if (manipulationDeltaEventArgs.IsInertial)
{
Point currentpoint = manipulationDeltaEventArgs.ManipulationOrigin;
if (currentpoint.X - _initialpoint.X >= 200)//500 is the threshold value, where you want to trigger the swipe right event
{
System.Diagnostics.Debug.WriteLine("Swipe Right");
// manipulationDeltaEventArgs.Complete();
}
if (currentpoint.X - _initialpoint.X <= -200)
{
System.Diagnostics.Debug.WriteLine("Swipe Left");
// manipulationDeltaEventArgs.Complete();
}
if (currentpoint.Y - _initialpoint.Y >= 200)
{
System.Diagnostics.Debug.WriteLine("Swipe Down");
// manipulationDeltaEventArgs.Complete();
}
if (currentpoint.Y - _initialpoint.Y <= -200)
{
VisualStateManager.GoToState(ShowControlBar, "ShowNav", true);
}
}
}
private void Window_InertiaStarting(object sender,
ManipulationInertiaStartingEventArgs manipulationInertiaStartingEventArgs)
{
// Decrease the velocity of the Rectangle's movement by
// 10 inches per second every second.
// (10 inches * 96 pixels per inch / 1000ms^2)
manipulationInertiaStartingEventArgs.TranslationBehavior.DesiredDeceleration = 10.0 * 96.0 / (1000.0 * 1000.0);
// Decrease the velocity of the Rectangle's resizing by
// 0.1 inches per second every second.
// (0.1 inches * 96 pixels per inch / (1000ms^2)
manipulationInertiaStartingEventArgs.ExpansionBehavior.DesiredDeceleration = 0.1 * 96 / (1000.0 * 1000.0);
// Decrease the velocity of the Rectangle's rotation rate by
// 2 rotations per second every second.
// (2 * 360 degrees / (1000ms^2)
manipulationInertiaStartingEventArgs.RotationBehavior.DesiredDeceleration = 720 / (1000.0 * 1000.0);
manipulationInertiaStartingEventArgs.Handled = true;
}
private void MainWindow_OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
_initialpoint = e.ManipulationOrigin;
}
}
}