繁体   English   中英

使用Image组件的Wp7 ListView在滚动之前不会显示图像

[英]Wp7 ListView using Image component not showing images until scrolled

我有一个ListView派生的类,它创建了一堆用户控件,这些用户控件是Panel派生的类,每个类都包含一些控件,最重要的是Image控件(m_labelIcon)。 我将此控件的图像源动态设置为资源中的PNG之一:

Uri uri = new Uri("/MyApp;component/Common/Main/res/drawable/some_icon.png");
StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
BitmapImage bitmapSource = new BitmapImage();

bitmapSource.CreateOptions = BitmapCreateOptions.None;

bitmapSource.SetSource(resourceInfo.Stream);
m_labelIcon.Source = bitmapSource;

但是,当列表视图出现时,图像全部丢失。 如果我将列表滚动到最底端,然后再回到顶部,则图像开始出现。 我指定了BitmapCreateOptions.None,它应该防止延迟加载图像(它们在我的资源中,而不是在网络上)。

我也尝试过使用ImageOpened事件,但这不起作用。

有什么想法吗?

谢谢,猪

经过数小时的调试,我偶然发现了简单的解决方案。 即使我像这样重写ArrangeOverride()和MeasureOverride():

protected override Size MeasureOverride(Size availableSize)
{
  Size panelDesiredSize = new Size();

  double height = Math.Max(getIconSizeToUseInPixels(),
    m_labelName.DesiredSize.Height);

  panelDesiredSize = new Size(availableSize.Width, height);

  return panelDesiredSize;
}

protected override Size ArrangeOverride(Size finalSize)
{
  double x = 0;
  double y = 0;

  m_labelName.Measure(finalSize);

  double iconWidth = getIconSizeToUseInPixels();
  Size iconSize = new Size(iconWidth, iconWidth);

  double nameWidth = m_labelName.DesiredSize.Width;
  double nameHeight = m_labelName.DesiredSize.Height;

  m_labelIcon.Arrange(new Rect(
    new Point(x, y), iconSize));

  m_labelName.Arrange(new Rect(
    new Point(iconWidth, y + (finalSize.Height - nameHeight) / 2),
    new Size(nameWidth, nameHeight)));
  m_labelName.Width = nameWidth;
  m_labelName.Height = nameHeight;

  return finalSize; // Returns the final Arranged size
}

我仍然需要在自定义控件的构造函数中手动设置Image控件的Width和Height属性,如下所示:

这解决了问题:

  m_labelIcon.Width = getIconSizeToUseInPixels();
  m_labelIcon.Height = getIconSizeToUseInPixels();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM