如何阻止事件发生? ( - =事件)似乎没有工作

时间:2015-01-16 18:32:25

标签: c#

我正在尝试为我的动画创建一个暂停按钮(所以OnRendering停止射击)但它似乎没有停止。我以为用这个                 CompositionTarget.Rendering - =(s,g)=> OnRendering(s,g,time);意味着它会阻止事件发射,但似乎没有起作用。谢谢你的帮助!

我的代码:

    private void btn_Pause_Click(object sender, RoutedEventArgs e)
    {
        time = DateTime.Now;
        if ((string)btn_Pause.Content == "Pause")
        {

            btn_Pause.Content = "Resume";
            CompositionTarget.Rendering -= (s, g) => OnRendering(s, g, time);
        }
        else
        {
            CompositionTarget.Rendering += (s, g) => OnRendering(s, g, time);
            btn_Pause.Content = "Pause";

        }
    }
    private void OnRendering(object sender, EventArgs g, DateTime t)
    {
        var now = DateTime.Now;
        var dt = (now - t).TotalSeconds;
        time = now;

        position += Velocity * dt;
        Velocity += acceleration * dt;
        projectileGeometry.Center = position;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        position = new Point(5, 290);
        Velocity = new Vector(50, -70); // y direction is downwards
        acceleration = new Vector(0, 30); // y direction is downwards
        time = DateTime.Now;
        CompositionTarget.Rendering += (s, g) => OnRendering(s, g, time);
    }

我尝试过使用CompositionTarget.Rendering - = OnRendering;但是我在“OnRending”匹配委托“System.EventHandler”时遇到No overload错误。

如果删除了DateTime T并将其作为参数传入,我如何控制暂停和恢复按钮以及后退和前进按钮?

    private void btn_Pause_Click(object sender, RoutedEventArgs e)
    {
        EventHandler Handler = (s, g) => OnRendering(s, g, time);
        time = DateTime.Now;
        if ((string)btn_Pause.Content == "Pause")
        {

            btn_Pause.Content = "Resume";
            CompositionTarget.Rendering -= Handler;
        }
        else
        {
            CompositionTarget.Rendering += Handler;
            btn_Pause.Content = "Pause";

        }
    }

试过这根本不会停止动画。

0 个答案:

没有答案