引发未处理的异常

引发未处理的异常

本文介绍了Windows Phone 工具包 ListPicker 引发未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Windows Phone 8 应用.我的应用程序使用工具包中的 ListPicker.我有问题的代码如下所示:

I'm working on a Windows Phone 8 app. My app uses the ListPicker from the Tookit. My code in question looks like the following:

<toolkit:ListPicker x:Name="myListPicker" Margin="12,-6,12,-2" Loaded="myListPicker_Loaded">
  <toolkit:ListPicker.Items>
    <!-- Items are defined here -->
  </toolkit:ListPicker.Items>
</toolkit:ListPicker>


private void myListPicker_Loaded(object sender, RoutedEventArgs e)
{
  if ((myListPicker != null) && (viewModel != null))
  {

  }
}

每当项目总数超过某个阈值时,我的应用程序就会抛出 System.ArgumentException.我知道这一点,因为我有以下代码:

Whenever the total number of items passes a certain threshold, my app throws an System.ArgumentException. I know this, because I have the following code:

    private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
        MessageBox.Show(e.ExceptionObject.Message + "\n\nException\n" + e.ExceptionObject.GetType().FullName + "\n" + e.ExceptionObject.StackTrace);
        if (Debugger.IsAttached)
        {
            // An unhandled exception has occurred; break into the debugger
            Debugger.Break();
        }
    }

消息显示值不在预期范围内.".据我所知,当 ListPicker 需要进入全屏模式时会发生这种情况.我不明白为什么会发生这种情况.

The message says "Value does not fall within the expected range.". From what I can tell, this happens when the ListPicker needs to go into full screen mode. I can't figure out why this happens though.

有人有任何见解吗?

推荐答案

看起来,在全屏模式下,您无法将 ListPicker 的项目设置为 xaml 页面中的特定 UI 元素.您必须绑定它们或使用模板.

Seemingly, with full screen mode you cannot set the ListPicker's items to specific UI Elements within the xaml page. You must bind them or use templating.

在遇到这个确切的问题后,我在这里找到了一个解释:http://silverlight.codeplex.com/工作项/9412

After having this exact problem, I found an explanation here: http://silverlight.codeplex.com/workitem/9412

ListPickerItems 是 UIElements,ListPicker 在它的 Presenter 中呈现它们.当项目数小于等于 5 时,展开模式会在当前页面打开,您可以在演示器中看到所有项目.当存在 6 个或更多项目时,打开列表选择器会进入完整模式,打开一个新页面.这个新页面有一个列表框,它的 items 属性设置为列表选择器的项目.这是它破裂的地方.通过将列表框的项目设置为列表选择器的项目(在本例中为列表选择器项目的列表),列表框会将这些 UIElements 放入其视图中.现在单个列表框项包含在可视化树的两个位置.

由于这个问题,ListPicker 只支持数据绑定和模板化.请勿将 ListPicker 的项目设置为特定的 UIElements.

Because of this issue, ListPicker only supports databinding and templating. DO NOT set the ListPicker's items to specific UIElements.

我设法让我的解决方案像这样工作:

I managed to get my solution working doing something like this:

<toolkit:ListPicker x:Name="myListPicker" Margin="12,-6,12,-2" Loaded="myListPicker_Loaded">
  <toolkit:ListPicker.ItemTemplate>
      <DataTemplate>
          <TextBlock Text="{Binding Name}" Tag="{Binding ID}"/>
      </DataTemplate>
   </toolkit:ListPicker.ItemTemplate>
   <toolkit:ListPicker.FullModeItemTemplate>
      <DataTemplate>
          <TextBlock Text="{Binding Name}" Tag="{Binding ID}"/>
      </DataTemplate>
   </toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>


private void myListPicker_Loaded(object sender, RoutedEventArgs e)
{
  if ((myListPicker != null) && (viewModel != null))
  {
     myListPicker.ItemsSource = _Data; //_data is an array of objects with 2 properties named ID & Name
  }
}

这篇关于Windows Phone 工具包 ListPicker 引发未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:19