Xamarin.Forms:Xamarin.Droid

时间:2016-07-28 08:51:16

标签: c# asp.net xamarin xamarin.android xamarin.forms

我正在创建 Xamarin.Forms便携式应用程序。我的Visual Studio中有一个数据库,我想将其中的数据显示到Xamarin ListView中。但每当我这样做时,数据都没有显示在我的Xamarin.Droid上,只留下一个空白区域。我在UWP中尝试了它并且它有效。我将如何在我的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 个答案:

答案 0 :(得分:0)

在您的ViewModel更改

public List<Customer> CustomerList

public ObservableCollection<Customer> CustomerList

并在您的xaml中,更改此

 <ListView ItemsSource="{Binding CustomerList}"

到这个

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