Xamarin将数据绑定到ListView中的网格组件
我尝试绑定数据,但是列表视图没有显示任何内容,我调试了它,并且我看到项目正确,但是列表视图没有显示任何内容。如何将列表绑定到列表视图我搜索了很多次,但没有找到任何解决方案 ResultPage.xaml.csXamarin将数据绑定到ListView中的网格组件,listview,xamarin,xamarin.forms,grid,Listview,Xamarin,Xamarin.forms,Grid,我尝试绑定数据,但是列表视图没有显示任何内容,我调试了它,并且我看到项目正确,但是列表视图没有显示任何内容。如何将列表绑定到列表视图我搜索了很多次,但没有找到任何解决方案 ResultPage.xaml.cs using System; using System.Collections.Generic; using FindDiscount.Models; using Xamarin.Forms; using System.Collections.ObjectModel; namespace
using System;
using System.Collections.Generic;
using FindDiscount.Models;
using Xamarin.Forms;
using System.Collections.ObjectModel;
namespace FindDiscount {
public partial class ResultPage : ContentPage {
public ObservableCollection<resultModel> products;
public ResultPage(int distance, string productName) {
InitializeComponent();
initData();
}
public ResultPage() {
InitializeComponent();
}
void initData(){
products = new ObservableCollection<resultModel>();
products.Add(new resultModel{
productName="Samsung Note 4",
companyName="Samsung",
productPrice=1000,
discountRate=15,
imageURL="hamburger.png"
});
products.Add(new resultModel {
productName = "iphone 7",
companyName = "Apple",
productPrice = 3000,
discountRate = 45,
imageURL = "hamburger.png"
});
products.Add(new resultModel {
productName = "Windows Phone ",
companyName = "Microsoft",
productPrice = 1000,
discountRate = 15,
imageURL = "hamburger.png"
});
ProductsListView.ItemsSource = products;
}
}
}
使用系统;
使用System.Collections.Generic;
使用FindDiscont.Models;
使用Xamarin.Forms;
使用System.Collections.ObjectModel;
名称空间查找{
公共部分类结果页:ContentPage{
公众可观察到的收集产品;
公共结果页(整数距离,字符串名称){
初始化组件();
initData();
}
公共结果页(){
初始化组件();
}
void initData(){
产品=新的可观察收集();
products.Add(新结果模型{
productName=“三星Note 4”,
companyName=“三星”,
产品价格=1000,
贴现率=15,
imageURL=“hamburger.png”
});
products.Add(新结果模型{
productName=“iphone 7”,
companyName=“苹果”,
产品价格=3000,
贴现率=45,
imageURL=“hamburger.png”
});
products.Add(新结果模型{
productName=“Windows Phone”,
companyName=“Microsoft”,
产品价格=1000,
贴现率=15,
imageURL=“hamburger.png”
});
ProductsListView.ItemsSource=产品;
}
}
}
ResultPage.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="FindDiscount.ResultPage">
<ContentPage.Content>
<ListView x:Name="ProductsListView" HasUnevenRows="true" ItemsSource="{Binding products}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.Column="0" Source="{Binding imageURL}" />
<Label Grid.Row="0" Grid.Column="1" Text="{Binding productName}" TextColor="Black" />
<Label Grid.Row="1" Grid.Column="1" Text="{Binding companyName}" TextColor="Black" />
<Label Grid.Row="2" Grid.Column="0" Text="{Binding productPrice}" TextColor="Black" />
<Label Grid.Row="2" Grid.Column="1" Text="{Binding discountRate}" TextColor="Black" />
<Button Grid.Row="3" Grid.Column="1" Text="Locaiton" TextColor="Black" />
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
我认为你应该写一些
public ObservableCollection<resultModel> products {get; set;}
publicobservableCollection产品{get;set;}
并在您的
resultModel
中实现INotifyPropertyChanged
,我已经用相同的代码使用MVVM解决了我的问题