当我从列表中选择一个项目时,整个选择过程会继续进行两次。
private void serving_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
try
{
Debug.WriteLine("Serving added to diary");
login(null, null);
}
catch (WebException)
{
Debug.WriteLine(e.Error);
}
ServingList.SelectedIndex = -1;
}
当我遗漏ServingList.SelectedIndex = -1;
时,这个过程会持续一次。但我需要将所选索引返回-1。
我错过了什么?
SelectionChanged上的处理程序:
private void ServingList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("\n[#] ServingList_SelectionChanged");
Debug.WriteLine("Selected item:" + ServingList.SelectedIndex);
Debug.WriteLine("ID of latest TP: " + tp.id);
String itemid = tp.id;
Produkt temp = (Produkt)ServingList.SelectedItem;
if (servingid.Length < 1)
{
servingid = temp.servingid;
}
WebClient addserving = new WebClient();
addserving.Credentials = new NetworkCredential(username.Text, passwort.Password);
addserving.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
addserving.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
addserving.UploadStringAsync(new Uri("http://fddb.info/api/v8/diary/add_item.xml?apikey=ICPOKUI83555PU43"), "&item_id=" + itemid + "&serving_id=" + servingid);
addserving.UploadStringCompleted += new UploadStringCompletedEventHandler(serving_UploadStringCompleted);
Pivot.SelectedIndex = 0;
}
答案 0 :(得分:0)
你正在造成一个无限循环。
您有列表框并更改它会导致操作
更改X&gt;行动X&gt;处理X&gt;改变X(-1)&gt;行动X
等
你需要对某个条件的循环进行某种形式的中断
private void serving_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
//Break condition
if (ServingList.SelectedIndex != -1)
{
try
{
Debug.WriteLine("Serving added to diary");
login(null, null);
}
catch (WebException)
{
Debug.WriteLine(e.Error);
}
ServingList.SelectedIndex = -1;
}
}
通过执行此操作,您只需在需要时将其设置为-1,否则它将继续将所选索引设置为-1,即使它已经实际设置。