use*_*192 5 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))
{
}
}
Run Code Online (Sandbox Code Playgroud)
每当项目总数超过某个阈值时,我的应用程序就会抛出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();
}
}
Run Code Online (Sandbox Code Playgroud)
消息称"价值不在预期范围内".据我所知,当ListPicker需要进入全屏模式时会发生这种情况.我无法弄清楚为什么会发生这种情况.
有没有人有任何见解?
看起来,在全屏模式下,您无法将ListPicker的项目设置为xaml页面中的特定UI元素.您必须绑定它们或使用模板.
遇到这个问题后,我在这里找到了一个解释:http://silverlight.codeplex.com/workitem/9412
ListPickerItems是UIElements,ListPicker在它的演示者中呈现它们.如果有5个或更少项目,则会在当前页面上打开展开模式,您可以在演示者中查看所有项目.当存在6个或更多项目时,打开列表选择器将进入完整模式,这将打开一个新页面.这个新页面有一个列表框,它将其items属性设置为listpicker的项目.这就是它破裂的地方.通过将列表框的项目设置为listpicker的项目(在本例中为listpickeritems列表),列表框将把这些UIElements放入其视图中.现在,单个listboxitem包含在可视树上的两个位置.
由于此问题,ListPicker仅支持数据绑定和模板化.不要将ListPicker的项目设置为特定的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
}
}
Run Code Online (Sandbox Code Playgroud)
ListPicker 具有可以在标准列表中显示的预定义数量的元素。在旧版本的工具包中,您可以更改此阈值,但他们已将其删除。
但是,由于该工具包是开源的,因此您始终可以执行我所做的操作 - 进入源代码并更改它以允许更大的列表。