如何将Xamarin.Forms移动应用程序正确连接到Web服务?
美好的一天。我只是一个新手在使用Xamarin.Forms,所以人们请帮助我。我创建了基本的移动应用程序。我还创建了一个ASP.NET WEB应用程序,使我能够对员工姓名和部门进行CRUD记录。我想要做的是获得我在网站上创建的所有记录并将其显示给我的移动应用程序。但是我在将XAMARIN.FORMS连接到WEB SERVICE时遇到了问题。我能够将记录保存在WEB应用程序中,但是当我尝试将它连接到我的移动应用程序时,它不起作用。这是一个跨平台,但我首先关注Android版本,所以现在请忽略iOS和Windows。请帮帮我。如果我在这样做的过程中走错了路,或者如果您有其他想法,请让我知道。 我希望我的代码能帮上忙。非常感谢。如何将Xamarin.Forms移动应用程序正确连接到Web服务?
MainPageMain.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinDemo.Views.MainPageMain"
xmlns:ViewModels="clr-namespace:XamarinDemo.ViewModels;assembly=XamarinDemo"
BackgroundColor="Teal">
<ContentPage.BindingContext>
<ViewModels:MainViewModel/>
</ContentPage.BindingContext>
<ListView ItemsSource="{Binding EmployeesList}"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical"
Padding="12,6">
<Label Text="{Binding Name}"
FontSize="24"/>
<Label Text="{Binding Department}"
FontSize="18"
Opacity="0.6"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
MainViewModel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using XamarinFormsDemo.Models;
using XamarinFormsDemo.Services;
namespace XamarinFormsDemo.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
private List<Employee> _employeesList;
public List<Employee> EmployeesList
{
get { return _employeesList; }
set
{
_employeesList = value;
OnPropertyChanged();
}
}
public MainViewModel()
{
InitializeDataAsync();
}
private async Task InitializeDataAsync()
{
var employeesServices = new EmployeesServices();
EmployeesList = await employeesServices.GetEmployeesAsync();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
EmployeesServices.cs
using Plugin.RestClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XamarinFormsDemo.Models;
namespace XamarinFormsDemo.Services
{
public class EmployeesServices
{
public async Task<List<Employee>> GetEmployeesAsync()
{
RestClient<Employee> restClient = new RestClient<Employee>();
var employeesList = await restClient.GetAsync();
return employeesList;
}
}
}
我觉得你GetEmployeesAsync()
函数返回一个ObservableCollection<Employee>
而不是List<Employee>
,我觉得你的观点将只是更新当ListView的绑定设置为ObservableCollection时。
如果这不是问题,则可以在调试器中检查您的EmployeesList是否填充了数据。
编辑: 在你MainViewModel,改变private List<Employee> _employeesList;
到private ObservableCollection<Employee> _employeesList;
和public List<Employee> EmployeesList
到public ObservableCollection<Employee> EmployeesList
在你InitializeDataAsync()方法改变
EmployeesList = await employeesServices.GetEmployeesAsync();
到
var templist = await employeesServices.GetEmployeesAsync();
foreach (var e in templist)
{
EmployeesList.Add(e);
}
而且我m确信如果你可以在xamarin中设置你的绑定上下文,你可以尝试在页面的CodeBehind中做到这一点。
您需要执行一些基本的调试以确定问题出在哪里。你有没有尝试过?您似乎重复发布完全相同的问题,但没有任何新的信息可以帮助我们。例如,在您的GetEmployeeAsync()中添加一个简单的Debug.Writeline(employeeList.Count())会告诉您您的服务是否正在向客户端返回数据。 – Jason
你可以请教我如何做你说的先生调试的东西? –
你允许在Android的Manifest文件中允许访问互联网吗?示例在这里,https://developer.xamarin.com/recipes/android/general/projects/add_permissions_to_android_manifest/ – Bearcat9425