在C#XNA中声明、实例化和使用委托
我试着按照标题的意思去做,但是我感到困惑 我知道代理应该如何工作,但VisualStudio告诉我我错了。说明如何实现这一点的Microsoft文档包含一个复杂的示例,该示例使用一个书店程序,该程序包含模板和一系列逻辑代码,因此很难理解在C#XNA中声明、实例化和使用委托,c#,visual-studio-2008,xna,C#,Visual Studio 2008,Xna,我试着按照标题的意思去做,但是我感到困惑 我知道代理应该如何工作,但VisualStudio告诉我我错了。说明如何实现这一点的Microsoft文档包含一个复杂的示例,该示例使用一个书店程序,该程序包含模板和一系列逻辑代码,因此很难理解 你是怎么做到的?谢谢。委托是一个安全的函数指针,您应该为该名称的声明变量分配一个方法,而不是尝试分配您正在执行的类型本身 class MyGameClass { SetGameAreaDelegate handler; MyGameClass()
你是怎么做到的?谢谢。委托是一个安全的函数指针,您应该为该名称的声明变量分配一个方法,而不是尝试分配您正在执行的类型本身
class MyGameClass
{
SetGameAreaDelegate handler;
MyGameClass()
{
// Instantiate the handler (since my callback is a non-static method)
// You'll need to preform this assignment in the constructor, 'this'
// is not valid during initialization
handler = new SetGameAreaDelegate(myGameAreaWithCallback);
handler = MyGameAreaWithCallback; // short for above
}
void MyGameAreaWithCallback(Game1.gameAreas newArea)
{
//...
}
}
更新:有关代表的详细信息
委托是函数指针的托管包装器。它有自己的类型签名,可能是原始函数指针的另一种保存方式。委托可以像C++样式成员函数指针那样保存对实例对象的引用,但是您不必担心这一点,因为运行时为您找出了这种信息。
最好知道非静态方法的委托将跟踪对该对象的引用。这可能导致内存不会被垃圾收集,因为委托虽然看起来无害,但会维护或跟踪对象引用
代码的问题是类型签名
void SetGameAreaWithCallback(Game1.gameAreas newArea, SetGameAreaDelegate callback)
…与您的代理类型不匹配
delegate void SetGameAreaDelegate(Game1.gameAreas newArea);
…为了让它起作用
SetGameAreaDelegate handler = SetGameAreaWithCallback;
…您的代表应该是
delegate void SetGameAreaDelegate(Game1.gameAreas newArea, SetGameAreaDelegate callback);
…如果这是您真正的意思,那么您忘记了一个参数,这就是方法解析失败的原因。委托是一个安全的函数指针,您应该将一个方法分配给该名称的声明变量,而不是尝试分配您正在执行的类型本身
class MyGameClass
{
SetGameAreaDelegate handler;
MyGameClass()
{
// Instantiate the handler (since my callback is a non-static method)
// You'll need to preform this assignment in the constructor, 'this'
// is not valid during initialization
handler = new SetGameAreaDelegate(myGameAreaWithCallback);
handler = MyGameAreaWithCallback; // short for above
}
void MyGameAreaWithCallback(Game1.gameAreas newArea)
{
//...
}
}
更新:有关代表的详细信息
委托是函数指针的托管包装器。它有自己的类型签名,可能是原始函数指针的另一种保存方式。委托可以像C++样式成员函数指针那样保存对实例对象的引用,但是您不必担心这一点,因为运行时为您找出了这种信息。
最好知道非静态方法的委托将跟踪对该对象的引用。这可能导致内存不会被垃圾收集,因为委托虽然看起来无害,但会维护或跟踪对象引用
代码的问题是类型签名
void SetGameAreaWithCallback(Game1.gameAreas newArea, SetGameAreaDelegate callback)
…与您的代理类型不匹配
delegate void SetGameAreaDelegate(Game1.gameAreas newArea);
…为了让它起作用
SetGameAreaDelegate handler = SetGameAreaWithCallback;
…您的代表应该是
delegate void SetGameAreaDelegate(Game1.gameAreas newArea, SetGameAreaDelegate callback);
…如果这是您真正的意思,您忘记了一个参数,这就是方法解析失败的原因。在您的示例中,我假设您希望
SetGameAreaWithCallback
方法在Game1
的实例上实际调用changegamea
方法
为此,您需要创建委托实例,以便它引用该方法:
// game1 is the instance of the Game1 class that you want to call
// Instantiate the handler
SetGameAreaDelegate handler = new SetGameAreaDelegate(game1.changeGameArea);
如果您使用的是C#2或更高版本,则语法更简单:
// game1 is the instance of the Game1 class that you want to call
// Instantiate the handler
SetGameAreaDelegate handler = game1.changeGameArea;
在您的示例中,我假设您希望
SetGameAreaWithCallback
方法在Game1
的实例上实际调用changeGameArea
方法
为此,您需要创建委托实例,以便它引用该方法:
// game1 is the instance of the Game1 class that you want to call
// Instantiate the handler
SetGameAreaDelegate handler = new SetGameAreaDelegate(game1.changeGameArea);
如果您使用的是C#2或更高版本,则语法更简单:
// game1 is the instance of the Game1 class that you want to call
// Instantiate the handler
SetGameAreaDelegate handler = game1.changeGameArea;
约翰,我显然没有正确地理解你,因为你在上面所做的正是我已经拥有的:/对,我忽略了一些东西,嗯,类型签名不匹配。。。在方法中有一个附加参数,该参数不在委托中。“SetGameAreaDelegate callback”John,我显然没有正确理解你,因为你在上面所做的正是我已经拥有的:/Right,我忽略了一些东西,嗯,类型签名不匹配。。。在方法中有一个附加参数,该参数不在委托中。“SetGameAreaDelegate callback”谢谢你,卢克,这比我想做的要简单得多:D谢谢你,卢克,这比我想做的要简单得多:D