【发布时间】:2018-09-14 01:07:21
【问题描述】:
我的 ListView 没有显示任何数据的问题。我正在尝试在我的 ListView 中加载 ImageUrls(作为文本)。我的视图(在 .xaml 中)在 StackLayout 中包含以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:App.ViewModels"
xmlns:abstractions="clr-namespace:RoundedBoxView.Forms.Plugin.Abstractions;assembly=RoundedBoxView.Forms.Plugin.Abstractions"
xmlns:iconize="clr-namespace:FormsPlugin.Iconize;assembly=FormsPlugin.Iconize"
x:Class="App.Views.ProductDetailView" >
<ContenPage.Content>
<StackLayout Spacing ="0"
BackgroundColor="White">
<ListView ItemsSource="{Binding Images}"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout HorizontalOptions="Start" >
<Label x:Name="URRl"
Text="{Binding Url}"
TextColor="Navy"
FontSize="20"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
我的 ViewModel (.cs) 如下:
public class ProductDetailViewModel : INotifyPropertyChanged
{
//event
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<ImageList> images;
//The variables which are binded to the View.
public ObservableCollection<ImageList> Images
{
get
{
return images;
}
set
{
if(images != value)
{
images = value;
foreach (ImageList il in images)
Console.WriteLine(il.Url);
OnPropertyChanged();
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="T:App.ViewModels.ProductDetailViewModel"/> class.
/// Listens for a message with the id of the product
/// </summary>
public ProductDetailViewModel(IRestService<ProductDetailsPhoto, ProductList> api)
{
//Images is being set here
}
/// <summary>
/// Notify INotifyPropertyChanged with the propertyName
/// </summary>
/// <param name="propertyName">Property name of the changed Variabele</param>
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ImageList
{
public string Url;
public ImageList(string _url)
{
Url = _url;
}
}
ProductDetailView 背后的代码
public partial class ProductDetailView : ContentPage
{
public ProductDetailView(int productID)
{
InitializeComponent();
BindingContext = App.ProductDetailsVM;
MessagingCenter.Send(this, Constants.GET_PRODUCT_DETAILS, productID);
}
}
我已经测试了将数据加载到图像中的函数。数据已正确加载。
实际行为: 视图显示一个没有数据的空列表。
预期行为: 在 ListView 中将图像 URL 显示为文本的视图。
谁能告诉我我的数据绑定或在我看来我做错了什么?如果您需要其他代码或有其他问题,请询问。
【问题讨论】:
-
您是否将
ProductDetailView.BindingContext设置为ProductDetailViewModel的实例?你是怎么做到的? -
在 ProductDetailView.xaml.cs 我有以下绑定上下文代码:
public partial class ProductDetailView : ContentPage { public ProductDetailView(int productID) { InitializeComponent(); BindingContext = App.ProductDetailsVM; MessagingCenter.Send(this, Constants.GET_PRODUCT_DETAILS, productID); } } -
我已将此信息添加到您的问题中。这是相关的。 @FraukeNonnenmacher 已经处理了这个问题 =)。
标签: c# xaml listview data-binding xamarin.forms