无效的表达式术语'string'事件和委托中的错误



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

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!!!!!!!!!!!!!!!!
                }
            }

,但它显示了错误无效的表达式术语字符串。如果我不提供字符串参数,那么它显示不接受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;
        }
    }

你必须注册这个事件

 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

1)不要使用静态事件。

c#中静态事件的负面影响/不良实践

2)使用事件设计

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

的例子:

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; }
}

相关内容

最新更新