2011-05-28 86 views
2

我试图显示列表框中嵌入我的asssembly中的图像。我可以使用转换器将图像显示出来,但不是加载,而是静止不动,它们不断从组件中重新加载,导致它们闪烁。我使用相同的转换器来加载我的应用程序周围的其他地方的图标,但这个问题不会发生 - 它似乎是以某种方式由lisbox造成的。我尝试删除VisualStates并切换转换器返回的位图图像的CreateOption,但我得到相同的结果。我相当确信这在WP7.0上没有发生,只有7.1。图像在WP7中闪烁ListBox

的风格是:

<Style x:Key="SnapshotList" TargetType="ListBox"> 
    <Setter Property="Margin" Value="2" /> 
    <Setter Property="BorderThickness" Value="1"/> 
    <!--<Setter Property="Background" Value="{StaticResource WindowBackgroundBrush}" /> 
    <Setter Property="BorderBrush" Value="{StaticResource PhoneBorderBrush}" />--> 
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/> 
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
    <!--Setter Property="OverridesDefaultStyle" Value="True"/--> 
    <Setter Property="ItemTemplate" Value="{StaticResource SnapshotTemplate}"/> 
    <Setter Property="ItemsPanel"> 
     <Setter.Value> 
      <ItemsPanelTemplate> 
       <Controls:WrapPanel HorizontalAlignment="Stretch" VerticalAlignment="Top"/> 
         <!--<WP7:BindingHelper.Binding> 
          <WP7:RelativeSourceBinding Path="(FrameworkElement.ActualWidth)" TargetProperty="Width" 
             RelativeMode="FindAncestor" 
             AncestorType="ScrollContentPresenter" /> 
         </WP7:BindingHelper.Binding>--> 
       <!--</Controls:WrapPanel>--> 

      </ItemsPanelTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBox"> 
       <Border Name="Border" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" 
         BorderBrush="{StaticResource PhoneBorderBrush}" CornerRadius="2"> 
        <ScrollViewer Margin="0"> 
         <ItemsPresenter/> 
        </ScrollViewer> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 

有问题的列表框:

<ListBox x:Name="lstIcon" ItemsSource="{Binding AvailableIcons}" SelectedItem="{Binding Recipe.Icon,Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectionMode="Single" 
    Style="{StaticResource SnapshotList}"> 
<ListBox.ItemTemplate> 
    <DataTemplate> 
     <Border Background="Transparent" MinWidth="30" MinHeight="30" Margin="3" Padding="3"> 
      <Image Source="{Binding Converter={StaticResource IconConverter}, ConverterParameter=32}" Stretch="None" MinWidth="30" MinHeight="30" /> 
     </Border> 
    </DataTemplate> 
</ListBox.ItemTemplate> 

转换器:

public class IconConverter : IValueConverter 
    { 
     private static IEnumerable<string> _names; 
     private static IEnumerable<string> ResourceNames { 
      get { 
       if (_names == null) { 
        _names = WP7Utils.GetResourcePaths(Assembly.GetExecutingAssembly()).Select(p=>System.Convert.ToString(p)).ToList(); 
       } 
       return _names; 
      } 
     } 

     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { 
      string baseFilename = (value ?? Shared.Constants.DefaultIcon).ToString().Trim(); 
      string size = (parameter ?? "32").ToString(); 
      try { 
       BitmapImage img = new BitmapImage(); 
       using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { 
        string fileName = string.Format("{0}_{1}.png", baseFilename, size); 
        img = ResourceHelper.GetBitmap("Resources/types/" + fileName, "MyAssembly"); 
       } 
       return img; 
      } catch (Exception ex) { 
       Console.WriteLine(string.Format("Error loading image {0} ({1}px): {2}", baseFilename, size, ex.Message)); 
      } 
      return value; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { 
      throw new NotImplementedException(); 
     } 
    } 

回答

1

这是由于在ListBox的DataTemplate中指定了MinHeight和MinWidth引起的。删除属性解决了问题。

0

您还应该将缓存上的图像缓存设置为BitmpaCache,以防止需要重新加载/重绘图像的框架。