亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

任務擴展以應對應用范圍內的服務調用

任務擴展以應對應用范圍內的服務調用

C#
LEATH 2021-07-01 04:28:10
我在 xamarin 工作并嘗試使用一種方法使用所有服務。為此,我編寫了一個 TaskExtension。這樣我就可以從應用程序的每個頁面調用該擴展方法。這是為了禁用按鈕,顯示加載屏幕,響應處理并從一個角度滿足異常處理。我在下面附上我的代碼。需要您對此解決方案的專家意見這是我的擴展類public static class TaskExtensions{    public static async Task<ResultViewModel<U>> ExecuteAsyncOperation<U>(this Task<HttpResponseMessage> operation, object sender = null)    {        ResultViewModel<U> resultModel = new ResultViewModel<U>();        Button button = BeforeAsyncCall(sender);        try        {            await BackgroundOperation(operation, resultModel);        }        catch (Exception ex)        {            resultModel.Status = HttpStatusCode.InternalServerError;            resultModel.Errors = new List<string>() { "Some error occurred. Please try again." };        }        finally        {            AfterAsyncCall(button);        }        return resultModel;    }    static async Task BackgroundOperation<U>(Task<HttpResponseMessage> operation, ResultViewModel<U> resultModel)    {        HttpResponseMessage RawResult = await operation;        var Response = await RawResult.Content.ReadAsStringAsync();        resultModel.Status = RawResult.StatusCode;        if (RawResult.IsSuccessStatusCode)        {            var responseObj = await Task.Run(() => JsonConvert.DeserializeObject<U>(Response));            resultModel.Result = responseObj;        }        else        {            var responseErrorObj = await Task.Run(() => JsonConvert.DeserializeObject<ErrorModel>(Response));            resultModel.Errors = new List<string>();            foreach (var modelState in responseErrorObj.ModelState)            {                foreach (var error in modelState.Value)                {                    resultModel.Errors.Add(error.ToString());                }            }        }    }
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 162 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號