为什么回收不起作用?

时间:2013-02-27 14:00:33

标签: .net wpf scroll recycle

我们有一个桌面应用程序,我们有一个ListView,ListView.ItemTemplate是一个KPageScrollViewer,它扩展了ScrollViewer

虽然我们设置了VirtualizingStackPanel.IsVirtualizing="True"VirtualizingStackPanel.VirtualizationMode="Recycling"

我们注意到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>

1 个答案:

答案 0 :(得分:0)

我阅读以下内容可能会有所帮助......与您使用ItemsPanel的方式有关:Virtualizing an ItemsControl? - 我发现这不是完全相同的主题,但它有一个很好的解释一个非常相似的问题。

希望它会有所帮助。