【问题标题】:How to Drag & Drop from Spotify to Winforms app如何从 Spotify 拖放到 Winforms 应用程序
【发布时间】:2019-06-02 22:16:34
【问题描述】:

如果有人将 Spotify 曲目从 spotify 桌面应用拖放到 Excel,Excel 会显示歌曲的艺术家和标题。

我有一个 winforms 应用程序,我想在其中做同样的事情。 如果我拖放到这样的列表框......

Private Sub ListBox1_DragEnter(sender As Object, e As DragEventArgs) Handles ListBox1.DragEnter
    ListBox1.Items.Add(e.Data.GetData(DataFormats.Text))
End sub

.... 它所做的只是显示 Spotify 轨道 ID。 由于 Excel 不是为读取 Spotify 网址而设计的,因此数据必须在拖放中。但无论我选择什么数据格式,我都只会得到 ID。

【问题讨论】:

  • 试试this SO question中的示例项目。该项目是一个测试,用于从 WebPages(图像、文本、链接等)中拖动复合元素,以查看 DataFormats WinForms 控件可以检测和处理什么。它是用C# 编写的,但您只需要运行它并在接受Drop 的控件中放置一些东西,看看它会产生什么。大多数时候,MemoryStream 你必须阅读标准方式。

标签: vb.net url drag-and-drop spotify


【解决方案1】:

如果你拖动轨道并将其粘贴到文本编辑器中,你会看到它是一个 uri

所以在 VB.NET DragEventArgs 中包含 URI

所以我们需要知道这里的 Spotify URI 是什么

所以从 Spotify 帖子中引用您正在处理 2 种类型的 URI

Spotify URI Codes

您需要做的是将自己注册为开发人员获取令牌(您作为开发人员的密钥)以使用他们的服务

使用该令牌向他们授权自己 您可以使用他们的 Rest API 并为他们提供该 URI 以获取您希望在 json 中拥有的数据

这是 Rest API 的文档 Spotify Rest API Documentation

如果该服务需要您花钱购买,您可以采取另一种方法

使用 String.Split(":") 或 Regex 两种方式都可以将 DesktopURI 转换为 Web URI

从而创建您的自定义 web uri,然后将该 web uri 粘贴到爬虫并获取您的数据

当然,您可以在执行额外步骤之前使用 .NET URI 构建器类对其进行验证

Spotify 中的每个平台都有 3 种类型的 uri,因此请确保将其分类为 Track、Album、PlayList,因为您的爬虫将获取的内容不同

我认为他们的 Rest API 是免费服务,但请确保您不违反他们的 SLA Spotify SLA

有趣的是我告诉你,当我的第二个解决方案建议没有遵循他们的 SLA 时

【讨论】:

    【解决方案2】:
    Private Sub lstQueue_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles lstQueue.MouseDown
    
            If (e.Button = MouseButtons.Right) Then
    
                'clear previous selection
                lstQueue.SelectedItems.Clear()
    
                'highlight selected item
                lstQueue.SelectedIndex = lstQueue.IndexFromPoint(e.X, e.Y)
    
                'initiate movedown event for drag/drop
                Dim ix As Integer = lstQueue.IndexFromPoint(e.Location)
                If ix <> -1 Then
                    lstQueue.DoDragDrop(ix.ToString, DragDropEffects.Move)
                End If
    
            End If
    
        End Sub
    
        Private Sub lstQueue_DragEnter(sender As Object, e As DragEventArgs) Handles lstQueue.DragEnter
    
            'make label visile for drag/drop
            lblMoveLine.Visible = True
    
        End Sub
    
        Private Sub lstQueue_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles lstQueue.DragOver
    
            'allow drag over for drag/drop
            If e.Data.GetDataPresent(DataFormats.Text) Then
                e.Effect = DragDropEffects.Move
                Dim index = lstQueue.IndexFromPoint(lstQueue.PointToClient(New Point(e.X, e.Y)))
                lblMoveLine.Top = (index - lstQueue.TopIndex) * lstQueue.ItemHeight
            End If
    
        End Sub
    
        Private Sub lstQueue_DragLeave(sender As Object, e As System.EventArgs) Handles lstQueue.DragLeave
    
            'hide label for drag/drop
            lblMoveLine.Visible = False
    
        End Sub
    
        Private Sub lstQueue_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles lstQueue.DragDrop
    
            'remove item and add in new location
            If e.Data.GetDataPresent(DataFormats.Text) Then
                Dim dix As Integer = CInt(e.Data.GetData(DataFormats.Text))
                Dim ix As Integer = lstQueue.IndexFromPoint(lstQueue.PointToClient(New Point(e.X, e.Y)))
                If ix <> -1 Then
                    Dim obj As Object = lstQueue.Items(dix)
                    lstQueue.Items.Remove(obj)
                    lstQueue.Items.Insert(ix, obj)
                    lstQueue.SelectedIndex = ix
                End If
            End If
    
            lblMoveLine.Visible = False
    
        End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 2014-12-25
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      相关资源
      最近更新 更多