2011-10-22 47 views
0

我有一個ListView派生類,它創建了一組用戶控件是面板派生的類,每個控件包含幾個控件,最重要的是圖像控件(m_labelIcon)。我動態設置圖像源出於此控制在我的資源PNG格式之一:Wp7使用圖像組件的ListView不顯示圖像,直到滾動

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

[?匈牙利表示法是壞的,mkay](http://www.joelonsoftware.com/articles/Wrong.html) –

回答

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控件的高度屬性:

THIS固定的問題:

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