如何从同一类的静态函数中调用公共事件? [英] How to call a public event from a static function in the same class?

查看:81
本文介绍了如何从同一类的静态函数中调用公共事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含另一个类的ObservableCollection的类.我想通知是否更改了一个类成员,因为我需要在MediaCollection类中进行一些计算.因此,我在该课程中添加了一个事件:

I have a class that contains an ObservableCollection of another class. I want to be notified if one of the class members is changed, because I need to do some calculating in the MediaCollection class. So I added an event to that class:

public event PropertyChangedEventHandler PropertyChangedEvent;

在此收集类中被调用:

public class MediaCollection : INotifyPropertyChanged
{
    private List<MediaEntry> ModifiedItems = new List<MediaEntry>();
    private ObservableCollection<MediaEntry> tagList = new ObservableCollection<MediaEntry>();

    public MediaCollection()
    {
        tagList = new ObservableCollection<MediaEntry>();
        tagList.CollectionChanged += CollectionChangedHandler;
    }

    public void CollectionChangedHandler(object sender, NotifyCollectionChangedEventArgs e)
    {
            foreach (MediaEntry newItem in e.NewItems)
            {
                ModifiedItems.Add(newItem);
                newItem.PropertyChangedEvent += OnItemPropertyChanged;
            }
    ...
    }

    public void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        MediaEntry item = sender as MediaEntry; 
        if (item != null)       
            ModifiedItems.Add(item);
    }

MediaEntry类看起来像这样:

The MediaEntry class looks something like this:

public class MediaEntry : DependencyObject
{
    public event PropertyChangedEventHandler PropertyChangedEvent;

    public bool IsError
    {
        get { return (bool)GetValue(IsErrorProperty); }
        set { SetValue(IsErrorProperty, value); }
    }
    public static readonly DependencyProperty IsErrorProperty =
        DependencyProperty.Register("IsError", typeof(bool), typeof(MediaEntry), new 
        UIPropertyMetadata(PropertyChanged));

    public static void PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        if (obj is MediaEntry)
        {
            ((MediaEntry)obj).ObjectPropertyChanged(args);
        }
    }

此调用将通知UI等,但是要向容器类引发事件,我需要引发PropertyChangedEvent(在容器类中侦听).根据文档,我需要添加以下行:

This call will notify the UI, etc. but to raise event to the container class I need to raise my PropertyChangedEvent (which is listened for in the container class). According to documentation I need to add these lines:

public static void PropertyEventChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
    PropertyChangedEventHandler handler = PropertyChangedEvent;
    if (handler != null)
    {
        handler(obj, new PropertyChangedEventArgs(args.Property.Name));
    }
}

我需要从公共静态void PropertyChanged函数进行调用.但是,这是一个真正的问题,如何在我的静态函数中调用公共事件?

Which I need to call from the public static void PropertyChanged function. However, and here is the real question, how can I call the public event from within my static function?

我尝试了很多类似的事情:

I have tried many, many things like:

  • 将公共事件PropertyChangedEventHandler更改为公共静态事件.这将产生如下错误:"Member MediaEntry.PropertyChangedEvent无法通过实例引用进行访问;而是使用类型名称来限定它".

  • Changing the public event PropertyChangedEventHandler to a public static event. This will give an error like this: "Member MediaEntry.PropertyChangedEvent cannot be accessed with an instance reference; qualify it with a type name instead"

将公共静态void PropertyChanged更改为非静态版本,但这会在所有UIPropertyMetadata(PropertyChanged))部分上产生错误,并显示以下错误消息:非静态字段需要对象引用,方法或属性"

Changing the public static void PropertyChanged to a non-static version, but this will give errors on all UIPropertyMetadata(PropertyChanged)) parts with this error message: "An object reference is required for the non-static field, method, or property"

还有更多,但无济于事.

And some more, but all to no avail.

我不知何故认为这里需要代表,但现在不知道如何开始或在哪里开始.非常感谢您在这里解决我的问题.

I somehow figure that I need delegates here, but don't now how or where to start. Any help is greatly appreciated in solving my problem here.

推荐答案

注册通过IsPropertyMetadata传递给它的IsError DependencyProperty时,您正在设置在属性更改时将调用的方法.您的情况是

When you register the IsError DependencyProperty passing it the UIPropertyMetadata, you are setting the method that will be called when the property will change. In your case it is

public static void PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)

其属性已更改的实例作为obj传递给此方法,并且在args中找到已更改的属性以及旧值和新值.这就是您必须调用事件的地方.您只是缺少ObjectPropertyChanged的实现,该实现不是静态的,因为您正在使用传递给铸造为MediaEntry的PropertyChanged方法的参数.该实现与您尝试使用PropertyEventChanged相似,唯一的区别是它不是静态的,并且您没有将任何对象传递给它:

The instance for which the property has changed is passed as obj to this method, and in the args you find the property that changed and the old and new value. This meis the place where you have to call your event. You are just missing the implementation of ObjectPropertyChanged, which is not static, since you are using the argument passed to your PropertyChanged method casted to MediaEntry. The implementation is similar to what you tried with PropertyEventChanged, the only difference being the fact that it is not static and that you don't pass any object to it:

public void ObjectPropertyChanged(DependencyPropertyChangedEventArgs args)
{
    PropertyChangedEventHandler handler = PropertyChangedEvent;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(args.Property.Name));
    }
}

顺便说一句,我会尝试使用更好的名称,因为当您阅读PropertyEventChanged和PropertyChangedEvent以及Property和Changed的许多不同组合时,很容易感到困惑.

BTW I would try to use better names, since it is very easy to get confused when you read PropertyEventChanged and PropertyChangedEvent and many different combination of Property and Changed :-).

这篇关于如何从同一类的静态函数中调用公共事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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