无法在vb.net中投射自定义listviewitem类

时间:2011-04-29 03:10:18

标签: vb.net listview inheritance casting listviewitem

这是我之前提出的另一个问题的后续问题。我以为我有我需要的一切,但我遇到了另一个问题。我正在尝试使用自定义listviewitem类,将附加信息附加到lisview项目。这是班级:

Public Class albumListViewItem
    Inherits ListViewItem
    Public hash As String
    Public id As Integer
    Public provider As String
    Public providerID As String
    Public providerURL As String
    Public providerArtistID As String
    Public albumName As String
    Public albumType As String
    Public numTracks As Integer
    Public imageURLs() As String
    Public genres() As String
    Public styles() As String
    Public label As String
    Public year As String
    Public country As String
    Public rating As String
    Public editorsPick As Boolean
    Public sampleStreamURL As String
    Public providerReview As String
End Class

当我尝试将listviewitem强制转换为我的自定义类时:

Dim albumItem As albumListViewItem = CType(lsvHidden.items.item(0), albumListViewItem)

我收到以下错误,“无法将'System.Windows.Forms.ListViewItem'类型的对象强制转换为'AudioMatic.albumListViewItem'。”

我在这里缺少什么?

3 个答案:

答案 0 :(得分:3)

从您之前的问题和此问题开始,我认为更适合您的问题的方法是使用常规ListViewItem并将附件信息存储在ListViewItem.Tag

你可以做到

Dim listViewItem As New ListViewItem("SomeText")
Dim albumInfo As New albumListViewItem()

albumInfo.albumName = "SomeAlbum"
...
listViewItem.Tag = albumInfo

listView1.Items.Add(listViewItem)

然后像这样检索它

Dim selectedItem As ListViewItem = listView1.SelectedItems(0).Item
Dim alubmInfo As albumListViewItem = TryCast(selectedItem.Tag, alubmListViewItem)

Dim albumName as String = albumInfo.albumName

看看这个解决方案是否适合您。

答案 1 :(得分:0)

如果您单步执行代码并观察变量“lsvHidden.items.item(0)”,您应该能够首先判断它是否实际上是“albumListViewItem”类型或其他类型。你确定它是第一个被添加到列表中的albumListViewItem吗?

你正在做的一些替代方案;  1.您可以实现一个对象并将其存储在ListViewItem的标记中。  2.以下文章似乎描述了另一种向列表视图添加列的方法,以允许在列表视图本身上存储额外信息; http://www.codeproject.com/KB/list/ListViewExtendedItem.aspx

我可以理解你的情况,因为我希望这样做。我也可以看到这样做的优点和原因。不确定代码项目示例是否适合您的需求,因此您需要查看概念。

答案 2 :(得分:0)

工作代码:

Public Class albumListViewItem
    Inherits ListViewItem
    Public hash As String
    Public id As Integer
    Public provider As String
    Public providerID As String
    Public providerURL As String
    Public providerArtistID As String
    Public albumName As String
    Public albumType As String
    Public numTracks As Integer
    Public imageURLs() As String
    Public genres() As String
    Public styles() As String
    Public label As String
    Public year As String
    Public country As String
    Public rating As String
    Public editorsPick As Boolean
    Public sampleStreamURL As String
    Public providerReview As String
End Class

使用listviewitem.tag存储信息:

Dim listViewItem As New ListViewItem("SomeText")
Dim albumItem As New albumListViewItem

albumItem.albumName = "Test Album"
albumItem.id = "testid"
albumItem.Text = albumItem.albumName
albumItem.year = "2011"
albumItem.numTracks = 10
'....
listViewItem.Tag = albumItem
'add viewable items to listview
albumItem.SubItems.Add(albumItem.year)
albumItem.SubItems.Add(albumItem.numTracks)
'....
ListView1.Items.Add(albumItem)

阅读以前存储的信息:

Dim albumInfo As albumListViewItem = CType(ListView1.SelectedItems(0), albumListViewItem)
Dim id as string = alumInfo.id