0

我有一个动态构建的网格控件。

我正在尝试向它添加一些其他控件,我的 TextBlock 显示正常,但 Image 没有。

是的,图像源是有效的 BitmapImage。CompanyDetails 已正确填充。

这是代码:

Private Sub CreateItemContent(ByVal _Item As CompanyTyping.List, ByVal _TV As RadTileView)
    Dim _Det = Company.CompanyDetails(_Item.CompanyID)
    Dim item As New RadTileViewItem()
    'create and define the inner grid
    Dim _PG As New Grid()
    Dim gridCol1 As New ColumnDefinition()
    gridCol1.Width = New GridLength(200)
    Dim gridCol2 As New ColumnDefinition()
    gridCol2.Width = New GridLength(0, GridUnitType.Star)
    _PG.ColumnDefinitions.Add(gridCol1)
    _PG.ColumnDefinitions.Add(gridCol2)
    'add the initial items to the initial grid
    Dim _Img As New Image()
    _Img.VerticalAlignment = Windows.VerticalAlignment.Top
    _Img.HorizontalAlignment = Windows.HorizontalAlignment.Stretch
    _Img.Visibility = Windows.Visibility.Visible
    _Img.MinHeight = 50
    _Img.MinWidth = 200
    _Img.Source = ByteArrayToImage(_Det(0).Logo)
    Grid.SetColumn(_Img, 1)
    _PG.Children.Add(_Img)
    Dim _PGI As New Grid()
    Dim gridRow1 As New RowDefinition()
    gridRow1.Height = New GridLength(29)
    Dim gridRow2 As New RowDefinition()
    gridRow2.Height = New GridLength(29)
    Dim gridRow3 As New RowDefinition()
    gridRow3.Height = New GridLength(29)
    Dim gridRow4 As New RowDefinition()
    gridRow4.Height = New GridLength(0, GridUnitType.Star)
    _PGI.RowDefinitions.Add(gridRow1)
    _PGI.RowDefinitions.Add(gridRow2)
    _PGI.RowDefinitions.Add(gridRow3)
    _PGI.RowDefinitions.Add(gridRow4)
    Grid.SetColumn(_PGI, 0)
    _PG.Children.Add(_PGI)
    Dim _TB1 As New TextBlock()
    _TB1.Text = _Item.MainContact
    Grid.SetRow(_TB1, 0)
    _PGI.Children.Add(_TB1)


    item.Header = _Item.Name & " - ID: " & _Item.CompanyID
    _TV.Items.Add(item)
    item.SetValue(RadTileViewItem.ContentProperty, _PG)
End Sub

和 ByteArrayToImage:

Public Function ByteArrayToImage(_Bytes As Byte()) As System.Windows.Media.Imaging.BitmapImage
    If _Bytes Is Nothing Then
        Return Nothing
    Else
        Dim _bm As New BitmapImage()
        Using _strm As New MemoryStream(_Bytes)
            With _bm
                .BeginInit()
                .CacheOption = BitmapCacheOption.OnLoad
                .StreamSource = _strm
                .EndInit()
            End With
        End Using
        Return _bm
    End If
End Function

我已经验证,在完整的 XAML 版本中,图像控件的源正确地显示了图像......只是不在此处

4

1 回答 1

1

好像是这样设置

gridCol2.Width = New GridLength(0, GridUnitType.Auto)

将使图像按预期显示。


只是为了展示一些代码,您将如何从包含编码图像缓冲区(例如 JPEG 或 PNG)的字节数组创建 BitmapImage。抱歉,这是 C#,但我不会说 VB。然而,它应该很容易被翻译成 VB。

byte[] buffer = ...
var bitmapImage = new BitmapImage();

using (var stream = new MemoryStream(buffer)) // create stream from buffer
{
    bitmapImage.BeginInit();
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad; // necessary as stream is closed after EndInit
    bitmapImage.StreamSource = stream;
    bitmapImage.EndInit();
}
// stream is closed by leaving the using block

完全没有 VB 知识,我将其翻译如下:

Dim stream As New MemoryStream(buffer)
Dim bitmapImage As New BitmapImage()
bitmapImage.BeginInit()
bitmapImage.CacheOption = BitmapCacheOption.OnLoad
bitmapImage.StreamSource = stream
bitmapImage.EndInit()
stream.Close()
于 2013-03-13T16:51:52.393 回答