首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自XAML的RoutedEvent

来自XAML的RoutedEvent
EN

Stack Overflow用户
提问于 2021-02-21 21:15:25
回答 1查看 85关注 0票数 0
代码语言:javascript
复制
<TextBlock x:Name="xDen" Text="Wrong">
        <TextBlock.Triggers>
            <EventTrigger RoutedEvent="TextBlock.Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="xDen" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0:2" AutoReverse="True" RepeatBehavior="Forever"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </TextBlock.Triggers>
    </TextBlock>

你好,这是我的代码块,我有一个特殊的代码块,我在代码块中解释过,

代码语言:javascript
复制
        private void Check_Click(object sender, RoutedEventArgs e)
    {
        if(engEdu.Text != "")
        {
            int indexen = wordEngs.FindIndex(a => a.Contains(engEdu.Text));
            if(checkPoint.Text == wordEsps[indexen])
            {
                espEdu.Text = wordEsps[indexen];
            }
            else
            {
                MessageBox.Show("Wrong!");
                **I want to work that animation here!**
            }
        }
    }

我应该怎么做才能解决这个问题?

我试过这样做,

代码语言:javascript
复制
private void ex()
{
  // I Tried to route 'ex' and work it but I couldn't
}
EN

回答 1

Stack Overflow用户

发布于 2021-02-21 22:22:14

你有很多可能性。例如,您可以使用EventTriggerDataTrigger

下面的示例定义了两个自定义路由事件:触发错误动画的ValidationFailed和停止错误动画的ValidationPassed

MainWindow.xaml

代码语言:javascript
复制
<Window>
  <Window.Triggers>
    <EventTrigger RoutedEvent="MainWindow.ValidationFailed">
      <BeginStoryboard>
        <Storyboard>
          <DoubleAnimation Storyboard.TargetName="xDen"
                           Storyboard.TargetProperty="Opacity"
                           From="0"
                           To="1"
                           Duration="0:0:0:2"
                           AutoReverse="True"
                           RepeatBehavior="Forever" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>

    <EventTrigger RoutedEvent="MainWindow.ValidationPassed">
      <BeginStoryboard>
        <Storyboard>
          <DoubleAnimation Storyboard.TargetName="xDen"
                           Storyboard.TargetProperty="Opacity"
                           From="1"
                           To="0"
                           Duration="0:0:0:2" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Window.Triggers>


  <TextBlock x:Name="xDen"
             Text="Wrong" />
</Window>

MainWindow.xaml.cs

代码语言:javascript
复制
public partial class MainWindow : Window
{
  #region ValidationFailedRoutedEvent

  public static readonly RoutedEvent ValidationFailedRoutedEvent = EventManager.RegisterRoutedEvent(
    "ValidationFailed",
    RoutingStrategy.Bubble,
    typeof(RoutedEventHandler),
    typeof(MainWindow));

  public event RoutedEventHandler ValidationFailed
  {
    add => AddHandler(MainWindow.ValidationFailedRoutedEvent, value);
    remove => RemoveHandler(MainWindow.ValidationFailedRoutedEvent, value);
  }

  #endregion

  #region ValidationPassedRoutedEvent

  public static readonly RoutedEvent ValidationPassedRoutedEvent = EventManager.RegisterRoutedEvent(
    "ValidationPassed",
    RoutingStrategy.Bubble,
    typeof(RoutedEventHandler),
    typeof(MainWindow));

  public event RoutedEventHandler ValidationPassed
  {
    add => AddHandler(MainWindow.ValidationPassedRoutedEvent, value);
    remove => RemoveHandler(MainWindow.ValidationPassedRoutedEvent, value);
  }

  #endregion

  private void Check_Click(object sender, RoutedEventArgs e)
  {
    if (engEdu.Text != "")
    {
      int indexen = wordEngs.FindIndex(a => a.Contains(engEdu.Text));
      if (checkPoint.Text == wordEsps[indexen])
      {
        // Stop the error animation
        RaiseEvent(new RoutedEventArgs(MainWindow.ValidationPassedRoutedEvent, this));
      }
      else
      {
        // Show the error animation
        RaiseEvent(new RoutedEventArgs(MainWindow.ValidationFailedRoutedEvent, this));
      }
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66302678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档