[英]Xamarin Forms Image not showing in listview
I want to display a /!\\
icon in my listview. 我想在listview中显示
/!\\
图标。 Unfortunately, it's not showing up anywhere. 不幸的是,它没有出现在任何地方。
I am 100% positive it's in the right place in both the iOS and Android app. 我100%肯定它在iOS和Android应用中都处于正确的位置。 I have double checked that it is a Android resource (as per screenshot below)
我已经仔细检查过它是一个Android资源(如下面的截图所示)
The code used to initialize the image: alarmIcon = new Image { Source = "warning.png" };
用于初始化图像的代码:
alarmIcon = new Image { Source = "warning.png" };
After initialization I put it in an AlarmPageItem object which as an Image property. 初始化后,我将它放在一个AlarmPageItem对象中,该对象作为Image属性。 This gets added to the listview.
这会添加到列表视图中。 I have checked that I matched the bindings with the
AlarmPageItem
properties. 我已经检查过我将绑定与
AlarmPageItem
属性匹配。 (The text does get shown). (文本确实显示)。
I am at a loss why it wouldn't work... 我不知道为什么它不起作用......
Things I tried 我试过的事情
Code: 码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="OSIApp.Pages.AlarmPage"
Title="Alarm Log">
<ContentPage.Content>
<StackLayout>
<ListView x:Name="listView"
ItemTapped="OnItemTapped" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<StackLayout>
<Label x:Name="alarmTimeLabel"
HorizontalOptions="Center"
HorizontalTextAlignment="Center"
Text="{Binding DateTime}"/>
<Image x:Name="alarmIconImage"
Source="{Binding AlarmImage}"/>
</StackLayout>
<Label x:Name="alarmTextLabel"
FontSize="14"
VerticalOptions="FillAndExpand"
VerticalTextAlignment="Center"
HorizontalOptions="FillAndExpand"
FontFamily="Avenir Next"
FontAttributes="Bold"
Text="{Binding AlarmText}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
public partial class AlarmPage : ContentPage
{
private List<AlarmPageItem> listViewItems;
private Image alarmIcon;
public AlarmPage ()
{
listViewItems = new List<AlarmPageItem>();
alarmIcon = new Image { Source = "warning.png" };
PopulateList();
InitializeComponent();
listView.ItemsSource = listViewItems;
}
private void OnItemTapped(object sender, ItemTappedEventArgs e)
{
if (e == null) return; // has been set to null, do not 'process' tapped event
Debug.WriteLine("Tapped: " + e.Item);
((ListView)sender).SelectedItem = null; // de-select the row
}
private void PopulateList()
{
listViewItems.Add(new AlarmPageItem() {AlarmImage = alarmIcon, AlarmText = "The front is too heavy", DateTime = DateTime.Now.ToShortTimeString()});
}
}
You are binding as "Source" of your Image
in the XAML an Image
object you are creating in your code behind. 您正在XAML中绑定
Image
“源”,这是您在代码后面创建的Image
对象。
Update your AlarmPageItem
class by making the AlarmImage
property either an ImageSource
or just a simple string
. 通过将
AlarmImage
属性设置为ImageSource
或只是一个简单的string
AlarmPageItem
更新您的AlarmPageItem
类。
Something like: 就像是:
class AlarmPageItem
{
public string AlarmImage { get; set; }
public string AlarmText { get; set; }
//Just a suggestion: Change this property name for something different as it
//can create some confusion.
public DateTime DateTime { get; set; }
...
// Add any other properties your class might have.
}
Once the above is updated just change your code to set the name of the image in the PopulateList
method 更新上述内容后,只需更改代码即可在
PopulateList
方法中设置图像名称
private void PopulateList()
{
listViewItems.Add(new AlarmPageItem() {AlarmImage = "warning.png", AlarmText = "The front is too heavy", DateTime = DateTime.Now.ToShortTimeString()});
}
Hope this helps.- 希望这可以帮助。-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.