我有一个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事件,但这不起作用。
有什么想法吗?
谢谢, 猪
答案 0 :(得分:0)
经过几个小时的调试,我偶然发现了简单的解决方案。即使我像这样覆盖了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();