[英]Empty listview elements binding Xamarin Forms not showing binding name
My listview was displaying the data, after messing with some things it stopped displaying, I saw that this is a very common problem in Xamarin Forms, but when I create other XAML it doesn't display either.我的列表视图正在显示数据,在弄乱了它停止显示的一些东西之后,我发现这是 Xamarin Forms 中一个非常常见的问题,但是当我创建其他 XAML 时它也不显示。
XAML markup: 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="ListPageExample.ProductListPage"
Shell.PresentationMode="ModalAnimated"
Title="Product List Page">
<ListView x:Name="ProductListView"
CachingStrategy="RecycleElement"
ItemTapped="ProductListView_ItemTapped"
ItemsSource="{Binding Products}"
HasUnevenRows="True"
SeparatorColor="Blue"
SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10">
<Frame CornerRadius="10" HasShadow="True">
<StackLayout Orientation="Horizontal">
<Image Source="{Binding ImageUrl}" WidthRequest="100" HeightRequest="100"></Image>
<StackLayout VerticalOptions="Center">
<Label VerticalOptions="Center" Text="{Binding Name}" FontSize="Large"></Label>
<Label VerticalOptions="Center" Text="{Binding Price}" FontSize="Large"></Label>
</StackLayout>
</StackLayout>
</Frame>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
C# code: C#代码:
using ListPageExample;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace ListPageExample
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ProductListPage : ContentPage
{
public ObservableCollection<Product Products { get; set; }
public ProductListPage()
{
InitializeComponent();
BindingContext = this;
base.OnAppearing();
Products = new ObservableCollection<Product();
{
Products.Add(new Product() { Id = 1, Name = "Sprite", Price = 20, ImageUrl= "bafomete.jpg"});
Products.Add(new Product() { Id = 2, Name = "Coca Cola", Price = 20, ImageUrl = "bafometa.jpg" });
Products.Add(new Product() { Id = 3, Name = "Fanta", Price = 20, ImageUrl = "bafomete.jpg" });
Products.Add(new Product() { Id = 4, Name = "Fanta Uva", Price = 20, ImageUrl = "bafometa.jpg" });
}
ProductListView.ItemsSource = Products;
}
private void ProductListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
if(e.Item==null)
{
return;
}
((ListView)sender).SelectedItem = null;
Navigation.PushModalAsync(new NewItemPage(e.Item as Product));
}
}
}
This item does not appear in the list view, only the blank lisview, can anyone help?该项目没有出现在列表视图中,只有空白的lisview,有人可以帮忙吗?
The code ItemsSource="{Binding Products}"
in ProductListPage.xaml
and the code ProductListView.ItemsSource = Products;
ProductListPage.xaml 中的代码
ItemsSource="{Binding Products}"
和代码ProductListPage.xaml
ProductListView.ItemsSource = Products;
in ProductListPage.xaml.cs
have the same function, you can delete the code ItemsSource="{Binding Products}"
.在
ProductListPage.xaml.cs
中具有相同的功能,可以删除代码ItemsSource="{Binding Products}"
。
The code ProductListView.ItemsSource = Products;
代码
ProductListView.ItemsSource = Products;
in ProductListPage.xaml.cs
can bind the data of Products to the ListView. ProductListPage.xaml.cs
中可以将Products的数据绑定到ListView。
I made a demo based on your code and it works well on my side.我根据你的代码做了一个演示,它在我这边运行良好。 Here is the code in
MainPage.xaml.cs
:这是
MainPage.xaml.cs
中的代码:
public partial class MainPage : ContentPage
{
public MainPage()
{
ObservableCollection<Product> Products = new ObservableCollection<Product>();
InitializeComponent();
BindingContext = this;
base.OnAppearing();
{
Products.Add(new Product() { Id = 1, Name = "Sprite", Price = 20, });
Products.Add(new Product() { Id = 2, Name = "Coca Cola", Price = 20, });
Products.Add(new Product() { Id = 3, Name = "Fanta", Price = 20, });
Products.Add(new Product() { Id = 4, Name = "Fanta Uva", Price = 20, });
}
ProductListView.ItemsSource = Products;
}
private void ProductListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
if (e.Item == null)
{
return;
}
((ListView)sender).SelectedItem = null;
Navigation.PushModalAsync(new NewItemPage(e.Item as Product));
}
}
Here is the code in MainPage.xaml:这是 MainPage.xaml 中的代码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App3.MainPage">
<ListView x:Name="ProductListView"
CachingStrategy="RecycleElement"
ItemTapped="ProductListView_ItemTapped"
HasUnevenRows="True"
SeparatorColor="Blue"
SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10">
<Frame CornerRadius="10" HasShadow="True">
<StackLayout Orientation="Horizontal">
<StackLayout VerticalOptions="Center">
<Label VerticalOptions="Center" Text="{Binding Name}" FontSize="Large"></Label>
<Label VerticalOptions="Center" Text="{Binding Price}" FontSize="Large"></Label>
</StackLayout>
</StackLayout>
</Frame>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage
This is my entity class:这是我的实体类:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
}
Here is the sample:这是示例:
You are using您正在使用
ItemsSource="{Binding Products}"
in xaml.在xml中。 And also
并且
ProductListView.ItemsSource = Products;
in code behind.在后面的代码中。 That is first thing that could cause a trouble.
这是可能引起麻烦的第一件事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.