翻譯|使用教程|編輯:陳津勇|2019-08-28 15:10:07.833|閱讀 332 次
概述:本文詳細介紹了在Xamarin.iOS、Xamarin.Android和Xamarin.Forms組件套包Essential Studio for Xamarin中使用ASP.NET Core Web API的詳細方法,歡迎閱讀了解。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
如題,這是從Web API訪問數據并在移動應用程序中顯示數據的最常見要求。要構建使用ASP.NET Core Web API或休息服務的Xamarin應用程序,我們需要HttpClient發送HTTP請求并從URI標識的Web服務接收HTTP響應。
在本文中,小編將通過6個簡單的步驟向大家展示在Xamarin應用程序中使用ASP.NET Core Web API的方法。
點擊下載Essential Studio for Xamarin試用版
步驟1:創建ASP.NET Core Web API服務或休息服務。
請閱讀文章1、文章2參考創建ASP.NET Core Web API服務并將其托管以供公共訪問。
步驟2:創建一個幫助程序類以使用API服務并返回數據。
創建一個幫助器類,并使用異步方法RefreshDataAsync將其命名為WebAPIService,并使用API服務URI。
WebAPIUrl =“//ej2services.syncfusion.com/production/web-services/api/Orders”; //在此處設置REST API URL。var uri = new Uri(WebAPIUrl);
第3步:傳遞服務URL以處理HttpClient get操作。
在基本URL上使用GetAsync來檢索訂單數組,使用C#await選項可以輕松使用該值。
將返回的對象傳遞給JsonConvert.DeserializeObject,將JSON數據轉換為訂單對象,并將數據返回給服務調用者。
public async System.Threading.Tasks.Task RefreshDataAsync(){ WebAPIUrl =“//ej2services.syncfusion.com/production/web-services/api/Orders”; //在此處設置REST API URL。 var uri = new Uri(WebAPIUrl); 嘗試 { var response = await client.GetAsync(uri); if(response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync(); Items = JsonConvert.DeserializeObject (content); 退換貨品; } } catch(Exception ex) { } return null;}
步驟4:創建一個模型類,其中包含從服務調用接收的對象的數據結構。
例如,創建一個Order類,該類包含從演示服務調用接收的對象的數據結構。
公共課程{ public int OrderID {get; 組; } public string CustomerID { get; set; } public int EmployeeID { get; set; } public double Freight { get; set; } public string ShipCity { get; set; } public bool Verified { get; set; } public DateTime OrderDate { get; set; } public string ShipName { get; set; } public string ShipCountry { get; set; } public DateTime ShippedDate { get; set; } public string ShipAddress { get; set; }}
步驟5:創建調用服務調用并接收數據的視圖模型。
創建一個名為OrdersViewModel的視圖模型,其中包含一個異步方法GetData來調用服務調用,并將接收到的數據存儲在適當的集合中。
public class OrdersViewModel : INotifyPropertyChanged{ #region Fields WebAPIService webAPIService; public event PropertyChangedEventHandler PropertyChanged; private ObservableCollection items; #endregion #region Properties public ObservableCollection Items { get { return items; } set { items = value; RaisepropertyChanged("Items"); } } #endregion #region Constructor public OrdersViewModel() { webAPIService = new WebAPIService(); //Item source that needs to be displayed on the list view. items = new ObservableCollection(); GetData(); } #endregion #region Methods async void GetData() { Items = await webAPIService.RefreshDataAsync(); } void RaisepropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion}
步驟6:添加引用并利用可綁定控件的項集合來使用和顯示接收的數據。
在這里,添加對Syncfusion ListView控件的引用以用于演示目的。它可以接收項目集合并使用自定義數據模板顯示它們。
<ContentPage xmlns="//xamarin.com/schemas/2014/forms" xmlns:x="//schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:SfListViewSample" xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms" x:Class="SfListViewSample.MainPage" Padding="10"> <syncfusion:SfListView x:Name="listView" ItemSize="90" ItemSpacing="5" Grid.Row="1" BackgroundColor="AliceBlue" ItemsSource="{Binding Items}">
輸出
以上就是在Essential Studio for Xamarin應用程序中使用ASP.NET Core Web API的全部步驟啦,希望能幫助到你!如果你有什么不明白或有其他建議,歡迎在下方留言告訴小編哦~
想要了解Essential Studio for Xamarin更多資源的伙伴,請點這里。
想要獲取Essential Studio for Xamarin正版授權的伙伴,
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn