regex 如何将标签文本从函数类更新为Form1vb.net

wlwcrazw  于 2023-02-14  发布在  .NET
关注(0)|答案(2)|浏览(75)

我正在尝试从Form1重写代码,并学习如何在不同的classes中实现代码,如FunctionsProperties等...
现在我已经创建了一个Sub,它可以为我的视频提供YouTube标题,我知道我可以使用API顺便说一句,但由于我测试了很多,我的API限制将立即关闭。
问题是,即使我在函数Public Shared Property Frm As New Form1中声明,或者我为标签设置了一个invoke sub,我仍然会得到错误:
System.NullReferenceException:'对象引用未设置为对象的示例。'
下面是我在RegularExpressions.vb中的代码:

Imports System.Text.RegularExpressions
Imports Leaf.xNet

Public Class RegularExpressions
    Public Shared Property Frm As New Form1
    Friend Shared Mainform As Form1
    Public Shared Property txtTitle As Label

    Public Shared Sub getValues_Offline_Title()

        Using req As New HttpRequest()
            Dim Offline_Title As String
            Dim url As String
            Dim _videoID As String = Functions.VideoURL_link.ToString

            req.AddHeader("Host", "www.youtube.com")
            req.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
            req.AddHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
            req.AddHeader("Accept-Language", "en-US,en;q=0.9,cs;q=0.8,de;q=0.7,fr;q=0.6,hu;q=0.5")
            req.AddHeader("Accept-Encoding", "gzip, deflate")
            req.AddHeader("Referrer", $"https://www.youtube.com/watch?v={_videoID}")

            Dim strData As String = req.[Get](String.Concat("https://www.youtube.com/watch?v=", _videoID), Nothing).ToString()
            Dim url2 = strData.ToString
            url = strData.ToString().Split({"twoColumnWatchNextResults"":"}, StringSplitOptions.None)(1).Split("]"c)(0)
            Offline_Title = url.Split(New String() {"text"":"""}, StringSplitOptions.None)(1).Split(""""c)(0)
            'MessageBox.Show(Offline_Title)
            Frm.Invoke(Sub() txtTitle.Text = Offline_Title.ToString)
            'Mainform.lbl_Views_sent.Text = Offline_Title.ToString
        End Using
    End Sub

End Class

这个方法在Form1加载时被调用,并且有2个单选按钮。如果单选按钮1被选中,那么它应该显示离线视频标题,否则单选按钮2被选中,那么它应该显示实时视频标题。

Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
    If RadioButton1.Checked Then
         RegularExpressions.getValues_Offline_Title()
    Else
         RegularExpressions.getValues_Live_Title()
    End If
End Sub
zfycwa2u

zfycwa2u1#

您必须基于form1创建一个名为Frm的表单类,并将Form1类的现有示例分配给它。(如果解释不完善,请进行注解)
这是可行的:

Public Class RegularExpressions
            Public Shared Sub getValues_Offline_Title()
                Dim Frm As Form1 = Form1 'create and assign
                Using req As New HttpRequest()
                    Dim Offline_Title As String
                    Dim url As String
                    Dim _videoID As String = Functions.VideoURL_link.ToString
                    
                    req.AddHeader("Host", "www.youtube.com")
                    req.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
                    req.AddHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
                    req.AddHeader("Accept-Language", "en-US,en;q=0.9,cs;q=0.8,de;q=0.7,fr;q=0.6,hu;q=0.5")
                    req.AddHeader("Accept-Encoding", "gzip, deflate")
                    req.AddHeader("Referrer", $"https://www.youtube.com/watch?v={_videoID}")
        
                    Dim strData As String = req.[Get](String.Concat("https://www.youtube.com/watch?v=", _videoID), Nothing).ToString()
                    Dim url2 = strData.ToString
                    url = strData.ToString().Split({"twoColumnWatchNextResults"":"}, StringSplitOptions.None)(1).Split("]"c)(0)
                    Offline_Title = url.Split(New String() {"text"":"""}, StringSplitOptions.None)(1).Split(""""c)(0)
                    'MessageBox.Show(Offline_Title)
                    Frm.txtTitle.Text = Offline_Title.ToString
                    'Mainform.lbl_Views_sent.Text = Offline_Title.ToString
                End Using
            End Sub
        
        End Class
mqkwyuun

mqkwyuun2#

因为我的代码是一个大烂摊子,我选择重写它,我知道我会挣扎很多,但在此期间,我喜欢提高我的技能,学习更好的编码。所以解决方案是忘记class中的Sub,创建一个Function,其中Return是所需的标题,从正则表达式和Form1中,我只需调用txtTitle.Text = MyFunction.Function.ToString

相关问题