我們有一個桌面應用程序,在其中我們有一個ListView的ListView.ItemTemplate
是延伸ScrollViewer
爲什麼回收不起作用?
儘管我們設置VirtualizingStackPanel.IsVirtualizing="True"
和VirtualizingStackPanel.VirtualizationMode="Recycling"
我們注意到一個KPageScrollViewer是KPageScrollViewer的構造函數(這是ItemTemplate
內)總是在查看ListView中的項目時執行。
我們預計它會被創建4到5次,然後,將使用相同的實例來查看數據,因爲我們正在使用回收模式,但這不會發生,因此我們最終會得到更多和更多的KPageScrollViewer實例創建..
是因爲我們已經定製ListView.ItemsPanel
?
<ListView.ItemsPanel>
<ItemsPanelTemplate >
<p:KVirtualizingStackPanel IsItemsHost="True"
有什麼想法嗎? 我想知道哪些原因可能導致丟失回收功能?
<ListView x:Class="KETAB.KStudio.Stage.PagesView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:KETAB.KStudio.Stage"
Name="PagesList" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden"
Loaded="instScroll_Loaded"
AllowDrop="True"
MouseMove="PagesList_MouseMove"
ScrollViewer.PanningMode="None"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.CleanUpVirtualizedItem="PagesList_CleanUpVirtualizedItem"
VirtualizingStackPanel.VirtualizationMode="Recycling"
>
<ListView.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/StageResources.xaml"/>
<ResourceDictionary Source="/KETAB.KStudio.UserControls;component/ScrollViewerStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Opacity="0.4" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Opacity="0.4" Color="Transparent" />
<!--<p:PageWidthConverter x:Key="PageWidthConverter" />-->
<p:PageWidthConverter x:Key="pageWidthConverter" />
<p:PageHeightConverter x:Key="pageHeightConverter" />
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="FocusVisualStyle"
Value="{x:Null}"/>
</Style>
</ResourceDictionary>
</ListView.Resources>
<ListView.ItemTemplate>
<DataTemplate x:Name="PagesViewDataTemplate">
<DataTemplate.Resources>
<Style x:Key="PageHostStyle" TargetType="{x:Type p:KPage}">
</Style>
</DataTemplate.Resources>
<p:KPageScrollViewer Name="ScrollContainer"
Padding="{Binding ElementName=PagesList, Path=PageScrollViewerPadding}"
Height="{Binding ElementName=PagesList, Path=ScaleY, Converter={StaticResource pageHeightConverter}}"
Width="{Binding ElementName=PagesList, Path=ScaleX, Converter={StaticResource pageWidthConverter}}"
MaxHeight="{Binding ElementName=PagesList, Path=Height}"
MaxWidth="{Binding ElementName=PagesList, Path=Width}"
ScrollViewer.HorizontalScrollBarVisibility="{Binding ElementName=PagesList, Path=PageScrollVisibilityHori}"
ScrollViewer.VerticalScrollBarVisibility="{Binding ElementName=PagesList, Path=PageScrollVisibilityVert}" >
<Grid x:Name="MarginStack" >
<p:KPage x:Name="KPage" SizeChanged="KPage_SizeChanged" >
</p:KPage>
</Grid>
</p:KPageScrollViewer>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate >
<p:KVirtualizingStackPanel IsItemsHost="True"
x:Name="WrapPanel1"
Orientation="{Binding ElementName=PagesList, Path=MyOrientation}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ClipToBounds="{Binding ElementName=PagesList, Path=PanelClipToBounds}"
Height="{Binding ElementName=PagesList, Path=ScaleY, Converter={StaticResource pageHeightConverter}}"
Width="{Binding ElementName=PagesList, Path=ScaleX, Converter={StaticResource pageWidthConverter}}"
MaxHeight="{Binding ElementName=PagesList, Path=Height}"
MaxWidth="{Binding ElementName=PagesList, Path=Width}"
>
</p:KVirtualizingStackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
編輯
這裏就是ListView的使用:(P:PagesView)
<Window x:Class="KETAB.KStudio.UI.WindowMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:kStudioControls="clr-namespace:KETAB.KStudio.UI"
xmlns:toolBoxUIPanel="clr-namespace:KETAB.KStudio.UI"
xmlns:Controls="clr-namespace:KETAB.KStudio.UserControls;assembly=KETAB.KStudio.UserControls"
xmlns:p="clr-namespace:KETAB.KStudio.Stage;assembly=KETAB.KStudio.Stage"
xmlns:tb="http://www.hardcodet.net/taskbar"
SizeChanged="Window_SizeChanged"
WindowState="Maximized"
WindowStyle="None"
Background="Transparent"
ResizeMode="CanResizeWithGrip"
PreviewKeyUp="WindowMain_PreviewKeyUp"
Closed="WindowMain_Closed"
Stylus.IsPressAndHoldEnabled="False"
Stylus.IsTapFeedbackEnabled="False"
Stylus.IsTouchFeedbackEnabled="False"
Stylus.IsFlicksEnabled="False"
AllowsTransparency="True">
<Grid FlowDirection="LeftToRight" Name="MainUIContainer">
<Grid.RowDefinitions>
<RowDefinition Height="auto" MinHeight="0" />
<RowDefinition />
</Grid.RowDefinitions>
<tb:TaskbarIcon
x:Name="MyNotifyIcon"
IconSource="/KETABStudio;component/KStudioControls/KStudioIcons/Notify.ico"
ToolTipText="KETAB Studio" Visibility="Collapsed"/>
<Border Name="borderContainer" ClipToBounds="True" Margin="0,0,0,0" Grid.Row="1" Grid.Column="0" BorderThickness="2, 0, 2, 2"
BorderBrush="{StaticResource MainWindowBorderColor}" Background="White">
<Grid Background="Transparent" ClipToBounds="True" Name="MainUISplit" SizeChanged="MainUISplit_SizeChanged">
<!--main backgound depends on this grid-->
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid Background="{StaticResource background}" Margin="0" ClipToBounds="True" Name="applicationBackground">
<Rectangle Name="OpacityRectangle" Visibility="Hidden" Margin="0,0,-60,0" Fill="Black" ></Rectangle>
</Grid>
<!--Page Area-->
<Grid Grid.Column="0" Grid.Row="0" Margin="0" Name="PageArea" AllowDrop="True">
<p:PagesView Name="PageList" />
<Controls:PageLoadingControl x:Name="pageLoadingControl" Visibility="Collapsed"/>
<Controls:HelpMode x:Name="HelpModeInstance" Visibility="Collapsed"/>
</Grid>
</Grid>
</Border>
</Grid>
</Window>
請顯示你的ListView是在 – Alan 2013-03-17 09:35:36
裏面請參見上面的編輯,我試圖刪除不相關的代碼來關注這個問題。 – simo 2013-03-17 11:01:29
如果垂直滾動條被禁用,你如何滾動這個ListView'crollViewer.VerticalScrollBarVisibility = 「隱藏」'?我刪除了這一行,虛擬化開始在我的示例應用程序中正常工作。 – vorrtex 2013-03-17 11:28:50