0

我是 xaml 编程的新手。我一直在尝试将多个图像绑定到列表框,但没有成功。我可以在 winrt 应用程序中看到文本,但看不到图像。下面是代码:

Imports Windows.Storage.Pickers
Imports Windows.Storage

Public NotInheritable Class MainPage
    Inherits Page

    Dim p As System.Uri

    ''' <summary>
    ''' Invoked when this page is about to be displayed in a Frame.
    ''' </summary>
    ''' <param name="e">Event data that describes how this page was reached.  The Parameter
    ''' property is typically used to configure the page.</param>
    Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)

    End Sub

Private Async Sub SelectFileName_Click(sender As Object, e As RoutedEventArgs) Handles _SelectFileName.Click

        Dim SelectedFileNameObject As New FileOpenPicker
        SelectedFileNameObject.FileTypeFilter.Add("*")

Dim SelectedFileName As IReadOnlyList(Of StorageFile) = Await SelectedFileNameObject.PickMultipleFilesAsync

        Dim a As New ObservableCollection(Of ImageLoc)
        For i As Integer = 0 To SelectedFileName.Count - 1

            p = New Uri(SelectedFileName.Item(i).Path.ToString, UriKind.RelativeOrAbsolute)

            a.Add(New ImageLoc() With {.ImageLocation = _SelectedFileName.Item(i).Path.ToString, .LineFour = p})

        Next
        ListName.ItemsSource = a

    End Sub

End class

Public Class ImageLoc
    Public location As String

    Property ImageLocation() As String
        Get
            Return location
        End Get
        Set(ByVal value As String)
            location = value
        End Set
    End Property
    Public b As Uri
    Public Property LineFour() As Uri
        Get
            Return b

        End Get
        Set(ByVal value As Uri)
            b = value
        End Set
    End Property
End Class

Xaml 是:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel Height="auto" Width="auto" Orientation="Horizontal">
        <Button x:Name="SelectFileName" Width="100" Height="50" Content="Browse Files" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,40,0"/>
        <ListBox x:Name="ListName" Width="700" Height="auto">
            <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                      <TextBlock Text="{Binding ImageLocation}" Height="auto" Width="auto"/>
                        <Image Height="100" Width="100">
                            <Image.Source>
                                <BitmapImage UriSource="{Binding Path=LineFour}"/>
                            </Image.Source>
                        </Image>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Grid>

有任何想法吗?

4

2 回答 2

0

查看以编程方式更改图像源

基本上:

<Image Margin="5" Source="{Binding BMImage}" Height="100"/>

BitmapImage bmImage;
public BitmapImage BMImage
{
    get
    {
        return bmImage;
    }
}

bmImage = new BitmapImage();
bmImage.UriSource = new Uri(new Uri(
     *your file path*, 
     *your image name*);

有关更多示例,请参见我的博客

于 2012-10-08T18:59:28.647 回答
0

一方面,您实际上不必编写手写BitmapImage代码。Image控制 spy 端口将 an 或 a 直接绑定到ImageSourceURI属性Source。另外,您确定您传递的 Uris 格式正确吗?有时他们需要在前面加上包名前缀才能正常工作。

于 2012-10-08T18:57:11.597 回答