【问题标题】:Xamarin.Forms: ListView are not being Displayed on Xamarin.DroidXamarin.Forms:ListView 未在 Xamarin.Droid 上显示
【发布时间】:2016-07-28 18:14:57
【问题描述】:

我正在创建 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;
    }
 }

【问题讨论】:

  • 作为测试,尝试删除ListView周围的StackLayout
  • @GeraldVersluis 我已经尝试过了,先生。但仍然没有显示 ListView。

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


【解决方案1】:

在您的 ViewModel 更改中

public List<Customer> CustomerList

public ObservableCollection<Customer> CustomerList

在你的 xaml 中,改变这个

 <ListView ItemsSource="{Binding CustomerList}"

到这里

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

【讨论】:

  • 你能把项目放在github上,这样我就可以从我的机器上运行了吗?
  • 我不知道如何使用 GitHub 先生。我可以通过电子邮件发送给您吗?
  • 不知道这里是否允许发邮件,但是你可以上传到服务器上并在这里分享链接
  • 我已将其上传到 mediafiere 先生。这是链接:mediafire.com/?v3ynw6qno5qy2xm
  • 你好,我已经下载了你的解决方案,但是它非常大,我无法运行,因为缺少资源,如果你制作一个非常小的应用程序,只显示你的问题,不需要服务或任何东西,只是一个带有列表和按钮的页面,当你按下按钮时,列表会增加它的价值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 2018-01-10
  • 2011-06-25
  • 2012-06-27
  • 2014-03-22
  • 2014-10-02
相关资源
最近更新 更多