[在此处输入图片描述][1]我正在尝试在列表视图中添加和显示所选数据,但不幸的是无法在列表视图中显示数据,如果有人可以提供帮助,谢谢!(这是我的代码版本)
namespace AppContacts
{
public partial class MainPage : ContentPage
{
SQLiteAsyncConnection _connection = DependencyService.Get().GetConnection();
public MainPage()
{
InitializeComponent();
this.BindingContext = this;
ReadSelectedData();
}
public void ReadSelectedData()
{
int[] intArray;
intArray = new int[4];
intArray[0] = 175;
intArray[1] = 197;
intArray[1] = 757;
intArray[3] = 915;
var list = _connection.Table<Contacts>().ToListAsync().Result;
var myAL = new ArrayList();
foreach (int rowList in intArray)
{
var NewItem = list.Where(x => x.Contact_ID.Equals(rowList));
myAL.Add(NewItem);
}
}
}
}
添加模型类和 XAML 页面,XAML 页面名称为 ContentPage1
XAML 页面页面名称为 ContentPage1
<StackLayout>
<ListView x:Name="listView" HasUnevenRows="True" ItemsSource="{Binding Source={x:Reference ContentPage1}, Path=BindingContext.myAL}" IsVisible="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
</ViewCell.ContextActions>
<StackLayout Orientation="Vertical" VerticalOptions="CenterAndExpand">
<Frame>
<StackLayout Orientation="Vertical" VerticalOptions="Center" >
<Label Text="Hello World" HorizontalOptions="Center"></Label>
<Label Text="{Binding Contact_Address}" HorizontalOptions="Center"></Label>
<Label Text="{Binding Contact_eMail}" HorizontalOptions="Center"></Label>
</StackLayout>
</Frame>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
模型类
using SQLite
namespace AppContacts.Models
{
public class Contacts
{
[PrimaryKey][AutoIncrement]
public int Contact_ID { get; set; }
public string Contact_Name { get; set; }
public string Contact_Address {get; set; }
public string Contact_eMail { get; set; }
}
}
[Output][1]
[enter image description here][2]
[1]: https://i.stack.imgur.com/ci5fK.png
[2]: https://i.stack.imgur.com/ppmp2.jpg
尝试在 中调用ReadSelectedData
方法OnAppearing()
,并设置ItemsSource
为ListView。
例如:
protected override async void OnAppearing()
{
base.OnAppearing();
int[] intArray;
intArray = new int[4];
intArray[0] = 175;
intArray[1] = 197;
intArray[2] = 757;
intArray[3] = 915;
var list = await _connection.Table<Contacts>().ToListAsync().Result;
var myAL = new ArrayList();
foreach (int rowList in intArray)
{
var NewItem = list.Where(x => x.Contact_ID.Equals(rowList));
myAL.Add(NewItem);
}
listView.ItemSource = myAL;
}
============================更新==================== ========
我查看了有关Enumerable.Where Method的文档,您可以看到方法返回的不是TSource
对象。
public static System.Collections.Generic.IEnumerable Where >(this System.Collections.Generic.IEnumerable source, Func<TSource,bool> >predicate);
它返回一个IEnumerable<TSource>
对象,因此您的代码:
var NewItem = list.Where(x => x.Contact_ID.Equals(rowList));
应该返回一个IEnumerable<Contacts>
对象。
因此,您可以修改代码如下:
foreach (int rowList in intArray)
{
var NewItem = list.Where(x => x.Contact_ID.Equals(rowList));
myAL.Add(NewItem.FirstOrDefault());
}
使用NewItem.FirstOrDefault()
得到的Contacts
对象。
否则,你不能使用Enumerable.Where 方法来做,也可以使用foreach
如下实现:
foreach (int rowList in intArray)
{
foreach (var newitem in list)
{
if(newitem.Contact_ID == rowList)
{
myAL.Add(newitem);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句