翻譯|使用教程|編輯:陳津勇|2019-08-28 15:10:07.833|閱讀 332 次
概述:本文詳細(xì)介紹了在Xamarin.iOS、Xamarin.Android和Xamarin.Forms組件套包Essential Studio for Xamarin中使用ASP.NET Core Web API的詳細(xì)方法,歡迎閱讀了解。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
如題,這是從Web API訪問(wèn)數(shù)據(jù)并在移動(dòng)應(yīng)用程序中顯示數(shù)據(jù)的最常見要求。要構(gòu)建使用ASP.NET Core Web API或休息服務(wù)的Xamarin應(yīng)用程序,我們需要HttpClient發(fā)送HTTP請(qǐng)求并從URI標(biāo)識(shí)的Web服務(wù)接收HTTP響應(yīng)。
在本文中,小編將通過(guò)6個(gè)簡(jiǎn)單的步驟向大家展示在Xamarin應(yīng)用程序中使用ASP.NET Core Web API的方法。
點(diǎn)擊下載Essential Studio for Xamarin試用版
步驟1:創(chuàng)建ASP.NET Core Web API服務(wù)或休息服務(wù)。
請(qǐng)閱讀文章1、文章2參考創(chuàng)建ASP.NET Core Web API服務(wù)并將其托管以供公共訪問(wèn)。
步驟2:創(chuàng)建一個(gè)幫助程序類以使用API服務(wù)并返回?cái)?shù)據(jù)。
創(chuàng)建一個(gè)幫助器類,并使用異步方法RefreshDataAsync將其命名為WebAPIService,并使用API服務(wù)URI。
WebAPIUrl =“//ej2services.syncfusion.com/production/web-services/api/Orders”; //在此處設(shè)置REST API URL。var uri = new Uri(WebAPIUrl);
第3步:傳遞服務(wù)URL以處理HttpClient get操作。
在基本URL上使用GetAsync來(lái)檢索訂單數(shù)組,使用C#await選項(xiàng)可以輕松使用該值。
將返回的對(duì)象傳遞給JsonConvert.DeserializeObject,將JSON數(shù)據(jù)轉(zhuǎn)換為訂單對(duì)象,并將數(shù)據(jù)返回給服務(wù)調(diào)用者。
public async System.Threading.Tasks.Task RefreshDataAsync(){ WebAPIUrl =“//ej2services.syncfusion.com/production/web-services/api/Orders”; //在此處設(shè)置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:創(chuàng)建一個(gè)模型類,其中包含從服務(wù)調(diào)用接收的對(duì)象的數(shù)據(jù)結(jié)構(gòu)。
例如,創(chuàng)建一個(gè)Order類,該類包含從演示服務(wù)調(diào)用接收的對(duì)象的數(shù)據(jù)結(jié)構(gòu)。
公共課程{ 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:創(chuàng)建調(diào)用服務(wù)調(diào)用并接收數(shù)據(jù)的視圖模型。
創(chuàng)建一個(gè)名為OrdersViewModel的視圖模型,其中包含一個(gè)異步方法GetData來(lái)調(diào)用服務(wù)調(diào)用,并將接收到的數(shù)據(jù)存儲(chǔ)在適當(dāng)?shù)募现小?/p>
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:添加引用并利用可綁定控件的項(xiàng)集合來(lái)使用和顯示接收的數(shù)據(jù)。
在這里,添加對(duì)Syncfusion ListView控件的引用以用于演示目的。它可以接收項(xiàng)目集合并使用自定義數(shù)據(jù)模板顯示它們。
<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應(yīng)用程序中使用ASP.NET Core Web API的全部步驟啦,希望能幫助到你!如果你有什么不明白或有其他建議,歡迎在下方留言告訴小編哦~
想要了解Essential Studio for Xamarin更多資源的伙伴,請(qǐng)點(diǎn)這里。
想要獲取Essential Studio for Xamarin正版授權(quán)的伙伴,
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn