簡體   English   中英

Xamarin.Forms:Xamarin.Droid上未顯示ListView

[英]Xamarin.Forms: ListView are not being Displayed on Xamarin.Droid

我正在創建一個Xamarin.Forms可移植應用程序 我的Visual Studio中有一個數據庫,我想將其中的數據顯示到Xamarin ListView。 但是無論何時,數據都不會顯示在Xamarin.Droid上,僅留下空白。 我在UWP中進行了嘗試,並且有效。 如何在Xamarin.Droid中做到這一點?

(我的Xamarin.Droid的屏幕截圖)

在此處輸入圖片說明

請注意,即使沒有顯示所有記錄,ListView仍會占用空間。 您認為這背后的原因是什么? 我什至在我的WEB API中檢查是否正在檢索數據並且確實如此。

意思是,真正的問題僅出現在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;
    }
 }

在您的ViewModel中更改

public List<Customer> CustomerList

public ObservableCollection<Customer> CustomerList

然后在您的xaml中,更改此

 <ListView ItemsSource="{Binding CustomerList}"

對此

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM