簡體   English   中英

Xamarin.Forms服務退貨清單<t>加上狀態碼</t>

[英]Xamarin.Forms Service return List<T> plus Status Code

這是我生成的服務的代碼。一切正常。 我得到了照片列表,但我想獲取 status_code 並在 ViewModel 中創建邏輯並向用戶顯示消息。

 public async Task<IList<Photo>> GetAllPosts()
        {
            try
            {
                if (CrossConnectivity.Current.IsConnected)
                {
                    instagramCloneClient.DefaultRequestHeaders.Clear();
                    instagramCloneClient.DefaultRequestHeaders.Add("Accept", "application/json");
                    var response = await instagramCloneClient.GetAsync($"/photos");
                    var status_code=(int)response.StatusCode;
                    if (response.IsSuccessStatusCode)
                    {
                        string jsonMessage;
                        using (Stream responseStream = await response.Content.ReadAsStreamAsync())
                        {
                            jsonMessage = new StreamReader(responseStream).ReadToEnd();
                        }

                        IList<Photo> photos = JsonConvert.DeserializeObject<IList<Photo>>(jsonMessage);
                        return photos;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {

                    return null;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                string error = ex.Message;
                return null;
            }
        } 

我返回照片列表。 我還希望對ViewModelresponse.StatusCode進行調用,我從中調用 function。我必須返回 IList,如何包含一些int status_code 最佳做法是什么?

您有多種選擇。 您可以使用您的列表和 StatusCode 的整數/枚舉創建一個新的 model。 另一種選擇是返回 ArrayList 的列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM