C# WPF背后代码中操纵增量上的GoToStateTAction

C# WPF背后代码中操纵增量上的GoToStateTAction,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我有一个简单的菜单,现在可以在鼠标悬停事件中隐藏/显示,但我想做的是根据滑动事件触发隐藏/显示的特定动画。我使用窗口上的manipulationdelta捕获了滑动事件,但我似乎无法启动动画。我正在努力: VisualStateManager.GoToState(ShowControlBar,“ShowNav”,true) 但它似乎什么也没做 谢谢你的帮助 编辑:如果有人在看这个,我设法让它通过运行 ExtendedVisualStateManager.GoToElementState(this

我有一个简单的菜单,现在可以在鼠标悬停事件中隐藏/显示,但我想做的是根据滑动事件触发隐藏/显示的特定动画。我使用窗口上的manipulationdelta捕获了滑动事件,但我似乎无法启动动画。我正在努力: 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;
        }
    }

}
公共部分类主窗口:窗口
{
专用点(u initialpoint);;
公共主窗口()
{
初始化组件();
}
私有无效窗口\u操作开始(对象发送器,
操纵开始事件args操纵开始事件args)
{
操纵开始事件args.操纵容器=此;
操纵开始事件args.Handled=true;
}
私有无效窗口\u操纵增量(对象发送方,操纵DeltaEventArgs操纵DeltaEventArgs)
{
if(操纵三角形目标IsInertial)
{
Point currentpoint=操纵DeltaEventargs.ManipulationRigin;
如果(currentpoint.X-_initialpoint.X>=200)//500是阈值,则要在其中触发向右滑动事件
{
System.Diagnostics.Debug.WriteLine(“向右滑动”);
//操纵deltaeventargs.Complete();
}
如果(currentpoint.X-_initialpoint.X=200)
{
系统。诊断。调试。写线(“向下滑动”);
//操纵deltaeventargs.Complete();
}
if(currentpoint.Y-_initialpoint.Y