xaml - Windows Phone 工具包 ListPicker 引发未处理的异常

标签 xaml windows-phone

我正在开发一个 Windows Phone 8 应用程序。我的应用程序使用来自 Tookit 的 ListPicker。我有问题的代码如下所示:

<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。我知道这一点,因为我有以下代码:
    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 需要进入全屏模式时会发生这种情况。我无法弄清楚为什么会发生这种情况。

有没有人有任何见解?

最佳答案

看起来,在全屏模式下,您无法将 ListPicker 的项目设置为 xaml 页面中的特定 UI 元素。您必须绑定(bind)它们或使用模板。
在遇到这个确切的问题后,我在这里找到了一个解释:http://silverlight.codeplex.com/workitem/9412

ListPickerItems are UIElements, and the ListPicker renders them in its presenter. When there are 5 or less items, the expanded mode opens on the current page, and you can see all the items in the presenter. When 6 or more items are present, opening the list picker goes to full mode which opens a new page. This new page has a listbox, which gets its items property set to the listpicker's items. This is where it breaks. By setting the listbox's items to the listpicker's items (in this case a list of listpickeritems), the listbox will put those UIElements into its view. Now a single listboxitem is included in two places on the visual tree.

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


我设法让我的解决方案做这样的事情:
<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
  }
}

关于xaml - Windows Phone 工具包 ListPicker 引发未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17065970/

相关文章:

wpf - 如何将WPF按钮的内容设置为具有多种颜色?

c# - 如何让 MVVM Light Messenger 与 PCL 配合使用

c# - 在 Windows Phone 后台播放音频

wpf - Windows Phone 开发使用 wpf?

c# - 停止 CTRL-Click 影响父数据网格

android - 如何在 Android AXML 中实现这个简单的 XAML 网格布局?

c# - WPF MVVM 为什么使用 ContentControl + DataTemplate View 而不是直接的 XAML 窗口 View ?

c# - 防止键盘打开时页面屏幕向上滚动(C#-XAML-Windows Store App)

c# - Windows 手机 : How to get list of installed applications & install/remove applications remotely

windows-phone-7 - WP8 模拟器不显示 map 内容