“System.StackOverflowException”发生在PresentationCore.dll中
我有一个 WPF UserControl,我尝试在其中实现自定义 MouseClick(因为 WPF(用户)控件上没有 MouseClick
事件)事件。
我得到以下内容:
一些代码:
/// <summary>
/// Occurs when users left clicks the MyControl.
/// </summary>
public event MouseButtonEventHandler MouseClick { add { AddHandler(MouseClickEvent, value); } remove { RemoveHandler(MouseClickEvent, value); } }
protected virtual void OnMouseClick(MouseButtonEventArgs e)
{
base.RaiseEvent(e);
//this.RaiseEvent(new RoutedEventArgs(MouseClickEvent, this));
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
if (!this.movedAfterMouseDown)
{
OnMouseClick(e);
}
this.gotLeftButtonDown = false;
this.movedAfterMouseDown = false;
}
那么,问题出在哪里?
UPDATE 1
protected virtual void OnMouseClick(MouseButtonEventArgs e)
{
//base.RaiseEvent(e);
MouseButtonEventArgs args = new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, e.ChangedButton);
this.RaiseEvent(args);
}
值不能为空。 参数名称:routedEvent
UPDATE 2
我成功实现的另一个自定义事件(工作没有问题) - SelectedChanged< /code>:
static void OnIsSelectedChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
var s = (MyControl)source;
s.RaiseEvent(new RoutedEventArgs(SelectedChangedEvent, s));
}
更新 3
System.Windows.Controls.Control 的 OnPreviewMouseDoubleClick 实现:
protected virtual void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)
{
base.RaiseEvent(e);
}
更新 5(适用于坦克中的人员)
class Foo : FrameworkElement
{
event EasterCameEvent; // I named it MouseClick
public DoSomething()
{
EasterCameArgs args= ...
if (Date.Now = EasterDate)
OnEasterCame(args)
}
protected virtual void OnEasterCame(EasterCameArgs e)
{
base.RaiseEvent(e);
}
}
I have a WPF UserControl in which I try to implement the custom MouseClick (because there is no MouseClick
event on a WPF (User)Control) event.
I got the following:
Some code:
/// <summary>
/// Occurs when users left clicks the MyControl.
/// </summary>
public event MouseButtonEventHandler MouseClick { add { AddHandler(MouseClickEvent, value); } remove { RemoveHandler(MouseClickEvent, value); } }
protected virtual void OnMouseClick(MouseButtonEventArgs e)
{
base.RaiseEvent(e);
//this.RaiseEvent(new RoutedEventArgs(MouseClickEvent, this));
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
if (!this.movedAfterMouseDown)
{
OnMouseClick(e);
}
this.gotLeftButtonDown = false;
this.movedAfterMouseDown = false;
}
So, where is the problem?
UPDATE 1
protected virtual void OnMouseClick(MouseButtonEventArgs e)
{
//base.RaiseEvent(e);
MouseButtonEventArgs args = new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, e.ChangedButton);
this.RaiseEvent(args);
}
Value cannot be null.
Parameter name: routedEvent
UPDATE 2
An other custom event I implemented successfully (work without problems) - SelectedChanged
:
static void OnIsSelectedChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
var s = (MyControl)source;
s.RaiseEvent(new RoutedEventArgs(SelectedChangedEvent, s));
}
UPDATE 3
System.Windows.Controls.Control's OnPreviewMouseDoubleClick implementation:
protected virtual void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)
{
base.RaiseEvent(e);
}
Update 5 (for people in the tank)
class Foo : FrameworkElement
{
event EasterCameEvent; // I named it MouseClick
public DoSomething()
{
EasterCameArgs args= ...
if (Date.Now = EasterDate)
OnEasterCame(args)
}
protected virtual void OnEasterCame(EasterCameArgs e)
{
base.RaiseEvent(e);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为是这样的:
如果您正在处理鼠标单击,您不想再次引发事件,因为这只会再次调用您的处理程序,这将引发事件......
I think it's in this line:
If you are handling the mouse click you don't want to raise the event again as that will simply call your handler again, which will raise the event....
您需要从自定义用户控件的所有部分中删除 base.RaiseEvent(e)。这就是堆栈溢出的原因。
如果您从 UserControl 继承,则已经为您实现了单击事件。您不需要重新实施。虽然您可能需要处理它们,但很可能您不需要。
这不会覆盖该事件。这是引发事件时的处理程序!您的控件的用户将编写像这样的处理程序。不要重新实现它。除非您需要对数据执行某些操作,否则甚至不要处理它。这些事件已经为您写好了。
编辑:
虽然我的答案是错误的,因为它没有解决问题,但它仍然有助于理解为什么发生 stackoverflow 异常。
控制流是:
接受的答案更改为:
This这就是我试图在其他答案的评论中解释的内容。如果我说得不够清楚,我对此表示歉意。我相信 serhio 和我不同步,因为当他在寻找代码修复时,我试图解释 stackvoverflow 的原因。如果我错了请纠正我。
You need to remove the base.RaiseEvent(e) from ALL parts of your custom user control. That is the cause of the stack overflow.
If you're inheriting from UserControl, the click events are already implemented for you. You do NOT need to re-implement. You MAY need to handle them though, but most probably you do not.
This does not over-ride the event. This is a handler for when the event is raised! Users of your control will write handlers just like this one. Don't reimplement it. Don't even handle it unless you need to do something with the data. These events are written for you already.
Edit:
While my answer was wrong in that it didn't fix the issue, it should still be helpful to understand WHY the stackoverflow exception was happening.
The control flow was:
The accepted answer changes to this:
This is what I was trying to explain in comments on other answers. If I wasn't clear enough I apologise for that. I believe that serhio and I were out of sync in that I was trying to explain the cause of the stackvoverflow, when he was looking for a fix to the code. Correct me if I'm wrong.