如何在Windows Phone应用程序中的简单堆栈面板中虚拟化数据

时间:2014-05-07 12:20:22

标签: c# xaml windows-phone-7 windows-phone-8 virtualization

我已经使用ScrollViewer和stackpanel创建了一个列表。我正在从代码隐藏中将用户控件添加到此stackpanel 我想虚拟化我可以提高应用程序性能的数据 我怎样才能做到这一点?

我无法使用Listbox,因为我将用户控件添加为DataItems,并且每个用户控件都有不同的宽度和高度。请建议如何实现 的代码:

XAML

<ScrollViewer>
    <StackPanel x:Name="stckPnlComponentsLst" Visibility="{Binding IsBusy, Converter={StaticResource BooleanToVisibilityInvertedConverter}}" Orientation="Vertical">
    </StackPanel>
</ScrollViewer>

C#

for (int count = 0; count < countItems; count++)
{
    stckPnlComponentsLst.Children.Add(new ChannelNewsLstControl(ViewModel.ComponentData[count], false, false));
}

2 个答案:

答案 0 :(得分:1)

我没有看到在ScrollViewer中使用StackPanel的任何情况都是个好主意。 你不应该这样做。

有两个控件可以做你想要的,ListBox和ListView。

如果你真的想在ScrollViewer中坚持使用stackpanel,只需用VirtualizingStackPanel替换你的StackPanel。但同样,你不应该这样做。

答案 1 :(得分:0)

改为使用ListBox。检查sample here。例如,在xaml中定义ListBox

<ListBox x:Name="MyList">

然后在codebehind

var MyListData = new List<ChannelNewsLstControl>();
MyListData.Add(new ChannelNewsLstControl {name = "MyFirstChannelName});
MyList.ItemsSource=MyListData;

如果您有很多项目,我建议使用Telerik's DataBoundListBox,因为它比普通ListBox快(至少对于wp7),并支持虚拟化和异步加载。示例将完全相同,因为组件继承了ListBox并添加了之前提到的自己的功能。

编辑:最终答案:

Try to put a grid inside of your ItemTemplate and set its RowDefinition.Height = "Auto". See some details here