代码之家  ›  专栏  ›  技术社区  ›  MC9000

将C#PageAsyncTask()转换为VB.Net等效项时出现问题

  •  0
  • MC9000  · 技术社区  · 7 年前

    错误是: “Task”类型的值不能转换为“Func(of Task)”

    不知道如何继续(我想我需要定义一个事件?)。 这是原始的C#代码

            protected void Page_Load(object sender, EventArgs e)
        {
            {
                AsyncMode = true;
                if (!dictionary.ContainsKey("accessToken"))
                {
                    if (Request.QueryString.Count > 0)
                    {
                        var response = new AuthorizeResponse(Request.QueryString.ToString());
                        if (response.State != null)
                        {
                            if (oauthClient.CSRFToken == response.State)
                            {
                                if (response.RealmId != null)
                                {
                                    if (!dictionary.ContainsKey("realmId"))
                                    {
                                        dictionary.Add("realmId", response.RealmId);
                                    }
                                }
    
                                if (response.Code != null)
                                {
                                    authCode = response.Code;
                                    output("Authorization code obtained.");
                                    PageAsyncTask t = new PageAsyncTask(performCodeExchange);
                                    Page.RegisterAsyncTask(t);
                                    Page.ExecuteRegisteredAsyncTasks();
                                }
                            }
                            else
                            {
                                output("Invalid State");
                                dictionary.Clear();
                            }
                        }
                    }
                }
                else
                {
                    homeButtons.Visible = false;
                    connected.Visible = true;
                }
            }
        }
    

    以及代码转换为:

            Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If True Then
                AsyncMode = True
                If Not dictionary.ContainsKey("accessToken") Then
                    If Request.QueryString.Count > 0 Then
                        Dim response = New AuthorizeResponse(Request.QueryString.ToString())
                        If response.State IsNot Nothing Then
                            If oauthClient.CSRFToken = response.State Then
                                If response.RealmId IsNot Nothing Then
                                    If Not dictionary.ContainsKey("realmId") Then
                                        dictionary.Add("realmId", response.RealmId)
                                    End If
                                End If
    
                                If response.Code IsNot Nothing Then
                                    authCode = response.Code
                                    output("Authorization code obtained.")
                                    Dim t As New PageAsyncTask(performCodeExchange)
                                    Page.RegisterAsyncTask(t)
                                    Page.ExecuteRegisteredAsyncTasks()
                                End If
                            Else
                                output("Invalid State")
                                dictionary.Clear()
                            End If
                        End If
                    End If
                Else
                    homeButtons.Visible = False
                    connected.Visible = True
                End If
            End If
        End Sub
    

    Dim t As New PageAsyncTask(performCodeExchange)
    

    任务函数是performcodexchange,它返回一个任务

        Public Async Function performCodeExchange() As Task
        output("Exchanging code for tokens.")
        Try
            Dim tokenResp = Await oauthClient.GetBearerTokenAsync(authCode)
            If Not _dictionary.ContainsKey("accessToken") Then
                _dictionary.Add("accessToken", tokenResp.AccessToken)
            Else
                _dictionary("accessToken") = tokenResp.AccessToken
            End If
    
            If Not _dictionary.ContainsKey("refreshToken") Then
                _dictionary.Add("refreshToken", tokenResp.RefreshToken)
            Else
                _dictionary("refreshToken") = tokenResp.RefreshToken
            End If
    
            If tokenResp.IdentityToken IsNot Nothing Then
                idToken = tokenResp.IdentityToken
            End If
            If Request.Url.Query = "" Then
                Response.Redirect(Request.RawUrl)
            Else
                Response.Redirect(Request.RawUrl.Replace(Request.Url.Query, ""))
            End If
        Catch ex As Exception
            output("Problem while getting bearer tokens.")
        End Try
    End Function
    

    为了彻底起见,原始C#代码:

           public async Task performCodeExchange()
        {
            output("Exchanging code for tokens.");
            try
            {
                var tokenResp = await oauthClient.GetBearerTokenAsync(authCode);
                if (!dictionary.ContainsKey("accessToken"))
                    dictionary.Add("accessToken", tokenResp.AccessToken);
                else
                    dictionary["accessToken"] = tokenResp.AccessToken;
    
                if (!dictionary.ContainsKey("refreshToken"))
                    dictionary.Add("refreshToken", tokenResp.RefreshToken);
                else
                    dictionary["refreshToken"] = tokenResp.RefreshToken;
    
                if (tokenResp.IdentityToken != null)
                    idToken = tokenResp.IdentityToken;
                if (Request.Url.Query == "")
                {
                    Response.Redirect(Request.RawUrl);
                }
                else
                {
                    Response.Redirect(Request.RawUrl.Replace(Request.Url.Query, ""));
                }
            }
            catch (Exception ex)
            {
                output("Problem while getting bearer tokens.");
            }
        }
    

    我不知道该怎么办-把代表交上来?如何使用任务(在VB.Net中)完成此任务?

    1 回复  |  直到 7 年前
        1
  •  4
  •   Kevin Gosse    7 年前

    执行时 PageAsyncTask t = new PageAsyncTask(performCodeExchange); 在C#语言中,指向 performCodeExchange 方法被隐式创建并传递给 PageAsyncTask

    现在声明 Dim t As New PageAsyncTask(performCodeExchange) Dim t As New PageAsyncTask(performCodeExchange()) . 也就是说 页面异步任务 接受评估结果 性能代码交换 而不是方法的委托。

    AdressOf 关键字。代码应重写为:

    Dim t As New PageAsyncTask(AddressOf performCodeExchange)