Windows phone 7 再次避免触发\u SelectionChanged当我将SelectedItem设置为null时
我正在开发Windows Phone 8,我有一个单一的选择列表框和此方法:Windows phone 7 再次避免触发\u SelectionChanged当我将SelectedItem设置为null时,windows-phone-7,windows-phone-8,Windows Phone 7,Windows Phone 8,我正在开发Windows Phone 8,我有一个单一的选择列表框和此方法: private void locationsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { ARLocation item = (ARLocation)locationsList.SelectedItem; NavigationService.Navigate(new
private void locationsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ARLocation item = (ARLocation)locationsList.SelectedItem;
NavigationService.Navigate(new Uri("/Views/MapPage.xaml", UriKind.Relative));
locationsList.SelectedItem = null;
}
当我执行locationsList.SelectedItem=null时
再次触发SelectionChanged
事件
如何避免在清除SelectedItem时触发该事件?除非您不想取消订阅该事件,否则无法避免它-您正在更改所选项目,因此它将触发该事件 您可以尝试这样做-取消订阅,然后订阅活动:
locationsList.SelectioChanged -= locationsList_SelectionChanged;
locationsList.SelectedItem = null;
locationsList.SelectioChanged += locationsList_SelectionChanged;
但在这种情况下,进行检查可能更容易:
private void locationsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if ((sender as ListBox).SelectedItem != null)
{
ARLocation item = (ARLocation)locationsList.SelectedItem;
NavigationService.Navigate(new Uri("/Views/MapPage.xaml", UriKind.Relative));
locationsList.SelectedItem = null;
}
}
或者,您可以提供一个布尔值来通知事件应该跳过它:
private bool SkipEvent = false;
private void locationsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (SkipEvent) return;
ARLocation item = (ARLocation)locationsList.SelectedItem;
NavigationService.Navigate(new Uri("/Views/MapPage.xaml", UriKind.Relative));
// Skip block:
try
{
SkipEvent = true;
locationsList.SelectedItem = null;
}
finally { SkipEvent = false; }
}
除非您不想取消订阅该活动,否则无法避免-您正在更改所选项目,因此它将触发该活动 您可以尝试这样做-取消订阅,然后订阅活动:
locationsList.SelectioChanged -= locationsList_SelectionChanged;
locationsList.SelectedItem = null;
locationsList.SelectioChanged += locationsList_SelectionChanged;
但在这种情况下,进行检查可能更容易:
private void locationsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if ((sender as ListBox).SelectedItem != null)
{
ARLocation item = (ARLocation)locationsList.SelectedItem;
NavigationService.Navigate(new Uri("/Views/MapPage.xaml", UriKind.Relative));
locationsList.SelectedItem = null;
}
}
或者,您可以提供一个布尔值来通知事件应该跳过它:
private bool SkipEvent = false;
private void locationsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (SkipEvent) return;
ARLocation item = (ARLocation)locationsList.SelectedItem;
NavigationService.Navigate(new Uri("/Views/MapPage.xaml", UriKind.Relative));
// Skip block:
try
{
SkipEvent = true;
locationsList.SelectedItem = null;
}
finally { SkipEvent = false; }
}
除非您不想取消订阅该活动,否则无法避免-您正在更改所选项目,因此它将触发该活动 您可以尝试这样做-取消订阅,然后订阅活动:
locationsList.SelectioChanged -= locationsList_SelectionChanged;
locationsList.SelectedItem = null;
locationsList.SelectioChanged += locationsList_SelectionChanged;
但在这种情况下,进行检查可能更容易:
private void locationsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if ((sender as ListBox).SelectedItem != null)
{
ARLocation item = (ARLocation)locationsList.SelectedItem;
NavigationService.Navigate(new Uri("/Views/MapPage.xaml", UriKind.Relative));
locationsList.SelectedItem = null;
}
}
或者,您可以提供一个布尔值来通知事件应该跳过它:
private bool SkipEvent = false;
private void locationsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (SkipEvent) return;
ARLocation item = (ARLocation)locationsList.SelectedItem;
NavigationService.Navigate(new Uri("/Views/MapPage.xaml", UriKind.Relative));
// Skip block:
try
{
SkipEvent = true;
locationsList.SelectedItem = null;
}
finally { SkipEvent = false; }
}
除非您不想取消订阅该活动,否则无法避免-您正在更改所选项目,因此它将触发该活动 您可以尝试这样做-取消订阅,然后订阅活动:
locationsList.SelectioChanged -= locationsList_SelectionChanged;
locationsList.SelectedItem = null;
locationsList.SelectioChanged += locationsList_SelectionChanged;
但在这种情况下,进行检查可能更容易:
private void locationsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if ((sender as ListBox).SelectedItem != null)
{
ARLocation item = (ARLocation)locationsList.SelectedItem;
NavigationService.Navigate(new Uri("/Views/MapPage.xaml", UriKind.Relative));
locationsList.SelectedItem = null;
}
}
或者,您可以提供一个布尔值来通知事件应该跳过它:
private bool SkipEvent = false;
private void locationsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (SkipEvent) return;
ARLocation item = (ARLocation)locationsList.SelectedItem;
NavigationService.Navigate(new Uri("/Views/MapPage.xaml", UriKind.Relative));
// Skip block:
try
{
SkipEvent = true;
locationsList.SelectedItem = null;
}
finally { SkipEvent = false; }
}