Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将Xamarin.Forms连接到Web服务_C#_Asp.net_Asp.net Mvc 4_Xamarin_Xamarin.forms - Fatal编程技术网

C# 无法将Xamarin.Forms连接到Web服务

C# 无法将Xamarin.Forms连接到Web服务,c#,asp.net,asp.net-mvc-4,xamarin,xamarin.forms,C#,Asp.net,Asp.net Mvc 4,Xamarin,Xamarin.forms,你好。我在如何向Xamarin.Forms应用程序显示我在ASP.NETWebAPI中创建的所有记录方面遇到了问题。我尝试创建员工姓名和部门的预定义列表,结果成功了。但我想做的是在ASP.NET Web应用程序中创建一个记录,并使其显示在我的移动应用程序中。我们将非常感谢您的帮助。提前谢谢。 我看了一段关于这个问题的视频教程。如果需要,请参阅此链接。() 这是我的密码 MainPageMain.xaml <?xml version="1.0" encoding="utf-8" ?>

你好。我在如何向Xamarin.Forms应用程序显示我在ASP.NETWebAPI中创建的所有记录方面遇到了问题。我尝试创建员工姓名和部门的预定义列表,结果成功了。但我想做的是在ASP.NET 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));
    }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用System.Runtime.CompilerServices;
使用系统文本;
使用System.Threading.Tasks;
使用XamarinFormsDemo.Models;
使用XamarinFormsDemo.Services;
命名空间XamarinFormsDemo.ViewModels
{
公共类MainViewModel:INotifyPropertyChanged
{
私人名单——员工名单;
公开名单雇员名单
{
获取{return\u employeesList;}
设置
{
_employeesList=价值;
OnPropertyChanged();
}
}
公共主视图模型()
{
InitializeDataAsync();
}
专用异步任务InitializeDataAsync()
{
var employeesServices=新的employeesServices();
EmployeesList=等待employeesServices.GetEmployeesAsync();
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
var handler=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
雇员服务公司

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;

     }

    }
}
使用Plugin.RestClient;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用XamarinFormsDemo.Models;
命名空间XamarinFormsDemo.Services
{
公共类雇员服务
{
公共异步任务GetEmployeesAsync()
{
RestClient RestClient=new RestClient();
var employeesList=await restClient.GetAsync();
返回员工名单;
}
}
}

以下是一篇关于正确方法的有用帖子:


如果您的记录没有完全显示在移动应用程序上,您可以使用POSTMAN测试您的ASP web服务,并仔细检查它是否正常工作,以及是否成功返回了正确的数据,然后在视图模型上正确绑定列表或可观察集合,然后正确更新列表/可观察集合属性以显示最新记录

向我们展示您已经尝试过的内容,以及不起作用的内容。请看,这就是我试过的。请仔细阅读我的问题。您的Web服务请求是否成功完成并返回列表?@Jason是的,先生,我可以返回列表,但只能在浏览器中返回,而不能在我的移动应用程序中返回。好的,很好,您知道您的服务是有效的。你需要弄清楚应用程序中的GetEmployeeAsync是否有效。缩小失败点是调试的一个重要部分。@Jason yeah意识到在编写答案的最后,我已经编辑了答案。@Jayceevangelista我没有发现您的代码有任何错误,您是否尝试过一步一步地调试所有过程?EmployeesList是否包含web服务响应的所有员工?你有什么东西吗?它可能是不同的问题:绑定、GetEmployeesAsync方法、UI问题,或者如果您希望在数据库中插入新记录后列表会自动更新,那么这是错误的,您还必须更新UI,例如:@jayceevangelista除非您从web API更新列表,否则更改不会自动显示,完成此操作的一些重要链接:,@jayceevangelista您可以一步一步地进行,例如,列表视图是否已渲染?不要关注数据,只需在列表视图中显示实体模型数据如果显示了列表视图,那么您的问题可能是绑定或数据。如何使用实体模型数据创建列表视图的一个快速示例是:@jayceevangelista它可以是不同的东西,您的绑定上下文,您的模型和视图模型中的属性名称,再次检查您的模型中是否有名称和部门,并尝试从代码隐藏中设置绑定。