object lastItem = null;
foreach (object item in listView.ItemsSource)
lastItem = item;
if (lastItem != null)
listView.SelectedItem = lastItem;
但我得到的只是一个'序列中没有匹配元素'将SelectedItem
设置为所需的lastItem时出现异常。这怎么可能,因为我从ItemsSource
?
编辑:已添加上下文... ListView XAML和一个模型,表示值为IsoStore中的不同文本文件(是的,我知道,设置将是更好的选择)
XAML:
<ListView Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="3" x:Name="listView" IsVisible="true">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand" Padding="5">
<Label Text="{Binding Datum, StringFormat='{0:d}'}" />
<StackLayout Orientation="Vertical" Spacing="0" VerticalOptions="StartAndExpand">
<StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand">
<Label Text="{Binding KmBeginn}" />
<Label Text="-" />
<Label Text="{Binding KmEnde}" />
<Label Text=" (" />
<Label Text="{Binding Entfernung}" />
<Label Text=" )" />
<Label Text="{Binding Kategorie}" />
</StackLayout>
<Label Text="{Binding Reisezweck}" VerticalOptions="Fill" />
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C#:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.CompilerServices;
using System.Diagnostics;
namespace EasyRideTouch.Models
{
public class Eintrag : INotifyPropertyChanged
{
public string FilePrefix { get; set; }
public Eintrag(string prefix = "")
{
FilePrefix = prefix;
}
public DateTime Datum
{
get {
string datestring = ReadStringFromIsoStore();
if (datestring == "")
return DateTime.Today;
else
return new DateTime(Int32.Parse(datestring.Substring(0, 4)), Int32.Parse(datestring.Substring(4, 2)), Int32.Parse(datestring.Substring(6, 2)));
}
set {
string datestring = String.Format("{0:yyyyMMdd}", value);
if (datestring != ReadStringFromIsoStore())
{
WriteStringToIsoStore(datestring);
OnPropertyChanged();
}
}
}
public string Kategorie
{
get { return ReadStringFromIsoStore(); }
set { if (value != ReadStringFromIsoStore()) { WriteStringToIsoStore(value); OnPropertyChanged(); } }
}
public string KmBeginn
{
get { return ReadStringFromIsoStore(); }
set { if (value != ReadStringFromIsoStore()) { WriteStringToIsoStore(value); OnPropertyChanged(); OnPropertyChanged("Entfernung"); } }
}
public string KmEnde
{
get { return ReadStringFromIsoStore(); }
set { if (value != ReadStringFromIsoStore()) { WriteStringToIsoStore(value); OnPropertyChanged(); OnPropertyChanged("Entfernung"); } }
}
public string Entfernung
{
get
{
if (KmEnde == "" || KmBeginn == "")
return "";
else
{
int ende;
int beginn;
try
{
ende = Int32.Parse(KmEnde);
beginn = Int32.Parse(KmBeginn);
}
catch
{
return "";
}
int entfernung = ende - beginn;
if (entfernung < 0)
return "";
else
return entfernung.ToString() + "km";
}
}
}
public string Reisezweck
{
get { return ReadStringFromIsoStore(); }
set { if (value != ReadStringFromIsoStore()) { WriteStringToIsoStore(value); OnPropertyChanged(); } }
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private string ReadStringFromIsoStore()
{
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
if (isoStore.FileExists(GetPropertyName()))
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(GetPropertyName(), FileMode.Open, isoStore))
{
using (StreamReader reader = new StreamReader(isoStream))
{
return reader.ReadToEnd();
}
}
}
else
return "";
}
private void WriteStringToIsoStore(string value)
{
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(GetPropertyName(), FileMode.OpenOrCreate|(isoStore.FileExists(GetPropertyName()) ? FileMode.Truncate : 0), isoStore))
{
using (StreamWriter writer = new StreamWriter(isoStream))
{
writer.WriteLine(value);
}
}
}
string GetPropertyName()
{
StackTrace callStackTrace = new StackTrace();
StackFrame propertyFrame = callStackTrace.GetFrame(2); // 2: below GetPropertyName frame
string properyAccessorName = propertyFrame.GetMethod().Name;
return FilePrefix + (properyAccessorName.Replace("get_","").Replace("set_",""));
}
}
}