如题,这是从Web API访问数据并在移动应用程序中显示数据的最常见要求。要构建使用ASP.NET Core Web API或休息服务的Xamarin应用程序,我们需要HttpClient发送HTTP请求并从URI标识的Web服务接收HTTP响应。
步骤1:创建ASP.NET Core Web API服务或休息服务。
请阅读文章1、文章2参考创建ASP.NET Core Web API服务并将其托管以供公共访问。
步骤2:创建一个帮助程序类以使用API服务并返回数据。
创建一个帮助器类,并使用异步方法RefreshDataAsync将其命名为WebAPIService,并使用API服务URI。
WebAPIUrl =“https://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 =“https://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:x="http://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正版授权的伙伴,请点这里。

标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!