2016-08-03 14 views
1

Guten Tag alle. Ich erstelle eine Xamarin.Forms Portable-Anwendung in meinem Visual Studio 2015. Ich möchte, dass meine Mobile Application eine Verbindung zur SQL-Datenbank herstellt, die ich in meinem VS2015 habe.Xamarin.Forms Verbinden mit DB mit Web-Service

Also habe ich zwei Projekte in meiner Lösung erstellt. Eine ist für meine mobile Anwendung, die Xamarin Platform verwendet, und die andere für die Handhabung der Webdienste und der Datenbank. Damit habe ich ASP.NET WEB API verwendet.

Ich möchte jede Quelle der Hilfe, ob ich auf dem richtigen Weg bin, diese Sache zu tun.

Lassen Sie mich Ihnen die Codes zeigen, die ich habe. Dies sollte die Liste aller Kunden/Kunden anzeigen.

1.) XamarinForms (Portable)

ClientList.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="XamarinFormsDemo.Views.ClientListPage" 
     xmlns:ViewModels="clr-namespace:XamarinFormsDemo.ViewModels;assembly=XamarinFormsDemo" 
     xmlns:controls="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin.Abstractions" 
     BackgroundImage="bg3.jpg" 
     Title="Client List"> 


    <ContentPage.BindingContext> 
    <ViewModels:CustomerVM/> 
    </ContentPage.BindingContext> 


<SearchBar Placeholder="Search" Text="{Binding Keyword}" SearchCommand="{Binding SearchCommand}" x:Name="txtSearch" /> 

<ListView ItemsSource="{Binding PlotModel, Mode=TwoWay}" 
      HasUnevenRows="True" 
      IsPullToRefreshEnabled="True" 
      x:Name="listView"> 


    <ListView.ItemTemplate> 
    <DataTemplate> 
     <ViewCell> 
     <Grid Padding="10" RowSpacing="10" ColumnSpacing="5"> 
      <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 

      <controls:CircleImage Source="icon.png" 
       HeightRequest="66" 
       HorizontalOptions="CenterAndExpand" 
       Aspect="AspectFill" 
       WidthRequest="66" 
       Grid.RowSpan="2" 
       /> 


      <Label Grid.Column="1" 
       Text="{Binding CUSTOMER_NAME}" 
       TextColor="#24e97d" 
       FontSize="24"/> 



      <Label Grid.Column="1" 
        Grid.Row="1" 
        Text="{Binding CUSTOMER_CODE}" 
        TextColor="White" 
        FontSize="18" 
        Opacity="0.6"/> 


      <Label Grid.Column="1" 
       Grid.Row="2" 
       Text="{Binding CUSTOMER_CONTACT}" 
       TextColor="White" 
       FontSize="18" 
       Opacity="0.6"/> 



     </Grid> 
     </ViewCell> 
    </DataTemplate> 
    </ListView.ItemTemplate> 

</ListView> 


<StackLayout Orientation="Vertical" 
     Padding="30,10,30,10" 
     HeightRequest="20" 
     BackgroundColor="#24e97d" 
     VerticalOptions="Center" 
     Opacity="0.5"> 
    <Label Text="© Copyright 2016 SMESOFT.COM.PH All Rights Reserved " 
     HorizontalTextAlignment="Center" 
     VerticalOptions="Center" 
     HorizontalOptions="Center" /> 
    </StackLayout> 




</ContentPage> 

customer.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace XamarinFormsDemo.Models 
{ 
    public class Customer 
    { 

public int Id { get; set; } 
public string CUSTOMER_CODE { get; set; } 
public string CUSTOMER_NAME { get; set; } 
public string CUSTOMER_EMAIL_ADDRESS { get; set; } 
public string CUSTOMER_MOBILE_NUMBER { get; set; } 
public string CUSTOMER_CONTACT { get; set; } 
public string CUSTOMER_LANDLINE { get; set; } 
public string CUSTOMER_FAX_NUMBER { get; set; } 
public string ADDRESS { get; set; } 
public int LOCATION_TYPE_ID { get; set; } 
public int INDUSTRY_TYPE_ID { get; set; } 
public int CUSTOMER_TYPE_ID { get; set; } 
public int IS_DELETED { get; set; } 
public DateTime DATE_CREATED { get; set; } 
} 
} 

CustomerVM.cs

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.Linq; 
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 ICommand SearchCommand 
    { 
     get 
     { 
      return new Command((sender) => 
      { 
       //var searchBar = (SearchBar)sender; 
       //this.Keyword = searchBar.Text; 
       Filter(); 
      }); 
     } 
    } 



    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)); 
     } 

    } 
} 

CustomerServices.cs

using Plugin.RestClient; 
using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
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; 
    } 

    } 
} 

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(); 

     var json = await httpClient.GetStringAsync(WebServiceUrl); 

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

     return taskModels; 
    } 
} 

.

2.) WebFormsDemo

CustomerViewModel.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace WebFormsDemo.ViewModel 
{ 
    public class CustomerViewModel 
    { 

    public int Id { get; set; } 
    public string CUSTOMER_CODE { get; set; } 
    public string CUSTOMER_NAME { get; set; } 
    public string CUSTOMER_EMAIL_ADDRESS { get; set; } 
    public string CUSTOMER_MOBILE_NUMBER { get; set; } 
    public string CUSTOMER_CONTACT { get; set; } 
    public string CUSTOMER_LANDLINE { get; set; } 
    public string CUSTOMER_FAX_NUMBER { get; set; } 
    public string ADDRESS { get; set; } 
    public int LOCATION_TYPE_ID { get; set; } 
    public int INDUSTRY_TYPE_ID { get; set; } 
    public int CUSTOMER_TYPE_ID { get; set; } 
    public int IS_DELETED { get; set; } 
    public DateTime DATE_CREATED { get; set; } 
    } 
} 

CustomerController.cs

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.Entity; 
using System.Data.Entity.Infrastructure; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
using System.Web.Http.Description; 
using WebFormsDemo; 
using WebFormsDemo.ViewModel; 

namespace WebFormsDemo.Controllers 
{ 
    public class CustomerController : ApiController 
    { 
    private EBMSEntities db = new EBMSEntities(); 

    // GET: api/Customer 
    public IQueryable<CustomerViewModel> GetCustomerViewModels() 
    { 
     var displaycustomerInfo = from cust in db.CUSTOMERs 
            select new CustomerViewModel 
            { 
             Id = cust.CUSTOMER_ID, 
             CUSTOMER_CODE = cust.CUSTOMER_CODE, 
             CUSTOMER_NAME = cust.CUSTOMER_NAME, 
             CUSTOMER_MOBILE_NUMBER = cust.CUSTOMER_MOBILE_NUMBER, 
             CUSTOMER_EMAIL_ADDRESS = cust.CUSTOMER_EMAIL_ADDRESS, 
             CUSTOMER_CONTACT = cust.CUSTOMER_EMAIL_ADDRESS + "," + " " + cust.CUSTOMER_MOBILE_NUMBER 
            }; 




     return displaycustomerInfo; 
    }   
    } 
} 

.

Wie Sie sehen können, greift mein RestClient nur auf den Inhalt meiner CustomerController.cs in meinem WebFormsDemo unter Verwendung der WebService URL zu.

Nachdem dies alles gesagt wurde, konnte ich die beiden Projekte weder verbinden noch anzeigen. Mache ich das Richtige? Bitte hilf mir. Danke vielmals.

Antwort

1

der Grund, weil es keine Daten, die Ihre Dienste nicht bekommen, verwenden Sie diese stattdessen

public interface ICustomer 
    { 
     Task<string> GetCustomers(); 
    } 

    public class ICustomerService : 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; 
    } 

    } 
+0

Wo soll ich diesen Codes Sir setzen? –

+0

Erstellen Sie einfach eine Datei in Ihren Diensten XamarinFormsDemo.Services –

+0

Alles klar Sir. Wie wäre es damit? var _custList = neu GetCustomers(); var returnJson = erwarten _custList.GetCustomers(); –

Verwandte Themen