0

VB.netのListViewへの画像のバインドに問題があります。画像のサイズ変更、変換、圧縮を行うアプリを作成していますが、選択した画像を画像名と一緒にListViewに表示したいと思います。

これは、ListViewのXAMLコードです。

<ListView x:Name="ListView" ItemsSource="{Binding Image}" HorizontalAlignment="Left" Height="481" Margin="10,275,0,0" VerticalAlignment="Top" Width="1069" BorderBrush="#FF003859" Foreground="White" Background="#42FFFFFF" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" BorderThickness="6,0,6,6" FontSize="16" SelectionMode="Multiple">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="vertical">
                        <Grid Height="160" Margin="6">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Border Width="160" Height="160">
                                <Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="{Binding ImageID}" Stretch="Uniform" />
                            </Border>
                            <StackPanel Grid.Column="1" VerticalAlignment="Center" Margin="10,0,0,0">
                                <TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextStyle}" TextWrapping="NoWrap"/>
                            </StackPanel>
                        </Grid>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Imageクラスのコードは次のとおりです。

Imports System.Collections.ObjectModel

Public Class Image</code>

    Private _Title As String
    Private _ImageID As BitmapImage

    Public ReadOnly Property Title() As String
        Get
            Return _Title
        End Get
    End Property
    Public ReadOnly Property Image() As BitmapImage
        Get
            Return _ImageID
        End Get
    End Property

    Public Sub New(ByVal Title As String, ByVal ImageID As BitmapImage)
        _Title = Title
        _ImageID = ImageID
    End Sub

End Class

そして、これがListViewに画像を追加するボタンのコードです。

Private Async Sub AddImage_Click(sender As Object, e As RoutedEventArgs) Handles AddImage.Click

        Dim picker As New FileOpenPicker()

        picker.SuggestedStartLocation = PickerLocationId.Desktop
        picker.ViewMode = PickerViewMode.Thumbnail
        picker.FileTypeFilter.Add(".jpg")
        picker.FileTypeFilter.Add(".jpeg")
        picker.FileTypeFilter.Add(".bmp")
        picker.FileTypeFilter.Add(".gif")
        picker.FileTypeFilter.Add(".png")
        picker.FileTypeFilter.Add(".tiff")
        picker.FileTypeFilter.Add(".tga")

        Dim files As IReadOnlyList(Of StorageFile) = Await picker.PickMultipleFilesAsync

        Dim imagearray(10000000) As BitmapImage
        Dim i = 0
        If files.Count > 0 Then
            For Each file In files
                imagearray(i) = New BitmapImage(New Uri(file.Path))
                i += 1
            Next
            Dim j = 0
            For Each file In files
                ListView.Items.Add(New Image(file.Name, imagearray(j)))
                j += 1
            Next
        End If

    End Sub

これを手伝ってください。

編集:

ListView XAMLコード:

<ListView x:Name="ListView" HorizontalAlignment="Left" Height="481" Margin="10,275,0,0" VerticalAlignment="Top" Width="1069" BorderBrush="#FF003859" Foreground="White" Background="#42FFFFFF" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" BorderThickness="6,0,6,6" FontSize="16" SelectionMode="Multiple">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="vertical">
                        <Grid Height="160" Margin="6">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Border Width="160" Height="160">
                                <Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="{Binding ImageID}" Stretch="Uniform" />
                            </Border>
                            <StackPanel Grid.Column="1" VerticalAlignment="Center" Margin="10,0,0,0">
                                <TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextStyle}" TextWrapping="NoWrap"/>
                            </StackPanel>
                        </Grid>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

画像クラス:

Imports System.Collections.ObjectModel

Public Class Image

    Private _Title As String
    Private _Image As BitmapImage

    Public ReadOnly Property Title() As String
        Get
            Return _Title
        End Get
    End Property
    Public ReadOnly Property ImageID() As BitmapImage
        Get
            Return _Image
        End Get
    End Property

    Public Sub New(ByVal Title As String, ByVal ImageID As BitmapImage)
        _Title = Title
        _Image = ImageID
    End Sub

End Class

ボタン:

Dim ImageCollection As New Collection(Of Image)

     Private Async Sub AddImage_Click(sender As Object, e As RoutedEventArgs) Handles AddImage.Click

            Dim picker As New FileOpenPicker()

            picker.SuggestedStartLocation = PickerLocationId.Desktop
            picker.ViewMode = PickerViewMode.Thumbnail
            picker.FileTypeFilter.Add(".jpg")
            picker.FileTypeFilter.Add(".jpeg")
            picker.FileTypeFilter.Add(".bmp")
            picker.FileTypeFilter.Add(".gif")
            picker.FileTypeFilter.Add(".png")
            picker.FileTypeFilter.Add(".tiff")
            picker.FileTypeFilter.Add(".tga")

            Dim files As IReadOnlyList(Of StorageFile) = Await picker.PickMultipleFilesAsync

            Dim imagearray(10000000) As BitmapImage
            Dim i = 0
            If files.Count > 0 Then
                For Each file In files
                    imagearray(i) = New BitmapImage(New Uri(file.Path))
                    i += 1
                Next
                Dim j = 0
                For Each file In files
                    ImageCollection.Add(New Image(file.Name, imagearray(j)))
                    j += 1
                Next
                For Each file In files
                    ListView.ItemsSource = ImageCollection
                Next

            End If

        End Sub

これは私が今持っているコードですが、画像はまだ表示されていません。なにが問題ですか?

新しい編集は次のとおりです。

ListView XAMLコード:

<ListView x:Name="ListView" DataContext="{Binding Image}" HorizontalAlignment="Left" Height="481" Margin="10,275,0,0" VerticalAlignment="Top" Width="1069" BorderBrush="#FF003859" Foreground="White" Background="#42FFFFFF" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" BorderThickness="6,0,6,6" FontSize="16" SelectionMode="Multiple">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="vertical">
                        <Grid Height="160" Margin="6">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Border Width="160" Height="160">
                                <Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="{Binding ImageProperty}" Stretch="Uniform" />
                            </Border>
                            <StackPanel Grid.Column="1" VerticalAlignment="Center" Margin="10,0,0,0">
                                <TextBlock Text="{Binding TitleProperty}" Style="{StaticResource TitleTextStyle}" TextWrapping="NoWrap"/>
                            </StackPanel>
                        </Grid>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

画像クラス:

Imports System.Collections.ObjectModel

Public Class Image

    Implements INotifyPropertyChanged

    Private ImageX As BitmapImage
    Private TitleX As String

    Public ReadOnly Property ImageProperty() As BitmapImage
        Get
            Return ImageX
        End Get
    End Property

    Public ReadOnly Property TitleProperty() As String
        Get
            Return TitleX
        End Get
    End Property

    Public Sub New(ByVal ImageTitle As String, ByVal ImageID As BitmapImage)
        TitleX = ImageTitle
        ImageX = ImageID
    End Sub

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Sub NotifyPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

End Class

そして、ImageCollectionクラスのボタン:

 Dim ImageCollection As New ImageCollectionClass

    Private Async Sub AddImage_Click(sender As Object, e As RoutedEventArgs) Handles AddImage.Click

        Dim picker As New FileOpenPicker()

        picker.SuggestedStartLocation = PickerLocationId.Desktop
        picker.ViewMode = PickerViewMode.Thumbnail
        picker.FileTypeFilter.Add(".jpg")
        picker.FileTypeFilter.Add(".jpeg")
        picker.FileTypeFilter.Add(".bmp")
        picker.FileTypeFilter.Add(".gif")
        picker.FileTypeFilter.Add(".png")
        picker.FileTypeFilter.Add(".tiff")
        picker.FileTypeFilter.Add(".tga")

        Dim files As IReadOnlyList(Of StorageFile) = Await picker.PickMultipleFilesAsync

        Dim imagearray(10000000) As BitmapImage
        Dim i = 0
        If files.Count > 0 Then
            For Each file In files
                imagearray(i) = New BitmapImage(New Uri(file.Path))
                i += 1
            Next
            Dim j = 0
            For Each file In files
                ImageCollection.AddImage(file.Name, imagearray(j))
                j += 1
            Next
            For Each file In files
                ListView.ItemsSource = ImageCollection
            Next

        End If

    End Sub

Public Class ImageCollectionClass

    Public ImageCollectionClass As New ObservableCollection(Of Image)

    Public Sub AddImage(ByVal ImageTitleInClass As String, ByVal ImageIDInClass As BitmapImage)
        ImageCollectionClass.Add(New Image(ImageTitleInClass, ImageIDInClass))
    End Sub

End Class

しかし、それは機能しません。今何が悪いのか教えてください!

4

1 に答える 1

0

元のループはかなり複雑です (それはプログラムの直接の原因ではないと考えられます)。このコードのすべて:

    Dim imagearray(10000000) As BitmapImage
    Dim i = 0
    If files.Count > 0 Then
        For Each file In files
            imagearray(i) = New BitmapImage(New Uri(file.Path))
            i += 1
        Next
        Dim j = 0
        For Each file In files
            ImageCollection.AddImage(file.Name, imagearray(j))
            j += 1
        Next
        For Each file In files
            ListView.ItemsSource = ImageCollection
        Next
    End If

次のように折りたたむことができます。

    For Each file In files
        ImageCollection.AddImage(file.Name, New BitmapImage(New Uri(file.Path)))
    Next
    ListView.ItemsSource = ImageCollection.ImageCollectionClass

として必要であることに注意ImageCollectionClassしてくださいItemsSource

そこから、XAML イメージ サンプルのシナリオ 2 のコードを組み込み、 SetSourceAsyncを介してイメージを取得するように変更できます。

    Dim fileStream As IRandomAccessStream
    For Each file In files
        fileStream = Await file.OpenAsync(Windows.Storage.FileAccessMode.Read)

        Dim bitmapImage As New BitmapImage()
        Await bitmapImage.SetSourceAsync(fileStream)

        ImageCollection.AddImage(file.Name, bitmapImage)
    Next
    ListView.ItemsSource = ImageCollection.ImageCollectionClass
于 2013-01-24T08:02:57.440 に答える