Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows phone 7 再次避免触发\u SelectionChanged当我将SelectedItem设置为null时_Windows Phone 7_Windows Phone 8 - Fatal编程技术网

Windows phone 7 再次避免触发\u SelectionChanged当我将SelectedItem设置为null时

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

我正在开发Windows Phone 8,我有一个单一的选择列表框和此方法:

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; }
}