:为什么可以';设置Xamarin.Forms.ListView的SelectedItem属性吗? object lastItem=null; foreach(listView.ItemsSource中的对象项) lastItem=项目; if(lastItem!=null) listView.SelectedItem=lastItem;

:为什么可以';设置Xamarin.Forms.ListView的SelectedItem属性吗? object lastItem=null; foreach(listView.ItemsSource中的对象项) lastItem=项目; if(lastItem!=null) listView.SelectedItem=lastItem;,xamarin.forms,Xamarin.forms,但是,当将SelectedItem设置为所需的lastItem时,我得到的只是一个“Sequence contains no matching element”异常。我从itemsource中抓取了最后一项,这怎么可能呢 编辑:添加了上下文。。。ListView XAML和一个在IsoStore中将值表示为不同文本文件的模型(是的,我知道,设置是更好的选择) XAML: C#: 使用系统; 使用系统集合; 使用System.Collections.Generic; 使用系统组件模型; 使用系

但是,当将
SelectedItem
设置为所需的lastItem时,我得到的只是一个“Sequence contains no matching element”异常。我从
itemsource
中抓取了最后一项,这怎么可能呢

编辑:添加了上下文。。。ListView XAML和一个在IsoStore中将值表示为不同文本文件的模型(是的,我知道,设置是更好的选择)

XAML:


C#:

使用系统;
使用系统集合;
使用System.Collections.Generic;
使用系统组件模型;
使用系统文本;
使用System.IO;
使用System.IO.IsolatedStorage;
使用System.Runtime.CompilerServices;
使用系统诊断;
名称空间EasyRideTouch.Models
{
公共类Eintrag:INotifyPropertyChanged
{
公共字符串文件前缀{get;set;}
公共Eintrag(字符串前缀=“”)
{
FilePrefix=前缀;
}
公共日期时间基准
{
获取{
string datestring=ReadStringFromIsoStore();
如果(日期字符串==“”)
返回日期时间。今天;
其他的
返回新的DateTime(Int32.Parse(datestring.Substring(0,4)),Int32.Parse(datestring.Substring(4,2)),Int32.Parse(datestring.Substring(6,2));
}
设置{
string datestring=string.Format(“{0:yyyyMMdd}”,value);
如果(datestring!=ReadStringFromIsoStore())
{
WriteStringToIsoStore(日期字符串);
OnPropertyChanged();
} 
}
}
公共字符串Kategorie
{
获取{return ReadStringFromIsoStore();}
设置{if(value!=ReadStringFromIsoStore()){WriteStringToIsoStore(value);OnPropertyChanged();}}
}
公共字符串酒店
{
获取{return ReadStringFromIsoStore();}
设置{if(value!=ReadStringFromIsoStore()){WriteStringToIsoStore(value);OnPropertyChanged();OnPropertyChanged(“Entfernung”);}
}
公共字符串KmEnde
{
获取{return ReadStringFromIsoStore();}
设置{if(value!=ReadStringFromIsoStore()){WriteStringToIsoStore(value);OnPropertyChanged();OnPropertyChanged(“Entfernung”);}
}
公共字符串Entfernung
{
得到
{
如果(KmEnde==“”| | KmBeginn==“”)
返回“”;
其他的
{
内德;
因特贝金;
尝试
{
ende=Int32.Parse(KmEnde);
beginn=Int32.Parse(KmBeginn);
}
抓住
{
返回“”;
}
int entfernung=ende-beginn;
if(entfernung<0)
返回“”;
其他的
返回entfernung.ToString()+“km”;
}
}
}
公共字符串Reisezweck
{
获取{return ReadStringFromIsoStore();}
设置{if(value!=ReadStringFromIsoStore()){WriteStringToIsoStore(value);OnPropertyChanged();}}
}
公共事件属性更改事件处理程序属性更改;
void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
var handler=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
私有字符串ReadStringFromIsoStore()
{
IsolatedStorageFile isoStore=IsolatedStorageFile.GetUserStoreForApplication();
如果(isoStore.FileExists(GetPropertyName()))
{
使用(IsolatedStorageFileStream isoStream=新的IsolatedStorageFileStream(GetPropertyName(),FileMode.Open,isoStore))
{
使用(StreamReader=新StreamReader(isoStream))
{
返回reader.ReadToEnd();
}
}
}
其他的
返回“”;
}
私有void WriteStringToIsoStore(字符串值)
{
IsolatedStorageFile isoStore=IsolatedStorageFile.GetUserStoreForApplication();
使用(IsolatedStorageFileStream isoStream=新的IsolatedStorageFileStream(GetPropertyName(),FileMode.OpenOrCreate |(isoStore.FileExists(GetPropertyName())?FileMode.Truncate:0),isoStore))
{
使用(StreamWriter=新StreamWriter(isoStream))
{
writer.WriteLine(值);
}
}
}
字符串GetPropertyName()
{
StackTrace callStackTrace=新的StackTrace();
StackFrame propertyFrame=callStackTrace.GetFrame(2);//2:在GetPropertyName框架下
字符串PropertyAccessorName=propertyFrame.GetMethod().Name;
返回FilePrefix+(properyAccessorName.Replace(“get_u3;”,即“”).Repla