基本 GUI 事件处理问题 C#

发布于 2024-10-08 12:08:53 字数 200 浏览 0 评论 0原文

下午好,

我有一些关于 GUI 事件处理的非常基本的问题。首先,使用 C# 我们如何将事件链接到对象 - 我猜是事件处理程序?如果是这样,每个处理程序可以使用单独的代码吗? - 事件处理程序如何定位它必须操作的对象?

我对JAVA的工作原理有一个大概的了解。给我一个参考就很好了——我已经在谷歌上搜索过答案,但没有结果。

非常感谢, J

Good Afternoon,

I have some very basic questions on GUI Event Handling. Firstly with C# how can we link events to objects - I am guessing event handlers? If so can each handler use separate code? - How can the event handler locate the objects it must manipulate?

I have a rough idea of how it works in JAVA. Pointing me towards a reference would be fine - I have already trawled Google for answers to no avail.

Many Thanks,
J

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

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

发布评论

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

评论(4

你丑哭了我 2024-10-15 12:08:53

首先,使用 C# 我们如何链接事件
到对象 - 我猜事件
处理程序?如果是这样,每个处理程序都可以使用
单独的代码?

是的,每个事件处理程序都可以有自己的代码:

class A {
    public event EventHandler SomeEvent;
}

class B {
    public B(A a) {
        a.SomeEvent += (sender, e) => { Console.WriteLine("B's handler"); };
    }
}

class C {
    public C(A a) {
        a.SomeEvent += (sender, e) => { Console.WriteLine("C's handler"); };
    }
}

事件处理程序如何定位
它必须操纵的对象?

我将对此进行过度简化,但事件处理程序本质上是观察者模式的包装器。与任何其他委托类型一样,事件处理程序在方法调用列表中保存订阅者列表(请参阅 Delegate.GetInitationList)。您可以这样想:(

class EventHandler {
    LinkedList<Action<object, EventArgs>> subscribers =
        new LinkedList<Action<object, EventArgs>>();

    public void Add(Action<object, EventArgs> f) {
        subscribers.AddLast(f);
    }

    public void Remove(Action<object, EventArgs> f) {
        subscribers.Remove(f);
    }

    public void Invoke(object sender, EventArgs e) {
        foreach(Action<object, EventArgs> f in subscribers)
            f(sender, e);
    }
}

上面的代码与真实事件处理程序类的实际实现细节相去甚远。委托类型是不可变的,因此添加处理程序会返回一个添加了处理程序的新委托,而不是改变处理程序我相信他们的添加/删除方法也有很多线程巫术。)

由于委托实例保存了对其每个订阅者的引用,因此它可以直接访问它所操作的任何对象。

Firstly with C# how can we link events
to objects - I am guessing event
handlers? If so can each handler use
separate code?

Yes, each event handler can have its own code:

class A {
    public event EventHandler SomeEvent;
}

class B {
    public B(A a) {
        a.SomeEvent += (sender, e) => { Console.WriteLine("B's handler"); };
    }
}

class C {
    public C(A a) {
        a.SomeEvent += (sender, e) => { Console.WriteLine("C's handler"); };
    }
}

How can the event handler locate the
objects it must manipulate?

I'm going to oversimplify this a lot, but event handlers are essentially wrappers around the observer pattern. EventHandlers like any other Delegate type hold a list of subscribers in a method invocation list (see Delegate.GetInvocationList). You can think of it like this:

class EventHandler {
    LinkedList<Action<object, EventArgs>> subscribers =
        new LinkedList<Action<object, EventArgs>>();

    public void Add(Action<object, EventArgs> f) {
        subscribers.AddLast(f);
    }

    public void Remove(Action<object, EventArgs> f) {
        subscribers.Remove(f);
    }

    public void Invoke(object sender, EventArgs e) {
        foreach(Action<object, EventArgs> f in subscribers)
            f(sender, e);
    }
}

(The code above is pretty far removed from the actual implementation details of the real event handler class. Delegate types are immutable, so adding a handler returns a new Delegate with the handler added rather than mutating the handler in place. I believe their Add/Remove methods have a lot of threading voodoo in them as well.)

Since the delegate instance holds a reference to each of its subscribers, it has direct access to any object it manipulates.

虫児飞 2024-10-15 12:08:53

每个事件实际上都是一个委托 - 您可以使用以下表示法向该委托注册事件处理程序:

myButton.Click += new EventHandler(myEventHandler);

这样,当按钮单击事件触发时,其调用列表上的任何事件处理程序(在本例中将包括 myEventHandler< 对于每个按钮,您都执行相同的

操作并注册要触发的事件处理程序(多个按钮可以使用相同的事件处理程序)

事件和代理

Each event is in fact a delegate - you register the event handler with this delegate using this notation:

myButton.Click += new EventHandler(myEventHandler);

This way, when the button click event fires, any event handlers on its invocation list (in this case this will include the myEventHandler function.

For each button, you do the same and register the event handler you want to fire (can be the same one for multiple buttons).

Perhaps this MSDN topic will shed some light: Events and Delegates.

拧巴小姐 2024-10-15 12:08:53

使用 Visual Studio 中的设计器,您将可以轻松地使用 GUI 对象处理程序。通过单击“闪电”按钮,您将看到特定控件的所有可用事件。通过在选定的事件上单击两次,IDE 将为您生成一个方法存根。

    private void button_Click(object sender, EventArgs e)
    {
        //sender is the object which actually raised the event
        ((Button)sender).Text = "Clicked Me";
    }

事件处理程序像这样添加(在 *.Designer.cs 文件中自动完成)

    button.Click += new System.EventHandler(this.button_Click);

Using designer in Visual Studio you will have an easy way with GUI objects handlers. By clicking the 'lightning' button you will see all available events for a partticular control. By clicking twice on selected event the IDE will generate a method stub for you.

    private void button_Click(object sender, EventArgs e)
    {
        //sender is the object which actually raised the event
        ((Button)sender).Text = "Clicked Me";
    }

The event handler is added like this(done automatically in the *.Designer.cs file)

    button.Click += new System.EventHandler(this.button_Click);
薄情伤 2024-10-15 12:08:53

这取决于您使用的 UI 库。在 WPF 中,您可以在 XAML 中定义事件处理程序:

<Button Click="HelloWorldClickEventHandler">Hello World</Button>

// this is in your .cs code file
private void HelloWorldClickEventHandler(object sender, RoutedEventArgs e)
{
    // some code
}

在 WinForms 中,您可以将处理程序附加到表单的构造函数中:

public MyForm() {
    // ...
    HelloWorldButton.Click += new EventHandler(HelloWorldClickEventHandler);
}

private void HelloWorldClickEventHandler(object sender, EventArgs e)
{
    // some code
}

当然,使用 Visual Studio 等 IDE 可以大大简化此过程。

事件处理程序如何定位它必须操作的对象?

有两种方法可以访问相关对象:

  • 由于这些方法是窗口(WPF)或窗体(WinForms)的实例方法,因此它们可以直接访问所有 UI 资源(即,您可以使用 this. MyLabel 在代码中)。

  • 第一个参数 sender 按照惯例是引发事件的 UI 控件。如果您对多个 UI 控件使用一个事件处理程序,这非常有用。

That depends on which UI library you are using. In WPF, you define the event handler in XAML:

<Button Click="HelloWorldClickEventHandler">Hello World</Button>

// this is in your .cs code file
private void HelloWorldClickEventHandler(object sender, RoutedEventArgs e)
{
    // some code
}

In WinForms, you attach the handlers in the constructor of your form:

public MyForm() {
    // ...
    HelloWorldButton.Click += new EventHandler(HelloWorldClickEventHandler);
}

private void HelloWorldClickEventHandler(object sender, EventArgs e)
{
    // some code
}

Of course, using a IDE such as Visual Studio simplifies this process a lot.

How can the event handler locate the objects it must manipulate?

There are two ways to access the relevant objects:

  • Since the methods are instance methods of the window (WPF) or form (WinForms), they can access all the UI resources directly (i.e., you can use this.MyLabel in your code).

  • The first parameter, sender, is by convention the UI control that raised the event. This is useful if you use one event handler for multiple UI controls.

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