订阅对象基类中的事件 [英] Subscribe to event in object base class

查看:79
本文介绍了订阅对象基类中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来非常直观,我觉得我必须遗漏一些明显的东西......无论如何,这是我第一次尝试在基类中创建事件,所以我不知道。

在ListBox类中,我想为Clicked事件添加一个处理程序,该处理程序可以在Control类中找到(请参阅ListBox中的AddNewListBoxItem()方法)。处理程序应选择ListBoxItem,以便突出显示它。 Listbox和ListBoxItem都继承自Control类。



虽然事件在基类(Control)中触发,但事件为null,因此订阅者(ListBox) )永远不会得到消息。



此外,如果我的代码不专业或难以阅读,我道歉,我只是一个业余爱好者;欢迎您对我的实践提出建议和批评。



我试图发布所有代码,但是一个对话框警告我你的愤怒,所以代码也可以在这里找到 - > GitHub - MayhemusMaximus / MapEditor:地图编辑的XNA解决方案 [ ^ ]



控制类:

This seems so intuitive that I feel like I must be missing something obvious... Anyways, this is the first time I've tried to create events in a base class, so I don't know.
In the ListBox class I want to add a handler to the clicked event which can be found in the Control class (see the AddNewListBoxItem() method in ListBox). The handler should "select" the ListBoxItem, so that it will be drawn highlighted. Both the Listbox and the ListBoxItem inherit from Control class.

Although, the event fires in the base class (Control), the event is null, so the subscriber(ListBox) never gets the news.

Also, I apologize if my code is unprofessional or hard to read, I'm just a hobbyist; your recommendations and critiques regarding my practices are welcome also welcome.

I tried to post all of the code, but a dialog box warned me about your wrath, so the code can also be found here -> GitHub - MayhemusMaximus/MapEditor: An XNA Solution to Map Editing[^]

Control Class:

public delegate void Clicked(object sender, EventArgs e);
public event Clicked clicked;
public virtual void OnClicked(EventArgs e)
{
    if (clicked != null)
        clicked(this.child, e);
}


private void handleMouseButtonEvents()
{
    if (mouseIsOver)
    {
        if(Statics.PreviousMouseState.LeftButton == ButtonState.Pressed
            && Statics.CurrentMouseState.LeftButton == ButtonState.Released)
            OnClicked(EventArgs.Empty);
    }
}





ListBox类:



ListBox Class:

namespace MapEditor.Controls
{
    public class ListBox : Control
    {
     ...


        public void AddNewListBoxItem(Texture2D texture, string text, Color imageColor)
        {
        ...

            ListBoxItem listBoxItem = new ListBoxItem(image, label);

            listBoxItem.clicked += listBoxItem_clicked;

            this.ListBoxItems.Add(listBoxItem);

        }

        void listBoxItem_clicked(object sender, EventArgs e)
        {
            foreach(ListBoxItem listBoxItem in ListBoxItems)
            {
                if (listBoxItem.IsSelected)
                    listBoxItem.IsSelected = false;
            }

            ((ListBoxItem)sender).IsSelected = true;
        }

    }
}





我尝试过:



我已经从头开始重写了几次,在方法中移动订阅,搜索Google,并询问IT我的工作人员。



What I have tried:

I have rewritten this from scratch a couple of times, moved the subscription around within the method, searching Google, and asking I.T. staff at my work.

推荐答案

看看这是否给你一些想法:
See if this gives you some ideas:
using System;

namespace YourNameSpace
{
    public class EventClassBase
    {
        public Action<string> Notify;
    }

    public class EventClassInheritor : EventClassBase
    {
        public void TriggerEvent(string str)
        {
            if (Notify != null) Notify(str);
        }
    }
}

使用示例:在同一个NameSpace中的方法或EventHandler中插入代码

EventClassInheritor einh = new EventClassInheritor();

einh.Notify = s => { MessageBox.Show(s); }; 
           
einh.TriggerEvent("example of event call");

请记住,.NET'Action是一个功能齐全的委托(有些人认为它是代表核心的语义糖;我发现它非常好吃。)

Keep in mind that the .NET 'Action is a full-featured delegate (some would regard it as "semantic sugar" around a delegate core; I find it very tasty).


首先,非常感谢Sergey Alexandrovich Kryukov和BillWoodruff。我已按照建议更改了访问修饰符,我正在研究从EventArgs派生并使用Action。



代码无法按照我的意图执行,因为我从来没有调用代码来测试触发事件所需的标准。我正在尝试一些关于这个事件的新东西,所以我唯一的想法是我做得不对。我忽视了最简单的错误......
First of all, much thanks to Sergey Alexandrovich Kryukov and BillWoodruff. I have changed the access modifiers as suggested, and I am researching both deriving from EventArgs, and using Action.

The code failed to execute the way I intended because I never called the code that tests the criteria that need to be met to fire the event. I was trying something new to me with the event, so my only thought is that I wasn't doing it right. I overlooked the easiest bug of all...


这篇关于订阅对象基类中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆