加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

xaml – Windows Phone Toolkit ListPicker引发未处理的异常

发布时间:2020-12-14 01:53:28 所属栏目:Windows 来源:网络整理
导读:我正在开发 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 -- /t
我正在开发 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 + "nnExceptionn" + 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元素.您必须绑定它们或使用模板.

在遇到这个问题之后,我在这里找到了一个解释: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,-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
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读