未在listview中显示的图像
我是Android新手,在获取示例时遇到了一些问题,无法像我参考的书中那样做 问题的主要要点是我正在构建一个应用程序,该应用程序应该在ListView中显示图像和一些文本。显示文本,但不显示图像。我最初只是将文件名放在ListItem类源属性的引号中 我正在尝试引用可绘制的资源目录,但没有成功访问它 这是密码未在listview中显示的图像,listview,xamarin,xamarin.forms,visual-studio-2017,Listview,Xamarin,Xamarin.forms,Visual Studio 2017,我是Android新手,在获取示例时遇到了一些问题,无法像我参考的书中那样做 问题的主要要点是我正在构建一个应用程序,该应用程序应该在ListView中显示图像和一些文本。显示文本,但不显示图像。我最初只是将文件名放在ListItem类源属性的引号中 我正在尝试引用可绘制的资源目录,但没有成功访问它 这是密码 using System; using System.Collections.Generic; using System.Text; using Andr
using System;
using System.Collections.Generic;
using System.Text;
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
namespace ListViewExample
{
public class ListItem
{
public string Source { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Price { get; set; }
}
public class ListViewCustom : ContentPage
{
public ListViewCustom()
{
ListView listView = new ListView();
listView.ItemsSource = new ListItem[]
{new ListItem{Source=Resource.first.png,Title="First",Description="1st item", Price="$100.00" },
new ListItem{Source=Resource.second.png,Title="Second",Description="2nd item", Price="$200.00" },
new ListItem{Source=Resource.third.png,Title="Third",Description="3rd item", Price="$300.00" }
};
listView.ItemTemplate = new DataTemplate(typeof(ImageCell));
listView.ItemTemplate.SetBinding (ImageCell.ImageSourceProperty, "Source");
listView.ItemTemplate.SetBinding(ImageCell.TextProperty, "Title");
listView.ItemTemplate.SetBinding(ImageCell.DetailProperty, "Description");
listView.RowHeight = 80;
listView.BackgroundColor = Color.Black;
listView.ItemTemplate = new DataTemplate(typeof(ListItemCell));
Content = listView;
listView.ItemTapped += async (sender, e) =>
{
ListItem item = (ListItem)e.Item;
await DisplayAlert("Tapped", item.Title.ToString(), " was selected.", "OK");
((ListView)sender).SelectedItem = null;
};
}
}
public class ListItemCell : ViewCell
{
public ListItemCell()
{
Image image = new Image()
{
HorizontalOptions = LayoutOptions.FillAndExpand
};
StackLayout imageLayout = new StackLayout()
{
HorizontalOptions = LayoutOptions.FillAndExpand,
Children =
{image}
};
Label titleLabel = new Label()
{
HorizontalOptions = LayoutOptions.FillAndExpand,
FontSize = 25,
WidthRequest = 100,
FontAttributes = Xamarin.Forms.FontAttributes.Bold,
TextColor = Color.White
};
titleLabel.SetBinding(Label.TextProperty, "Title");
Label descLabel = new Label()
{
HorizontalOptions = LayoutOptions.FillAndExpand,
FontSize = 12,
WidthRequest = 100,
TextColor = Color.White
};
descLabel.SetBinding(Label.TextProperty, "Description");
StackLayout viewLayoutItem = new StackLayout()
{
HorizontalOptions = LayoutOptions.StartAndExpand,
Orientation = StackOrientation.Vertical,
Padding = new Thickness(10, 0, 50, 10),
Children =
{
imageLayout,
titleLabel,
descLabel
}
};
Label priceLabel = new Label()
{
HorizontalOptions = LayoutOptions.FillAndExpand,
FontSize = 25,
FontAttributes = Xamarin.Forms.FontAttributes.Bold,
TextColor = Color.Aqua
};
priceLabel.SetBinding(Label.TextProperty, "Price");
StackLayout viewLayout = new StackLayout()
{
HorizontalOptions = LayoutOptions.StartAndExpand,
Orientation = StackOrientation.Horizontal,
Padding = new Thickness(25, 10, 55, 15),
Children =
{
viewLayoutItem,
priceLabel
}
};
View = viewLayout;
}
}
}
在此方面的任何帮助都将不胜感激。这可能有点愚蠢,但就像我说的,我是Android新手
*编辑(将此添加为OP的编辑,该OP在hvaughan3答案下的评论后将其作为答案发布):
好吧,我不能在这里显示代码,因为它太长了,无法添加注释。基本上,错误是MainActivity.cs中的以下任一to语句
global::Xamarin.Forms.Forms.Init (this, bundle);
LoadApplication (newListViewExample.App ())
我还没有对文件做任何修改,所以我有点困惑,为什么会出现错误。我甚至试着重新创建这个项目。同样的结果
PS:回答hvaughan3,图像位于Resources\drawable目录中。是的,我确实将listview.itemssource语句更改为只有双引号。您是否尝试添加源绑定?
我在这里可以看到很多问题。基于您遇到的问题,我首先强烈建议您通读(而不仅仅是阅读)本手册 在您发布的代码中,您将ListView.ItemTemplate设置了5次,该属性只能设置或绑定一次。将某个内容分配给它2次(
listView.ItemTemplate=…
),然后尝试将某个内容绑定到它3次(listView.SetBinding()
)。每次都会覆盖上一个
所以我的第一个建议是坚持最后一个作业:
listView.ItemTemplate = new DataTemplate(typeof(ListItemCell));
下一个问题是,您没有将任何内容绑定到列表项单元格
的图像
。您可能希望执行以下操作:
image.SetBinding(Image.SourceProperty, "Source");
最后,当您声明
ListView.ItemSource
时,您正在设置每个ListItem.Source
,但您没有将资源.first.png
括在双引号中。另外,这些图像是命名为Resource.first.png
还是实际命名为first.png
?如果它们实际上被命名为first.png
,那么您应该将ListItem.Source
改为first.png
。谢谢您的输入。但是,在进行了您建议的更改后,图像仍然没有显示。你提出的大部分建议都是我试图解决这个问题,就像我说我是新来的一样,所以我尝试了我能找到的任何建议。@user182162绝对理解这种感觉!我也这么做。因此,您只设置了一次ItemTemplate
,设置了图像的绑定,最后是否使用了first.png
或Resource.first.png
?另外,我的图像
绑定也有问题。请查看我编辑的代码。还有,图像在哪里?它们是否在本机iOS和Android项目中?如果是,每个项目的目录是什么?他们的构建操作设置为什么(突出显示VS中的图像并打开属性面板)?嗯,我想我发现了问题。我没有将ListItem源绑定到图像。但是,我无法测试修复程序,因为我在以下代码中遇到运行时错误。(请参阅下一条注释)好的,我不能在这里显示代码,因为它对于注释来说太长了。基本上,错误是MainActivity.cs的global::Xamarin.Forms.Forms.Init(这个bundle)中的以下语句之一:;LoadApplication(新建ListViewExample.App());'@user182162错误或异常是什么?通常意味着当它第一次尝试加载ListViewExample.App()
时发生异常;listView.BackgroundColor=Color.Black;listView.ItemTemplate=new DataTemplate(typeof)(ListItemCell Content=listView;我已经根据hvaughan3的建议删除了涉及ImageCell的代码您是否尝试添加缺少的绑定?我已经更新了我的答案谢谢您的帮助Vaughn
image.SetBinding(Image.SourceProperty, "Source");