【问题标题】:Xamarin.Forms Connecting to Web ServicesXamarin.Forms 连接到 Web 服务
【发布时间】:2016-08-03 08:16:14
【问题描述】:

大家好。我正在我的 Visual Studio 2015 中创建一个简单的 Xamarin.Forms 便携式应用程序。

我希望我的移动应用程序连接到我在 VS2015 中拥有的 SQL 数据库并返回一个应该显示在我的手机上的客户列表。

我创建了一个 Xamarin Portable 项目和一个 WebForms 项目来处理我的 Web 服务和数据库。

在我的 WebForms 项目中,我创建了一个控制器,它应该返回客户列表。这有一个 Web 服务 URL api/Customer 我用来连接到我的 Xamarin Portable 中的 RestClient。我还有 CustomerViewModel 应该代表我的应用程序中的数据。

在我的 Xamarin Portable 项目中,我有一个 ClientList.xaml,它应该显示来自我的数据库的列表。我还有一个连接到服务和我的 RestClient 的 CustomerVM。我的 RestClient 使用 WEB SERVICE URL 从我的 WebForms 项目中获取客户列表。

根据上面给出的步骤,我仍然无法在我的手机中显示数据。您认为这背后的原因是什么?感谢您的帮助。

这是我的一些代码:

RestClient.cs

  public class RestClient_Customer<T>
{


    private const string WebServiceUrl = "http://localhost:50857/api/Customer/";

    public async Task<List<T>> GetCustomerAsync()
    {

        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var json = await httpClient.GetStringAsync(WebServiceUrl);

        var taskModels = JsonConvert.DeserializeObject<List<T>>(json);

        return taskModels;
   }
 }

客户服务.cs

using Plugin.RestClient;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using XamarinFormsDemo.Models;

namespace XamarinFormsDemo.Services
{
    public class CustomerServices
    {

    public async Task<List<Customer>> GetCustomerAsync()
    {
        RestClient_Customer<Customer> restClient = new RestClient_Customer<Customer>();

        var customerList = await restClient.GetCustomerAsync(); //yung getasync ay pantawag as restclient

        return customerList;
        }
    }
}

CustomerVM.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using XamarinFormsDemo.Models;
using XamarinFormsDemo.Services;
using XamarinFormsDemo.Views;

namespace XamarinFormsDemo.ViewModels
{
    public class CustomerVM : INotifyPropertyChanged
    {


        private List<Customer> _customerList; // keep all customers
        private List<Customer> _searchedCustomerList; // keep a copy for searching
        private Customer _selectedCustomer = new Customer();

        private string _keyword = "";
        public string Keyword
        {
            get
            {
                return _keyword;
            }
            set
            {
                this._keyword = value;

                // while keyword changed we filter Employees
                //Filter();
            }
        }



        private void Filter()
        {
            if (string.IsNullOrWhiteSpace(_keyword))
            {
                CustomerList = _searchedCustomerList;

            }
            else
            {
                // var lowerKeyword = _keyword.ToLower();
                CustomerList = _searchedCustomerList.Where(r => r.CUSTOMER_NAME.ToLower().Contains(_keyword.ToLower())).ToList();
                //  EmployeesList = _searchedEmployeesList.Where(r => r.EMPLOYEE_NAME.Contains(_keyword)).ToList();


            }
        }


        public List<Customer> CustomerList
        {
            get
            {
                return _customerList;
            }
            set
            {
                _customerList = value;
                OnPropertyChanged();
            }
        }



        public CustomerVM()
        {
            InitializeDataAsync();

        }

        private async Task InitializeDataAsync()
        {
            var customerServices = new CustomerServices();
            _searchedCustomerList = await customerServices.GetCustomerAsync();
            CustomerList = await customerServices.GetCustomerAsync();

        }




        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}

【问题讨论】:

    标签: c# asp.net asp.net-web-api xamarin xamarin.forms


    【解决方案1】:

    我认为问题出在您的服务上,希望对您有所帮助,

          public interface ICustomer
          {
               Task<string> GetCustomers();
          }
    
          public class CustomerService : ICustomer
          {
    
            public async Task<string> GetCustomers()
            {
              var client = new HttpClient();
              var response = await client.GetAsync(string.Format("http://mysite/api/Customer"));
              var responseString = await response.Content.ReadAsStringAsync();
              return responseString; 
            }
    
           }
    

    在任何你喜欢的地方调用它

           var _custList = new GetCustomers();
    
           var returnJson = await _custList.GetCustomers(); 
    

    请注意,返回是 json 字符串格式或 xml 格式,具体取决于您的 REST API,因此您需要先对其进行解析,然后才能获取该值并将其显示到 ListView

    【讨论】:

    • 先生,我应该把这个放在哪里?在我的 ViewModel 中还是在我的服务中?
    • 基本上它仍在您的服务中,因为这是您的服务的一部分,将您的解析数据填充到您的模型中
    • 这是否意味着我不再需要 RestClient,因为这是我获得 webserviceurl 的地方?
    • 如果你这样做,那将是多余的。 :D 你也可以把它放到你的 RestClient 中,它不会违反任何规则
    • 先生,我应该把这个放在哪里? var _custList = new GetCustomers(); var returnJson = await _custList.GetCustomers();
    【解决方案2】:

    尝试在 UWP 中运行它。如果它在 UWP 中工作,那么你必须看看 Xamarin HttpClient.GetStringAsync not working on Xamarin.Droid

    我遇到了同样的问题,但是当我在 UWP 中尝试时,它运行良好。我仍在寻找使用设备运行 xamarin.android 的解决方案。

    【讨论】:

      猜你喜欢
      • 2016-09-28
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      相关资源
      最近更新 更多