【问题标题】:Image flickering in WP7 ListBoxWP7 ListBox 中的图像闪烁
【发布时间】:2011-05-28 17:28:31
【问题描述】:

我正在尝试在 ListBox 中显示嵌入在我的程序集中的图像列表。我可以使用转换器显示图像,但不是加载,然后保持静止,而是不断地从程序集中重新加载,导致它们闪烁。我正在使用相同的转换器在我的应用程序周围的各个其他地方加载图标,但没有发生这个问题 - 它似乎是由 lisbox 引起的。我尝试删除 VisualStates 并为转换器返回的位图图像切换 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();
        }
    }

【问题讨论】:

    标签: listbox datatemplate ivalueconverter windows-phone-7


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2010-10-29
        • 2010-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-22
        • 2015-04-25
        相关资源
        最近更新 更多