如何解决或解决此委托事件问题?

时间:2019-01-25 11:32:59

标签: c#

namespace Rocket.Common
{
    using System;
    using System.Threading;

    public class SharedProperties
    {
        public static bool snapEnabled = false;
        public static float snapValue = 0.25f;
        public static bool useAxisConstraints = true;

        public static  event PushToGridEventHandler PushToGridEvent
        {
            add
            {
                PushToGridEventHandler pushToGridEvent = PushToGridEvent;
                while (true)
                {
                    PushToGridEventHandler a = pushToGridEvent;
                    PushToGridEventHandler handler3 = (PushToGridEventHandler) Delegate.Combine(a, value);
                    pushToGridEvent = Interlocked.CompareExchange(ref PushToGridEvent, handler3, a);
                    if (ReferenceEquals(pushToGridEvent, a))
                    {
                        return;
                    }
                }
            }
            remove
            {
                PushToGridEventHandler pushToGridEvent = PushToGridEvent;
                while (true)
                {
                    PushToGridEventHandler source = pushToGridEvent;
                    PushToGridEventHandler handler3 = (PushToGridEventHandler) Delegate.Remove(source, value);
                    pushToGridEvent = Interlocked.CompareExchange(ref PushToGridEvent, handler3, source);
                    if (ReferenceEquals(pushToGridEvent, source))
                    {
                        return;
                    }
                }
            }
        }

        public static void PushToGrid(float snapValue)
        {
            if (PushToGridEvent != null)
            {
                PushToGridEvent(snapValue);
            }
        }

        public delegate void PushToGridEventHandler(float snapValue);
    }
}

有人可以帮助我解决这个难题。我似乎无法解决这个错误。

我在以下几行中遇到错误;

  1. (16,58):错误CS0079:事件Rocket.Common.SharedProperties.PushToGridEvent仅出现在+=-=运算符的左侧
  2. (21,71):错误CS0079:事件Rocket.Common.SharedProperties.PushToGridEvent仅出现在+=-=运算符的左侧
  3. (30,58):错误CS0079:事件Rocket.Common.SharedProperties.PushToGridEvent仅出现在+=-=运算符的左侧
  4. (35,71):错误CS0079:事件Rocket.Common.SharedProperties.PushToGridEvent仅出现在+=-=运算符的左侧
  5. (46,17):错误CS0079:事件Rocket.Common.SharedProperties.PushToGridEvent仅出现在+=-=运算符的左侧
  6. (48,17):错误CS0079:事件Rocket.Common.SharedProperties.PushToGridEvent仅出现在+=-=运算符的左侧

一次又一次地出现相同的错误。我已经在网上搜索了很多关于此错误的解释,但我似乎找不到正确的错误。还是我今天过得很糟糕:)

请帮帮我

1 个答案:

答案 0 :(得分:0)

这是因为您已经声明了addremove,为了使代码正常工作,您必须声明一个后备字段(私有),就像使用get和/或{ {1}},然后您就可以使用此私有字段进行调用。

所以你应该有这样的东西

set