VB.NET에서 ExtendedWebBrowser를 초기화하고 C #에서 VB.Net으로의 구문 분석 문제

마르첼로

이전 답변에 따라 새 탭 WebBrowser 컨트롤에서 링크를 엽니 다 .

그리고 C # :
Extended WebBrowser Control for C #에 좋은 예제를 게시 한 Mauricio Rojas에게 감사드립니다 .

C # 용으로 제공된 코드를 VB.NET으로 변환하려고하는데 문제가 있습니다. 클래스
에서 DWebBrowserEvents2인터페이스 를 구현하려고 할 때 WebBrowserExtendedEventsVisual Studio에서 오류가 발생하는 이유를 이해할 수 없습니다 .

WebBrowserExtendedEvents 클래스는 인터페이스 DWebBrowserEvents2에 대해 Sub NewWindow2 (ByRef pDisp As Object, ByRef cancel As Boolean)를 구현해야합니다.

클래스와 인터페이스에서 해당 메서드를 올바르게 구현 한 것 같습니다.

참고 : jmcilhinney가 제안한 것처럼 질문과 관련이없는 코드의 일부를 제거하는 것에 대해 하지만 웹에서 ExtendedWebBrowserVB.Net 대한 명확한 예제를 찾지 못했기 때문에 이점을 위해 전체 코드를 남기기로 결정했습니다. 다른 것들의.

여기 VB.Net으로 번역 된 전체 코드

'First define a new EventArgs class to contain the newly exposed data
Public Class NewWindow2EventArgs
    Inherits CancelEventArgs

    Private _ppDisp As Object
    Public Property PPDisp As Object
        Get
            Return _ppDisp
        End Get
        Set(value As Object)
            _ppDisp = value
        End Set
    End Property

    Public Sub New(ByRef ppDisp As Object, ByRef cancel As Boolean)
        MyBase.New()
        Me.ppDisp = Me.ppDisp
        Me.Cancel = cancel
    End Sub
End Class

Public Class DocumentCompleteEventArgs
    Inherits EventArgs

    Private _ppDisp As Object
    Public Property PPDisp As Object
        Get
            Return _ppDisp
        End Get
        Set(value As Object)
            _ppDisp = value
        End Set
    End Property

    Private _url As Object
    Public Property Url As Object
        Get
            Return _url
        End Get
        Set(value As Object)
            _url = value
        End Set
    End Property

    Public Sub New(ByVal ppDisp As Object, ByVal url As Object)
        MyBase.New()
        Me.ppDisp = Me.ppDisp
        Me.url = Me.url
    End Sub
End Class

Public Class CommandStateChangeEventArgs
    Inherits EventArgs

    Private _command As Long
    Public Property Command As Long
        Get
            Return _command
        End Get
        Set(value As Long)
            _command = value
        End Set
    End Property

    Private _enable As Boolean
    Public Property Enable As Boolean
        Get
            Return _enable
        End Get
        Set(value As Boolean)
            _enable = value
        End Set
    End Property

    Public Sub New(ByVal command As Long, ByRef enable As Boolean)
        MyBase.New()
        Me.command = Me.command
        Me.enable = Me.enable
    End Sub

End Class

'Extend the WebBrowser control
Public Class ExtendedWebBrowser
    Inherits WebBrowser

    Private cookie As AxHost.ConnectionPointCookie

    Private Shadows events As WebBrowserExtendedEvents

    'This method will be called to give you a chance to create your own event sink
    Protected Overrides Sub CreateSink()
        'MAKE SURE TO CALL THE BASE or the normal events won't fire
        MyBase.CreateSink()
        Me.events = New WebBrowserExtendedEvents(Me)
        Me.cookie = New AxHost.ConnectionPointCookie(Me.ActiveXInstance, Me.events, GetType(DWebBrowserEvents2))
    End Sub

    Public ReadOnly Property Application As Object
        Get
            Dim axWebBrowser As IWebBrowser2 = CType(Me.ActiveXInstance, IWebBrowser2)
            If (Not (axWebBrowser) Is Nothing) Then
                Return axWebBrowser.Application
            Else
                Return Nothing
            End If

        End Get
    End Property

    Protected Overrides Sub DetachSink()
        If (Not (Me.cookie) Is Nothing) Then
            Me.cookie.Disconnect()
            Me.cookie = Nothing
        End If

        MyBase.DetachSink()
    End Sub

    'This new event will fire for the NewWindow2
    Public Event NewWindow2 As EventHandler(Of NewWindow2EventArgs)

    Protected Sub OnNewWindow2(ByRef ppDisp As Object, ByRef cancel As Boolean)
        'Dim h As EventHandler(Of NewWindow2EventArgs) = NewWindow2

        Dim args As NewWindow2EventArgs = New NewWindow2EventArgs(ppDisp, cancel)
        If Not IsNothing(NewWindow2Event) Then
            RaiseEvent NewWindow2(Me, args)
        End If

        'Pass the cancellation chosen back out to the events
        'Pass the ppDisp chosen back out to the events
        cancel = args.Cancel
        ppDisp = args.PPDisp
    End Sub

    'This new event will fire for the DocumentComplete
    Public Event DocumentComplete As EventHandler(Of DocumentCompleteEventArgs)

    Protected Sub OnDocumentComplete(ByVal ppDisp As Object, ByVal url As Object)
        'Dim h As EventHandler(Of DocumentCompleteEventArgs) = DocumentComplete
        Dim args As DocumentCompleteEventArgs = New DocumentCompleteEventArgs(ppDisp, url)
        If Not IsNothing(DocumentCompleteEvent) Then
            RaiseEvent DocumentComplete(Me, args)
        End If

        'Pass the ppDisp chosen back out to the events
        ppDisp = args.PPDisp
        'I think url is readonly
    End Sub

    'This new event will fire for the CommandStateChange
    Public Event CommandStateChange As EventHandler(Of CommandStateChangeEventArgs)

    Protected Sub OnCommandStateChange(ByVal command As Long, ByRef enable As Boolean)
        'Dim h As EventHandler(Of CommandStateChangeEventArgs) = CommandStateChange
        Dim args As CommandStateChangeEventArgs = New CommandStateChangeEventArgs(command, enable)
        If Not IsNothing(CommandStateChangeEvent) Then
            RaiseEvent CommandStateChange(Me, args)
        End If

    End Sub

    'This class will capture events from the WebBrowser
    Public Class WebBrowserExtendedEvents
        Inherits System.Runtime.InteropServices.StandardOleMarshalObject

        '******************* HERE THE ERROR ********************
        Implements DWebBrowserEvents2
        '*******************************************************

        Private _Browser As ExtendedWebBrowser

        Public Sub New(ByVal browser As ExtendedWebBrowser)
            MyBase.New()
            Me._Browser = browser
        End Sub

        'Implement whichever events you wish
        Public Sub NewWindow2(ByRef pDisp As Object, ByRef cancel As Boolean)
            Me._Browser.OnNewWindow2(pDisp, cancel)
        End Sub

        'Implement whichever events you wish
        Public Sub DocumentComplete(ByVal pDisp As Object, ByRef url As Object)
            Me._Browser.OnDocumentComplete(pDisp, url)
        End Sub

        'Implement whichever events you wish
        Public Sub CommandStateChange(ByVal command As Long, ByVal enable As Boolean)
            Me._Browser.OnCommandStateChange(command, enable)
        End Sub

    End Class

    <ComImport(), _
     Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), _
     InterfaceType(ComInterfaceType.InterfaceIsIDispatch), _
     TypeLibType(TypeLibTypeFlags.FHidden)> _
    Public Interface DWebBrowserEvents2

        <DispId(105)> _
        Sub CommandStateChange(ByVal command As Long, ByVal enable As Boolean)

        <DispId(259)> _
        Sub DocumentComplete(ByVal pDisp As Object, ByRef URL As Object)

        <DispId(251)> _
        Sub NewWindow2(ByRef pDisp As Object, ByRef cancel As Boolean)

    End Interface

    <ComImport(), _
     Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E"), _
     TypeLibType((TypeLibTypeFlags.FOleAutomation _
                Or (TypeLibTypeFlags.FDual Or TypeLibTypeFlags.FHidden)))> _
    Public Interface IWebBrowser2

        <DispId(100)> _
        Sub GoBack()

        <DispId(101)> _
        Sub GoForward()

        <DispId(102)> _
        Sub GoHome()

        <DispId(103)> _
        Sub GoSearch()

        <DispId(104)> _
        Sub Navigate(ByVal Url As String, ByRef flags As Object, ByRef targetFrameName As Object, ByRef postData As Object, ByRef headers As Object)

        <DispId(-550)> _
        Sub Refresh()

        <DispId(105)> _
        Sub Refresh2(ByRef level As Object)

        <DispId(106)> _
        Sub [Stop]()

        <DispId(200)> _
        ReadOnly Property Application As Object

        <DispId(201)> _
        ReadOnly Property Parent As Object

        <DispId(202)> _
        ReadOnly Property Container As Object

        <DispId(203)> _
        ReadOnly Property Document As Object

        <DispId(204)> _
        ReadOnly Property TopLevelContainer As Boolean

        <DispId(205)> _
        ReadOnly Property Type As String

        <DispId(206)> _
        Property Left As Integer

        <DispId(207)> _
        Property Top As Integer

        <DispId(208)> _
        Property Width As Integer

        <DispId(209)> _
        Property Height As Integer

        <DispId(210)> _
        ReadOnly Property LocationName As String

        <DispId(211)> _
        ReadOnly Property LocationURL As String

        <DispId(212)> _
        ReadOnly Property Busy As Boolean

        <DispId(300)> _
        Sub Quit()

        <DispId(301)> _
        Sub ClientToWindow(ByRef pcx As Integer, ByRef pcy As Integer)

        <DispId(302)> _
        Sub PutProperty(ByVal _property As String, ByVal vtValue As Object)

        <DispId(303)> _
        Function GetProperty(ByVal _property As String) As Object

        <DispId(0)> _
        ReadOnly Property Name As String

        <DispId(-515)> _
        ReadOnly Property HWND As Integer

        <DispId(400)> _
        ReadOnly Property FullName As String

        <DispId(401)> _
        ReadOnly Property Path As String

        <DispId(402)> _
        Property Visible As Boolean

        <DispId(403)> _
        Property StatusBar As Boolean

        <DispId(404)> _
        Property StatusText As String

        <DispId(405)> _
        Property ToolBar As Integer

        <DispId(406)> _
        Property MenuBar As Boolean

        <DispId(407)> _
        Property FullScreen As Boolean

        <DispId(500)> _
        Sub Navigate2(ByRef URL As Object, ByRef flags As Object, ByRef targetFrameName As Object, ByRef postData As Object, ByRef headers As Object)

        <DispId(503)> _
        Sub ShowBrowserBar(ByRef pvaClsid As Object, ByRef pvarShow As Object, ByRef pvarSize As Object)

        <DispId(-525)> _
        ReadOnly Property ReadyState As WebBrowserReadyState

        <DispId(550)> _
        Property Offline As Boolean

        <DispId(551)> _
        Property Silent As Boolean

        <DispId(552)> _
        Property RegisterAsBrowser As Boolean

        <DispId(553)> _
        Property RegisterAsDropTarget As Boolean

        <DispId(554)> _
        Property TheaterMode As Boolean

        <DispId(555)> _
        Property AddressBar As Boolean

        <DispId(556)> _
        Property Resizable As Boolean
    End Interface
End Class
지미

VB.Net에서는 해당 인터페이스 멤버를 구현하는 멤버에 Implements 키워드 를 추가해야합니다.

귀하의 WebBrowserExtendedEvents클래스 구현을 정의하지만, 회원은 모든 사용자들은, 검색어 구현합니다 누락되었습니다. 예를 들면 :

Public Sub NewWindow2(ByRef pDisp As Object, ByRef cancel As Boolean)
    Me._Browser.OnNewWindow2(pDisp, cancel)
End Sub

변경해야합니다 (두 줄로 작성할 수 없으므로 여기에 한 줄로 유지).

Public Sub NewWindow2(ByRef pDisp As Object, ByRef cancel As Boolean) Implements DWebBrowserEvents2.NewWindow2
    Me._Browser.OnNewWindow2(pDisp, cancel)
End Sub

또한 C # 샘플 코드에서 찾은 MarshalAs 특성 을 유지하는 것이 좋습니다 .
예를 들어 DWebBrowserEvents2인터페이스 정의에서 다음을 변경합니다.

<ComImport(), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), [...]
Public Interface DWebBrowserEvents2
    '[...]
    <DispId(251)> _
    Sub NewWindow2(ByRef pDisp As Object, ByRef cancel As Boolean)
End Interface

에:

<ComImport(), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), [...]
Public Interface DWebBrowserEvents2
    '[...]
    <DispId(251)>
    Sub NewWindow2(<MarshalAs(UnmanagedType.IDispatch)> ByRef pDisp As Object, ByRef cancel As Boolean)
End Interface

클래스가 구현해야하는 알림을 받으면 밑줄이 그어진 멤버를 선택하고을 눌러 ALT+ENTERVisual Studio가 인터페이스를 구현하도록 할 수 있습니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

VB .Net에서 XML 구문 분석

분류에서Dev

VB.NET에서 JSON 구문 분석

분류에서Dev

보고서에서 vb.net regex 구문 분석 단락

분류에서Dev

C #에서 VB.Net 코드베이스 구문 분석

분류에서Dev

VB.NET에서 날짜를 iso로 구문 분석하는 방법

분류에서Dev

vb.net에서 문자열 구문 분석

분류에서Dev

C #에서 VB.Net으로의 변환 문제

분류에서Dev

C #에서 VB.Net으로의 변환 문제

분류에서Dev

XML이 아닌 Json에서 구문 분석. VB.Net

분류에서Dev

VB.net에서 JSON을 구문 분석하는 데 머리를 쓸 수 없습니다.

분류에서Dev

vb.net에서 문자열 분할 (구분 기호 없음)

분류에서Dev

C ++ / cli에서 vb.net으로 배열 참조를 다시 전달하는 데 문제가 있습니다.

분류에서Dev

vb.net에서 문자열 분할

분류에서Dev

C # .net에서 XML 구문 분석

분류에서Dev

VB6에서 .NET으로 .NET 개체 전달 문제

분류에서Dev

C ++를 VB.Net 문제로 변환

분류에서Dev

VB.net에서 C # 문자 인코딩 (1251)으로 변환

분류에서Dev

VB.Net의 JSON 개체 내의 JSON 배열 내에서 JSON 개체 구문 분석

분류에서Dev

VB.Net에서 '-'로 구분 된 문자열에서 값을 추출하는 방법

분류에서Dev

VB.NET에서 변수를 곱할 때 구문 오류

분류에서Dev

vb.net에서 MS Access를 사용하여 삽입시 구문 오류

분류에서Dev

VB DOTNET으로 HTML 구문 분석

분류에서Dev

동적 문자열 Vb.net에서 고정 길이의 문자열 분할

분류에서Dev

C #에서 DateTime 문제 구문 분석

분류에서Dev

VB.Net의 특정 문자 수를 기준으로 문자열 분할

분류에서Dev

VB.NET의 문자열에서 부분 문자열 텍스트 얻기

분류에서Dev

C ++에서 구분 기호 뒤의 부분을 구문 분석하는 방법에 대한 문제 해결?

분류에서Dev

C #을 VB.net으로 변환-구문 바로 가기

분류에서Dev

VB.Net의 구문 분석 도움말

Related 관련 기사

  1. 1

    VB .Net에서 XML 구문 분석

  2. 2

    VB.NET에서 JSON 구문 분석

  3. 3

    보고서에서 vb.net regex 구문 분석 단락

  4. 4

    C #에서 VB.Net 코드베이스 구문 분석

  5. 5

    VB.NET에서 날짜를 iso로 구문 분석하는 방법

  6. 6

    vb.net에서 문자열 구문 분석

  7. 7

    C #에서 VB.Net으로의 변환 문제

  8. 8

    C #에서 VB.Net으로의 변환 문제

  9. 9

    XML이 아닌 Json에서 구문 분석. VB.Net

  10. 10

    VB.net에서 JSON을 구문 분석하는 데 머리를 쓸 수 없습니다.

  11. 11

    vb.net에서 문자열 분할 (구분 기호 없음)

  12. 12

    C ++ / cli에서 vb.net으로 배열 참조를 다시 전달하는 데 문제가 있습니다.

  13. 13

    vb.net에서 문자열 분할

  14. 14

    C # .net에서 XML 구문 분석

  15. 15

    VB6에서 .NET으로 .NET 개체 전달 문제

  16. 16

    C ++를 VB.Net 문제로 변환

  17. 17

    VB.net에서 C # 문자 인코딩 (1251)으로 변환

  18. 18

    VB.Net의 JSON 개체 내의 JSON 배열 내에서 JSON 개체 구문 분석

  19. 19

    VB.Net에서 '-'로 구분 된 문자열에서 값을 추출하는 방법

  20. 20

    VB.NET에서 변수를 곱할 때 구문 오류

  21. 21

    vb.net에서 MS Access를 사용하여 삽입시 구문 오류

  22. 22

    VB DOTNET으로 HTML 구문 분석

  23. 23

    동적 문자열 Vb.net에서 고정 길이의 문자열 분할

  24. 24

    C #에서 DateTime 문제 구문 분석

  25. 25

    VB.Net의 특정 문자 수를 기준으로 문자열 분할

  26. 26

    VB.NET의 문자열에서 부분 문자열 텍스트 얻기

  27. 27

    C ++에서 구분 기호 뒤의 부분을 구문 분석하는 방법에 대한 문제 해결?

  28. 28

    C #을 VB.net으로 변환-구문 바로 가기

  29. 29

    VB.Net의 구문 분석 도움말

뜨겁다태그

보관