【发布时间】:2020-10-27 14:05:44
【问题描述】:
[在此处输入图像描述][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
【问题讨论】:
-
1) 不要在异步操作中使用
Result,学习使用 async/await,2) 不要在循环内分配 ItemsSource,在循环完成后执行,3) 你是确定myAL实际上包含数据?,4) LIstView 的 XAML 在哪里? -
您好,您需要先检查
list是否包含数据,然后才能检查NewItem是否包含匹配的数据。 -
@Jason 感谢您的及时回复,循环后已经尝试过 ItemSource,也使用了 async/await,myAL 在检查断点时确实包含数据
-
可能是您的 XAML 有问题,但由于您没有发布,所以很难说
-
您好@Junior_Jiang 感谢您的回复,列表中包含断点检查时的数据,NewItem 也包含匹配的数据!