C# 表单:在Android中不显示ListView
你好。我正在创建一个简单的Xamarin.Forms(便携)应用程序,它允许我创建员工的记录并将其保存在VisualStudio的数据库中。所有记录都显示在列表视图中 我的应用程序在UWP上运行时运行良好。它会在ListView上正确显示所有创建的记录 但当我在Android平台上运行它时,它不会在ListView中显示任何记录。我甚至尝试在Web API上检查它是否返回值。我确实得到了一个值。也就是说,问题在于Android平台为什么不显示任何记录 你遇到过这个问题吗?你认为这背后的原因是什么?我现在能做什么?对不起,我只是Xamarin的新手。希望你能帮助我。非常感谢 以下是我的一些代码: EmployeeRecordsPage.xamlC# 表单:在Android中不显示ListView,c#,xaml,xamarin,xamarin.forms,uwp-xaml,C#,Xaml,Xamarin,Xamarin.forms,Uwp Xaml,你好。我正在创建一个简单的Xamarin.Forms(便携)应用程序,它允许我创建员工的记录并将其保存在VisualStudio的数据库中。所有记录都显示在列表视图中 我的应用程序在UWP上运行时运行良好。它会在ListView上正确显示所有创建的记录 但当我在Android平台上运行它时,它不会在ListView中显示任何记录。我甚至尝试在Web API上检查它是否返回值。我确实得到了一个值。也就是说,问题在于Android平台为什么不显示任何记录 你遇到过这个问题吗?你认为这背后的原因是什么
<?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.EmployeeRecordsPage"
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="List of Employees">
<ContentPage.BindingContext>
<ViewModels:MainViewModel/>
</ContentPage.BindingContext>
<StackLayout Orientation="Vertical">
<ListView ItemsSource="{Binding EmployeesList}"
HasUnevenRows="True"
IsPullToRefreshEnabled="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 EMPLOYEE_NAME}"
TextColor="#24e97d"
FontSize="24"/>
<Label Grid.Column="1"
Grid.Row="1"
Text="{Binding EMP_NUMBER}"
TextColor="White"
FontSize="18"
Opacity="0.6"/>
<Label Grid.Column="1"
Grid.Row="2"
Text="{Binding DEPT_COMP}"
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>
EmployeeViewModel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using XamarinFormsDemo.Models;
using XamarinFormsDemo.Services;
namespace XamarinFormsDemo.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
private List<Employee> _employeesList;
private Employee _selectedEmployee = new Employee();
public List<Employee> EmployeesList
{
get { return _employeesList; }
set
{
_employeesList = value;
OnPropertyChanged();
}
}
public MainViewModel()
{
InitializeDataAsync();
}
private async Task InitializeDataAsync()
{
var employeesServices = new EmployeesServices();
EmployeesList = await employeesServices.GetEmployeesAsync();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
using Plugin.RestClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XamarinFormsDemo.Models;
namespace XamarinFormsDemo.Services
{
public class EmployeesServices
{
public async Task<List<Employee>> GetEmployeesAsync()
{
RestClient<Employee> restClient = new RestClient<Employee>();
var employeesList = await restClient.GetAsync();
return employeesList;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用System.Runtime.CompilerServices;
使用系统文本;
使用System.Threading.Tasks;
使用Xamarin.Forms;
使用XamarinFormsDemo.Models;
使用XamarinFormsDemo.Services;
命名空间XamarinFormsDemo.ViewModels
{
公共类MainViewModel:INotifyPropertyChanged
{
私人名单——员工名单;
私人员工_selectedEmployee=新员工();
公开名单雇员名单
{
获取{return\u employeesList;}
设置
{
_employeesList=价值;
OnPropertyChanged();
}
}
公共主视图模型()
{
InitializeDataAsync();
}
专用异步任务InitializeDataAsync()
{
var employeesServices=新的employeesServices();
EmployeesList=等待employeesServices.GetEmployeesAsync();
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
var handler=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
EmployeesServices.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using XamarinFormsDemo.Models;
using XamarinFormsDemo.Services;
namespace XamarinFormsDemo.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
private List<Employee> _employeesList;
private Employee _selectedEmployee = new Employee();
public List<Employee> EmployeesList
{
get { return _employeesList; }
set
{
_employeesList = value;
OnPropertyChanged();
}
}
public MainViewModel()
{
InitializeDataAsync();
}
private async Task InitializeDataAsync()
{
var employeesServices = new EmployeesServices();
EmployeesList = await employeesServices.GetEmployeesAsync();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
using Plugin.RestClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XamarinFormsDemo.Models;
namespace XamarinFormsDemo.Services
{
public class EmployeesServices
{
public async Task<List<Employee>> GetEmployeesAsync()
{
RestClient<Employee> restClient = new RestClient<Employee>();
var employeesList = await restClient.GetAsync();
return employeesList;
}
}
}
使用Plugin.RestClient;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用XamarinFormsDemo.Models;
命名空间XamarinFormsDemo.Services
{
公共类雇员服务
{
公共异步任务GetEmployeesAsync()
{
RestClient RestClient=new RestClient();
var employeesList=await restClient.GetAsync();
返回员工名单;
}
}
}
我认为问题在于,在您使用以下内容获取项目后,您没有更新ListView:
EmployeesList = await employeesServices.GetEmployeesAsync();
我建议使用ObservableCollection而不是List。对于可观察集合,当添加或删除可观察集合中的项时,ListView应自动更新。因此,不是:
private List<Employee> _employeesList;
要执行上述操作,您必须为ListView指定一个名称,以便在代码中引用它。您可以在XAML中为ListView设置此名称:
<ListView
ItemsSource="{Binding EmployeesList}"
HasUnevenRows="True"
IsPullToRefreshEnabled="True"
x:Name="listView"
>
但可观察的收集将是更好的选择 我认为问题在于,在您使用以下内容获取项目后,您没有更新ListView:
EmployeesList = await employeesServices.GetEmployeesAsync();
我建议使用ObservableCollection而不是List。对于可观察集合,当添加或删除可观察集合中的项时,ListView应自动更新。因此,不是:
private List<Employee> _employeesList;
要执行上述操作,您必须为ListView指定一个名称,以便在代码中引用它。您可以在XAML中为ListView设置此名称:
<ListView
ItemsSource="{Binding EmployeesList}"
HasUnevenRows="True"
IsPullToRefreshEnabled="True"
x:Name="listView"
>
但可观察的收集将是更好的选择 你能展示一下android的主要活动吗?您是否尝试在Xamarin表单页面中设置断点并检查是否被击中?@user3185569我没有尝试设置断点,先生。我还不知道怎么做。这是我的主要活动代码。问题不是显示代码时向我跳出来。你能提供一个运行的示例项目吗?@therealjohn好的,先生。我可以给你发封电子邮件吗?如果可以的话,我会直接发送给你。@user3185569先生,你检查过我的代码了吗?你能给你看一下android的主要活动吗?您是否尝试在Xamarin表单页面中设置断点并检查是否被击中?@user3185569我没有尝试设置断点,先生。我还不知道怎么做。这是我的主要活动代码。问题并不是在我面前突然出现代码。你能提供一个运行的示例项目吗?@therealjohn好的,先生。我可以给你发电子邮件吗?如果可以的话,我会直接发送给你。@user3185569你检查过我的代码了吗,先生?先生,为什么即使我在这里声明了listView,我也不能在我的ViewModel中访问它:x:Name=“listView”它将在你的代码隐藏文件中用于Xaml页面,而不是视图模型。我不是MVVM专家,但据我所知,理想情况下,ViewModel不需要了解有关视图的任何信息,因为视图ViewModel可能用于多个视图。因此,您可能希望在加载EmployeesList时让ViewModel引发一个事件,并且您的view代码隐藏可以订阅该事件,以便它可以重新加载listView的ItemsSource。是的,先生,它在我的代码隐藏中可用。因此,您在这里所做的是不正确的:EmployeesList=Wait employeesServices.GetEmployeesAsync();listView.ItemsSource=null;listView.ItemsSource=EmployeesList;抱歉,我忽略了您的视图模型中的代码。然而,我的回答基本上是正确的,除了把代码放在哪里刷新listView,所以请接受它