【问题标题】:.NET c# - how to pass aditional data to PropertyChangedEventHandler.NET c# - 如何将附加数据传递给 PropertyChangedEventHandler
【发布时间】:2019-11-17 00:11:49
【问题描述】:

我有这个代码:

public void SomeMethod() 
{
    MyClass clss = new MyClass();    //note: MyClass implements INotifyPropertyChanged
    clss.DoSomething();
    clss.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MyEventHandler);
}
static void MyEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
{
    Debug.WriteLine("some property on MyClass has changed!");
}

这一切正常,当 SomeClass 中的属性发生变化时,MyEventHandler() 会运行。 但是现在我需要将附加数据从 SomeMethod() 传递到 MyEventHandler() ,我该怎么做?

* 更新 *

好吧,我想我应该更好地解释整个问题:MyClass 中的方法 DoSomething() 调用外部 Web 服务,将回调传递给它,因此当 Web 服务完成工作时,它将调用回调,将操作结果的值传递给它。在该回调中,我正在更改该类的一个属性,以便为其分配从 Web 服务接收到的值,从而触发 propertyChanged 事件。 然后在调用者类中,我订阅了那个事件,这样当它发生时我可以做一些事情。

最终的目标是,在调用DoSomething()之后,能够等到Web服务完成它的工作并返回一个结果,这样我就可以在数据库中保存一些东西等等,然后才从@返回987654330@...

所以这是 MyClass,简化版:

public class MyClass : INotifyPropertyChanged
{
    private long _wsReturnValue;
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    public long wsReturnValue 
    {
        get { return _wsReturnValue; }
        set {
            _wsReturnValue = value;
            OnPropertyChanged("wsReturnValue");
        }
    }
    protected void OnPropertyChanged(string name) 
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) 
            handler(this, new System.ComponentModel.PropertyChangedEventArgs(name));
    } 

    public void DoSomething(object entity) 
    {
        //here I just call external web service and returns, the webservice will call TheCallback() when finished
    }

    public void TheCallback(CommunicationEventArgs e) 
    {
        this.wsReturnValue = e.res;
    }

}        

这是使用 MyClass 的类:

class MainClass
{
    public void SomeMethod(object someObject)
    {
        MyClass clss = new MyClass();    //note: MyClass implements INotifyPropertyChanged
        clss.DoSomething(someObject);   //someObject contains data that I want to use later in the event handler
        clss.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MyEventHandler);
    }

    private static void MyEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        //here I need to use the object someObject...
    }
}

【问题讨论】:

  • 需要传递哪些信息?你想完成什么?
  • 你可以实现你自己的版本,比如this

标签: c# .net event-handling


【解决方案1】:

不确定这是否与你的意思相近,但你去吧;

class EventClass
    {
        public void SomeMethod()
        {
            MyClass clss = new MyClass();    //note: MyClass implements INotifyPropertyChanged
            clss.DoSomething(new object());
            clss.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MyEventHandler);
        }

        private static void MyEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var customeEventArgs = (CustomEventArgs) e;
            Debug.WriteLine("some property on MyClass has changed! Extra Data : {0}", customeEventArgs.ExtraData);
        }
    }

实现了更多的shell代码来说明

internal class MyClass
    {
        public void DoSomething(object data)
        {

            var e = new CustomEventArgs("Property")
            {
                ExtraData = data
            };

            PropertyChanged?.Invoke(this, e);
        }

        public event PropertyChangedEventHandler PropertyChanged;


    }

internal class CustomEventArgs : PropertyChangedEventArgs
    {
        public CustomEventArgs(string propertyName) : base(propertyName)
        {
        }

        public object ExtraData { get; set; }
    }

有什么帮助吗? :)

【讨论】:

  • 你的解决方案可能没问题,你创建一个自定义事件来传递一个额外的参数:)。
  • 谢谢,我已经尝试过了,但我一定是做错了什么......请查看我更新的问题,了解有关整个问题的更多信息
  • @patsy2k 从我可以看到您所做的更改不包括使用“自定义”事件参数类来携带额外数据?
猜你喜欢
  • 2019-04-11
  • 1970-01-01
  • 1970-01-01
  • 2015-03-03
  • 2023-04-06
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多