事件没有被取消订阅我不知道为什么而且它杀了我...... 我有一个暂停按钮,使用与CompositionTarget.Rendering相同的想法 - = RenderingEvent;它被称为但似乎没有阻止它......
我的代码:
private EventHandler RenderingEvent;
StopSimulation(sender, t);// how i am calling it
private void StopSimulation(object sender, ElapsedEventArgs e)
{
time = DateTime.Now;
int removedtime = 0;
InSimulation = false;
aTimer.Enabled = false;
CompositionTarget.Rendering -= RenderingEvent; // doesnt seem to stop it
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
CompositionTarget.Rendering -= RenderingEvent;
position = new Point(5, HighestPoint * MetresPerPixel);
//acceleration = new Vector(0, 30); // y direction is downwards
int removedtime = 0;
time = DateTime.Now;
RenderingEvent = (s, g) => OnRendering(s, g, removedtime, time);
CompositionTarget.Rendering += RenderingEvent;
btn_Pause.Content = "Pause";
}
我的完成"代码(删除所有垃圾)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Timers;
using System.ComponentModel;
using System.Windows.Media.Animation;
using System.Text.RegularExpressions;
namespace WpfApplication3
{
public partial class MainWindow : Window
{
bool LabelExist = false;
int count = 1;
bool InSimulation = false;
bool ModelModeAsParticle;
static System.Timers.Timer aTimer;
public MainWindow()
{
InitializeComponent();
}
private Point position; // in pixels
private Vector Velocity; // in pixels per second
private Vector acceleration; // in pixels per square second
private DateTime time;
private EventHandler RenderingEvent;
private System.Timers.ElapsedEventHandler TimerEvent;
private double TimeConstant;
private double TimersTime;
private double MetresPerPixel;
double HighestPoint, LowestPoint;
private void OnLoaded(object sender, RoutedEventArgs e)
{
CompositionTarget.Rendering -= RenderingEvent;
position = new Point(5, HighestPoint * MetresPerPixel);
//acceleration = new Vector(0, 30); // y direction is downwards
int removedtime = 0;
time = DateTime.Now;
RenderingEvent = (s, g) => OnRendering(s, g, removedtime, time);
CompositionTarget.Rendering += RenderingEvent;
btn_Pause.Content = "Pause";
}
private void OnRendering(object sender, EventArgs g, int removedtime, DateTime t)
{
var now = DateTime.Now.AddSeconds(-removedtime);
var dt = (now - t).TotalSeconds;
time = now;
position += Velocity * dt;
Velocity += acceleration * dt;
projectileGeometry.Center = position;
}
void LoadStaticsAndStart(object sender, RoutedEventArgs e)
{
if (FormatCheck(txtb_AngleOLaunch) == false || FormatCheck(txtbox_InitialVelocity) == false || FormatCheck(txtbox_TimeOfFlight) == false)
{
}
else
{
MPP(newParticle, newEnvironment);
OnLoaded(this, e);
double HVelTemp = newParticle.InitialVelocity.HorizontalVelocity * MetresPerPixel;
double VVelTemp = newParticle.InitialVelocity.VerticalVelocity * MetresPerPixel * -1;
Velocity = new Vector(HVelTemp, VVelTemp); // y direction is downwards
acceleration = new Vector(0, -1*newEnvironment.gravity*MetresPerPixel); // y direction is downwards
aTimer = new System.Timers.Timer(200);
TimerEvent = (s, t) => onTimedEvent(s, t, newParticle, newEnvironment);
aTimer.Elapsed += TimerEvent;
InSimulation = true;
aTimer.Enabled = true;
}
}
int DigitAccuracy = 2;
void onTimedEvent(Object sender, ElapsedEventArgs t, particle newProjectile, Environment newEnvironment)
{
if (newProjectile.Time < newProjectile.TimeOfFlight && InSimulation == true)
{
newProjectile.Time = Math.Round(TimersTime,1);
}
else if (newProjectile.TimeOfFlight == newProjectile.Time)
{
StopSimulation(sender, t);
}
}
private void StopSimulation(object sender, ElapsedEventArgs e)
{
InSimulation = false;
aTimer.Enabled = false;
CompositionTarget.Rendering -= RenderingEvent;
}
private void btn_Pause_Click(object sender, RoutedEventArgs e)
{
if ((string)btn_Pause.Content == "Pause")
{
aTimer.Enabled = false;
CompositionTarget.Rendering -= RenderingEvent;
}
else if (InSimulation == true && (string)btn_Pause.Content != "Pause")
{
aTimer.Enabled = true;
RenderingEvent = (s, g) => OnRendering(s, g, removedtime, time);
CompositionTarget.Rendering += RenderingEvent;
}
else if (InSimulation != true)
{
}
}
private void btn_fastforward_Click(object sender, RoutedEventArgs e)
{
if (InSimulation == true)
{
CompositionTarget.Rendering -= RenderingEvent;
RenderingEvent = (s, g) => OnRendering(s, g, removedtime, time);
CompositionTarget.Rendering += RenderingEvent;
}
else if (InSimulation == false)
{
}
}
private void btn_backwards_Click(object sender, RoutedEventArgs e)
{
if (InSimulation == true)
{
CompositionTarget.Rendering -= RenderingEvent;
RenderingEvent = (s, g) => OnRendering(s, g, removedtime, time);
CompositionTarget.Rendering += RenderingEvent;
}
else if (InSimulation == false)
{
}
}
答案 0 :(得分:0)
要进行诊断,可能会创建一个有效包装CompositionTarget.RenderingEvent事件的新事件,但在附加/分离时除外:
private int _eventSubscriptionCount = 0;
public event EventHandler<EventArgs> RenderingEventWrapper
{
add
{
CompositionTarget.RenderingEvent += value;
_eventSubscriptionCount++;
}
remove
{
CompositionTarget.RenderingEvent -= value;
_eventSubscriptionCount--;
}
}
然后看看当你期望没有更多的订阅时仍然订阅了多少...调试添加/删除部分也将完成这个技巧并在执行中断时检查调用堆栈。