【问题标题】:Invalid expression term 'string' error in event and delegates事件和委托中的无效表达式术语“字符串”错误
【发布时间】:2015-12-04 22:30:21
【问题描述】:

使用事件和委托我已经尝试过了。

public delegate void ColourChangerAction(string str);
public static event ColourChangerAction OnClicked;
void OnGUI() { 
            if(GUI.Button(new Rect(Screen.width/2, Screen.height-200, 50,50),texture1)){
                if (OnClicked != null) {
                    OnClicked(string strr);///error here!!!!!!!!!!!!!!!!
                }
            }

但它向我显示错误 Invalide expression term string。如果我不提供字符串参数,那么它显示不需要 0 参数。

好吧,我是代表和活动的新手。我错过了什么。??

在另一个课程中,我这样做是为了获取事件调用。

 EventManager.OnClicked += ColourChanger;//registering 
 public void ColourChanger(string colourName)
    {
        print("Colour Changer " + colourName);
        if (colourName == "Red")
        {
            print(gameObject.name);
            hitInfo.collider.gameObject.renderer.material.color = Color.red;
        }
        else if (colourName == "Green")
        {
            print(gameObject.name);
            hitInfo.collider.gameObject.renderer.material.color = Color.green;
        }
        else if (colourName == "Yellow")
        {
            print(gameObject.name);
            hitInfo.collider.gameObject.renderer.material.color = Color.yellow;
        }
    }

【问题讨论】:

    标签: c# events delegates


    【解决方案1】:

    1) 不要使用静态事件。

    Negative Aspects/Bad Practice of Static Event in C#

    2) 使用Event Design

    请使用 System.EventHandler 而不是手动创建新的委托作为事件处理程序。

    Example:

    static void Main(string[] args)
    {
        Counter c = new Counter( ... );
        c.ThresholdReached += c_ThresholdReached;
        ...
    }
    
    static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
    {
        Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold,  e.TimeReached);
        ...
    }
    
    
    class Counter
    {
        ...
        public void Add(int x)
        {
            total += x;
            if (total >= threshold)
            {
                ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
                args.Threshold = threshold;
                args.TimeReached = DateTime.Now;
                OnThresholdReached(args);
            }
        }
    
        protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
        {
            //Thread safe event
            EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
            if (handler != null)
            {
                handler(this, e);
            }
        }
    
        public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
    }
    
    public class ThresholdReachedEventArgs : EventArgs
    {
        public int Threshold { get; set; }
        public DateTime TimeReached { get; set; }
    }
    

    【讨论】:

      【解决方案2】:

      你必须注册那个事件

       OnClickedE += new ColourChangerAction(OnGUI);
      

      您必须重新定义订阅此事件的方法签名。

      void OnGUI(string str)
      {
          if (1 == 1)
          {
              if (OnClickedE != null)
              {
                  //  OnClicked(string strr);///error here!!!!!!!!!!!!!!!!
              }
          }
      }
      

      因此您的整个代码将如下所示:

      //Defining Delegate and event
      public delegate void ColourChangerAction(string str);
      public static event ColourChangerAction OnClicked;
      // Method signature for subscribe the event
         static void OnGUI(string str)
         {
          if (1 == 1)
          {
              if (OnClickedE != null)
              {
                 // perform your action here
              }
          }
         }
      //register the event 
      OnClicked += new ColourChangerAction(OnGUI);
      // Invoke the event dynamically
       OnClicked.DynamicInvoke("Some string");
      

      更新:

      在您的情况下,请将代码 OnClicked(string strr); 替换为

      OnClicked.DynamicInvoke("red");// pass the color here
      

      【讨论】:

      • 您现在可以查看答案中的更新以获取解决方案
      猜你喜欢
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      • 2020-09-23
      • 2014-06-01
      • 1970-01-01
      相关资源
      最近更新 更多