2013-03-23 171 views
0

希望我能解釋什麼,我需要和我有以下列表框有什麼問題是清除圖像緩存

<ListBox Margin="0,8,0,0" toolkit:TiltEffect.IsTiltEnabled="True" x:Name="ImageList" ItemsSource="{Binding Images}" HorizontalAlignment="Center" BorderThickness="4"> 

     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid Background="{Binding imageID, Converter={StaticResource ImageConverter}}" Width="125" Height="125" Margin="6" Tap="list_OnTap"> 

        <TextBlock Name="description" Foreground="{Binding TextColor}" Text="{Binding text}" Visibility="{Binding ShowText, Converter={StaticResource BooleanToVisibilityConverter}}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap"/> 
        <toolkit:ContextMenuService.ContextMenu> 
         <toolkit:ContextMenu IsZoomEnabled="False" Name="deletectx"> 
          <toolkit:MenuItem Header="delete" Click="delete_Click"/> 
         </toolkit:ContextMenu> 
        </toolkit:ContextMenuService.ContextMenu> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <toolkit:WrapPanel/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
    </ListBox> 

</Grid> 

上面的代碼,使圖像的網格我用網格背景來顯示圖像,因爲我需要顯示我的圖像上的文字(不知道是否有任何其他方式做到這一點) 此頁面加載有關使用尺寸爲125x125像素的30張圖像,並約4KB每個。我注意到它消耗了大量的內存。我在這裏有關清除的圖像的緩存中讀取一些帖子,但我不知道我應該怎樣做,與考慮我設置網格背景我的形象不是圖像控件上述代碼。

我也許能夠訪問列表框裏面的網格,但無論我做的話,會被應用到第一圖像不僅沒有休息。我需要在我離開的事件清除圖像緩存。

另一個問題,我也有一些性能問題,進入這個頁面需要一點點時間,我在Windows Phone應用分析器得到低幀率警告,不知道我在做什麼(通過Converter加載圖像for每個列表框項目)是否正確! 如何讓這個更快?

+0

歡迎計算器!爲您的問題選擇標題時,不需要在其中添加標籤。請閱讀http://meta.stackexchange.com/questions/19190進行討論,以及爲什麼他們不需要。此外,最後的問候是不必要的,包括任何問候,而不是你的聯繫卡,它顯示在所有問題的右下角。 – Patrick 2013-03-23 23:20:06

回答

0

要確保您可以收回由默認的圖像緩存行爲,你可以使用以下使用的任何記憶:(從另一個項目剪斷並略加編輯,但應該是工作)

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) 
{ 
    if (e.NavigationMode == NavigationMode.Back) 
    { 
     // Unload all images so as to reclaim any allocated memory 
     foreach (var child in VisualTreeHelperEx.GetVisualChildren(this.ImageList).Where(c => c is Image)) 
     { 
      var image = child as Image; 

      if (image != null) 
      { 
       var bitmapImage = image.Source as BitmapImage; 

       if (bitmapImage != null) 
       { 
        System.Diagnostics.Debug.WriteLine("unloading " + bitmapImage.UriSource); 
        bitmapImage.UriSource = null; 
       } 

       image.Source = null; 
      } 
     } 
    } 
} 

其中採用這個幫手:

public static class VisualTreeHelperEx 
{ 
    public static IEnumerable<DependencyObject> GetVisualChildren(DependencyObject element) 
    { 
     if (element == null) 
     { 
      throw new ArgumentNullException("element"); 
     } 

     return GetVisualChildrenAndSelfIterator(element).Skip(1); 
    } 

    public static IEnumerable<DependencyObject> GetVisualChildrenAndSelfIterator(DependencyObject element) 
    { 
     Debug.Assert(element != null, "element should not be null!"); 

     yield return element; 

     int count = VisualTreeHelper.GetChildrenCount(element); 

     for (int i = 0; i < count; i++) 
     { 
      yield return VisualTreeHelper.GetChild(element, i); 
     } 
    } 
} 

這可能有它自己的性能問題,所以謹慎使用並測試對實際性能的影響。

在頁面加載性能問題來一次加載所有圖像可能是由於。在頁面加載完成後,您可以將它們填充到列表中以避免這種情況。如果實際的圖像比背景面積應設置DecodePixelHeightDecodePixelWidth。由於許多複雜的轉換器會影響性能,因此可能需要刪除轉換器並添加完整路徑的屬性。