2016-07-28 154 views
0

我正在創建一個Xamarin.Forms便攜式應用程序。我在我的Visual Studio中有一個數據庫,我想將其中的數據顯示到Xamarin ListView中。但是每當我這樣做時,數據就不會顯示在我的Xamarin.Droid上,只留下一個空白區域。我在UWP中嘗試過它,它工作。我將如何在我的Xamarin.Droid中做到這一點?Xamarin.Forms:ListView不會顯示在Xamarin.Droid

(我Xamarin.Droid截圖)

enter image description here

注意ListView控件仍然佔據即使不被顯示的所有記錄的空間。你認爲這背後的原因是什麼?我甚至在我的WEB API中檢查這個數據是否正在被檢索並且IT是否可用。

意思是,真正的問題只發生在顯示ListView上的記錄。希望您能夠幫助我。

這是我試過的代碼。

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> 
    <StackLayout Orientation="Vertical"> 

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

    <ListView ItemsSource="{Binding CustomerList}" 
      HasUnevenRows="True"> 
     <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> 
    </StackLayout> 

</ContentPage> 

ClientListViewModel.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; 

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


    } 
} 

CustomerService.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; 
    } 
} 
+1

嘗試,作爲一個測試,刪除'ListView'附近的'StackLayout' –

+0

@GeraldVersluis我已經試過了,先生。但仍然沒有顯示ListView。 –

回答

0

在您的視圖模型變化

public List<Customer> CustomerList 

public ObservableCollection<Customer> CustomerList 

,並在XAML中,更改此

<ListView ItemsSource="{Binding CustomerList}" 

這種

<ListView ItemsSource="{Binding CustomerList, Mode=TwoWay}" 
+0

先生沒有工作。什麼也沒有變。 –

+0

你可以把項目放在github上,這樣我就可以從我的機器上運行了嗎? – BraveHeart

+0

我不知道如何使用GitHub先生。我可以通過電子郵件發送給你嗎? –