我正在尝试在ListBox中显示嵌入在我的asssembly中的图像列表。我可以使用转换器显示图像,但不是加载,而是保持静止,它们不断从组件重新加载,导致它们闪烁。我正在使用相同的转换器在我的应用程序周围的各种其他地方加载图标,但这个问题不会发生 - 它似乎是由lisbox以某种方式。我已经尝试删除VisualStates并切换转换器返回的Bitmap图像的CreateOption,但我得到了相同的结果。我很确定这不会发生在WP7.0上,只有7.1。
风格是:
<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();
}
}
答案 0 :(得分:1)
这是通过在ListTe的DataTemplate中指定MinHeight和MinWidth引起的。删除属性修复了问题。
答案 1 :(得分:0)
您还应该将映像上的缓存设置为BitmpaCache
,以防止框架需要重新加载/重绘图像。