【问题标题】:Setting Animation to trigger on a buttons second click not first click将动画设置为在按钮第二次单击而不是第一次单击时触发
【发布时间】:2018-03-09 09:27:57
【问题描述】:

我的主页中有一个框架,我已将其设置为在单击按钮时进行拉伸/滑动。我不希望框架在第一次单击按钮时拉伸/滑动,而是在用户第二次单击同一个按钮时。 XAML:

        <Storyboard x:Key="FrameSlide">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="currentPage">
            <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
            <SplineDoubleKeyFrame KeyTime="0:0:0.7" Value="1.217"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="currentPage">
            <SplineDoubleKeyFrame KeyTime="0" Value="0"/>
            <SplineDoubleKeyFrame KeyTime="0:0:0.7" Value="-126"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="FrameSlideBack">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="currentPage">
            <SplineDoubleKeyFrame KeyTime="0" Value="1.217"/>
            <SplineDoubleKeyFrame KeyTime="0:0:0.7" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="currentPage">
            <SplineDoubleKeyFrame KeyTime="0" Value="-126"/>
            <SplineDoubleKeyFrame KeyTime="0:0:0.7" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>




</Window.Resources>
<Window.Triggers>
    <EventTrigger RoutedEvent="UIElement.MouseLeftButtonDown" SourceName="TransmitBTN">
        <BeginStoryboard x:Name="FrameSlide" Storyboard="{StaticResource FrameSlide}"/>
    </EventTrigger>
    <EventTrigger RoutedEvent="UIElement.MouseLeftButtonDown" SourceName="PatientBTN">
        <BeginStoryboard x:Name="FrameSlideBack" Storyboard="{StaticResource FrameSlideBack}"/>
    </EventTrigger>
</Window.Triggers>

我尝试在我的 VB.net 代码中为按钮使用 storyboard.stop() / storyboard.being(),但似乎 xaml 已经摆脱了 vb 代码。我已经厌倦了如果我的框架是某个宽度的语句然后什么都不做触发动画。 不知道从这里去哪里。 谢谢

【问题讨论】:

    标签: wpf vb.net xaml animation storyboard


    【解决方案1】:

    我在 C# 中回答这个问题,因为我不了解 VB,但据我所知,您可以轻松更改它?

    我能得到的答案是在按钮上创建一个事件 button_click,在类中创建一个公共属性。在事件中增加属性并且当它达到两个时,触发你的故事板。

    按钮事件的 XAML:

    <Button x:Name="button" Content="Button" Click="Button_OnClick"
    

    C#代码:

    public partial class StackOverFlowExample
    {
       public StackOverFlowExample()
       {
        InitializaComponent();
        buttonClickCounter = 0;
       }
    
       public int buttonClickCounter {get; set;}
    
       private void Button_Click(object sender, RoutedEventArgs e)
       {
         buttonClickCounter++;
         if (buttonClickCounter == 2)
         {
          buttonClickCounter = 0;
    
          Storyboard sb = Application.Current.Resources["FrameSlide"] as 
          Storyboard;
    
          if (sb.IsSealed)
              sb = sb.Clone();
          Storyboard.SetTarget(sb, this.NextButton);
          sb.Completed += delegate { };
          sb.Begin();
         }
    
       e.Handled = true;
       }
    }
    

    【讨论】:

    • 谢谢,这为我指明了正确的方向。我得到了它的工作。
    • @Elliott 没问题:p 如果您满意,请接受答案! :p
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多