在 C# 中引发外部对象事件

发布于 2024-07-17 13:47:03 字数 260 浏览 10 评论 0原文

如果 actions 是一个面板,我是否能够引发其父面板的 Click 事件? 我目前有此代码,但 Click 事件不是方法,因此此代码无效。
有谁知道我怎样才能实现这一目标?

actions.Click += delegate(object Sender, EventArgs e)
{
    ((Panel)Sender).Parent.Click();
}

If actions is a Panel, am I able to raise the Click event of it's parent?
I have this code at the moment, but the Click event isn't a method, so this code is invalid.
Does anyone know how I can achieve this?

actions.Click += delegate(object Sender, EventArgs e)
{
    ((Panel)Sender).Parent.Click();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

长亭外,古道边 2024-07-24 13:47:03

无法直接从另一个 引发 C# 事件(即使它是 public)。 您可以提供一个代表您引发事件的方法(具有足够的访问修饰符),并在另一个中调用该方法。

顺便说一句,这可以通过反射实现,但我认为这是一个肮脏的黑客行为。

It's not possible to raise a C# event directly from another class (even if it's public). You could provide a method (with a sufficient access modifier) that raises the event on your behalf and call that method in the other class.

By the way, this is possible with reflection, but I consider that a dirty hack.

厌倦 2024-07-24 13:47:03

CLR 中引发事件的大多数类型都有一个受保护的 On[EventName] 方法来负责引发事件。 您可以使用反射“从外部”调用此受保护的方法:

 Control parent = ((Control)sender).Parent;
 Type parentType = parent.GetType();
 MethodInfo onClickMethod = parentType.GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic);

 onClickMethod.Invoke(parent, new object[] { e });

Most types in the CLR that raises events has a protected On[EventName] method that takes care of raising the event. You can invoke this protected method "from the outside" using Reflection:

 Control parent = ((Control)sender).Parent;
 Type parentType = parent.GetType();
 MethodInfo onClickMethod = parentType.GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic);

 onClickMethod.Invoke(parent, new object[] { e });
合约呢 2024-07-24 13:47:03

我使用反射和扩展方法实现了这一点,这样我就可以通过调用(在本例中)调用 LinkLabel 单击事件:

var link = new LinkLabel()'
link.Text = "Some link";
link.click();

click() 方法是一个 C# 扩展方法:

    public static void click(this LinkLabel linkLabel)
{
    var e = new LinkLabelLinkClickedEventArgs((LinkLabel.Link)(linkLabel.prop("FocusLink")));
    linkLabel.invoke("OnLinkClicked", e);
}

它使用另一个扩展方法来:

  • 从以下位置获取私有属性: LinkLabel(需要创建 LinkLabelLinkClickedEventArgs 对象)
  • 调用 OnLinkClicked 方法(该方法将触发事件

I implemented this using reflection and extension methods so that I can just invoke (in this case) a LinkLabel click event by just calling:

var link = new LinkLabel()'
link.Text = "Some link";
link.click();

the click() method is an C# extension method:

    public static void click(this LinkLabel linkLabel)
{
    var e = new LinkLabelLinkClickedEventArgs((LinkLabel.Link)(linkLabel.prop("FocusLink")));
    linkLabel.invoke("OnLinkClicked", e);
}

which uses another extension methods to:

  • get a private property from the LinkLabel (needed to create the LinkLabelLinkClickedEventArgs object)
  • invoke the OnLinkClicked method (which will fire the event
孤云独去闲 2024-07-24 13:47:03

根据您使用的框架,您可能不必手动调用父级上的单击事件,WPFASP .NET 例如,将事件冒泡到元素树中。

如果这不是一个选择,你可以像迈赫达德建议的那样做。

Depending on what framework you are using its possible you don't have to manually invoke the click event on the parent, WPF and ASP.NET for example bubble events up the element tree.

If that's not an option you can do like Mehrdad suggests.

始于初秋 2024-07-24 13:47:03

我能想到的您想要引发 Click 事件的唯一原因是您是否正在模拟用户输入。 您正在开发自动测试驱动程序或类似的东西吗? 如果是这样,可能有比用仅测试辅助方法玷污代码更好的解决方案。 这个首先出现在谷歌中,我不知道它是否有好处。

如果您尝试将其作为应用程序本身的一种流量控制,我会退一步看看这是否是正确的做法 - 它有点“闻起来很糟糕”。

The only reason I can think of for why you would want to raise the Click event is if you are simulating user input. Are you working on an automated test driver or something like that? If so, there may be better solutions than sullying the code with test-only auxiliary methods. This comes up first in google, I have no idea if it's any good.

If you're trying to do this as a kind of flow control in your application itself, I'd take a step back to see if this is the right thing to do at all - it kind of "smells bad".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文