0

我需要一个在 CallbackMethodSendRegistration 中传输到 AsyncSendRegistrationMethod 的标签。

private delegate ResponceFromServer AsyncSendRegistrationDelegate(RegistrationToUser registrationToUser, Label label);
private ResponceFromServer AsyncSendRegistrationMethod(RegistrationToUser registrationToUser, Label label)
{
    SetText(label,  registrationToUser.Name + " registration...");


    return Requests.DataBase.Authorization.Registration(
        registrationToUser.Name, 
        registrationToUser.IdRoleUser, 
        registrationToUser.IdGroup);
}
private void CallbackMethodSendRegistration(IAsyncResult ar)
{
    var sendRegistrationDelegate = (AsyncSendRegistrationDelegate)ar.AsyncState;

    var responceFromServer = (ResponceFromServer)sendRegistrationDelegate.EndInvoke(ar);
    if(responceFromServer.IsError)
    {
       //here need label.Text
    }
    else
    {

    }
}
4

1 回答 1

3

获取对传递给 sendRegistrationDelegate 的标签的引用的一种方法是将回调设置为 lambda。在呼叫站点,这将如下所示:

    var registrationToUser = ...;
    var label = ...;

    sendRegistrationDelegate.BeginInvoke(registrationToUser, label, ar =>
    {
        var responceFromServer  = sendRegistrationDelegate.EndInvoke(ar);
        if (responceFromServer.IsError)
        {
            label.Text = "";
        }
        else
        {

        }
    }, null);
于 2011-07-02T01:00:21.713 回答