2025-05-12 05:00:00

什么是回调?

什么是回调,它在c#中是如何实现的?


当前回答

我才刚认识你, 这很疯狂, 这是我的number (delegate) 所以如果发生了什么(事件), 打给我吧(回电话)?

其他回答

我才刚认识你, 这很疯狂, 这是我的number (delegate) 所以如果发生了什么(事件), 打给我吧(回电话)?

定义

回调是可执行的代码 作为参数传递给其他代码。

实现

// Parent can Read
public class Parent
{
    public string Read(){ /*reads here*/ };
}

// Child need Info
public class Child
{
    private string information;
    // declare a Delegate
    delegate string GetInfo();
    // use an instance of the declared Delegate
    public GetInfo GetMeInformation;

    public void ObtainInfo()
    {
        // Child will use the Parent capabilities via the Delegate
        information = GetMeInformation();
    }
}

使用

Parent Peter = new Parent();
Child Johny = new Child();

// Tell Johny from where to obtain info
Johny.GetMeInformation = Peter.Read;

Johny.ObtainInfo(); // here Johny 'asks' Peter to read

链接

c#的更多细节。

在计算机编程中,回调是作为参数传递给其他代码的可执行代码。 -维基百科:Callback(计算机科学)

c#有为此目的的委托。它们被大量用于事件,因为一个事件可以自动调用许多附加的委托(事件处理程序)。

回调工作步骤:

1)我们必须实现ICallbackEventHandler接口

2)注册客户端脚本:

 String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
    String callbackScript = "function UseCallBack(arg, context)" + "{ " + cbReference + ";}";
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallBack", callbackScript, true);

1)从UI调用在客户端点击调用javascript函数为EX:- builpopup(p1,p2,p3…)

Var finalfield= p1,p2,p3; UseCallBack (finalfield”、“);使用UseCallBack将客户端的数据传递给服务器端

在eventArgument中我们获取传递的数据 //执行一些服务器端操作并传递给"callbackResult"

3) GetCallbackResult() //使用此方法数据将被传递到客户端(ReceiveServerData()函数)端

callbackResult

4)获取客户端数据: ReceiveServerData(text),在文本服务器响应中,我们将得到。

回调是传递给另一个函数的函数指针。你正在调用的函数将在另一个函数完成时“回调”(执行)。

查看这个链接。