简体   繁体   中英

VB.NET Imports without project name

My form only works when I use

Imports WindowsApplication1.FrameGrabber

but not when I use

Imports FrameGrabber

I will be using the FrameGrabber in several different projects, so I would really prefer having only to say "Imports FrameGrabber".

My "FrameGrabber / CameraWindow" is defined like this:

Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms
Imports System.Threading

Namespace FrameGrabber
    ''' <summary>
    ''' Summary description for CameraWindow.
    ''' </summary>
    Public Class CameraWindow
        Inherits System.Windows.Forms.Control
        Private m_camera As Camera = Nothing
        Private m_autosize As Boolean = False
        Private needSizeUpdate As Boolean = False
        Private firstFrame As Boolean = True

        ' AutoSize property
        <DefaultValue(False)> _
        Public Overrides Property AutoSize() As Boolean
            Get
                Return m_autosize
            End Get
            Set(value As Boolean)
                m_autosize = value
                UpdatePosition()
            End Set
        End Property

        ' Camera property
        <Browsable(False)> _
        Public Property Camera() As Camera
            Get
                Return m_camera
            End Get
            Set(value As Camera)
                ' lock
                Monitor.Enter(Me)

                ' detach event
                If m_camera IsNot Nothing Then
                    RemoveHandler m_camera.NewFrame, AddressOf Me.pCameraWindow_NewFrame
                End If

                m_camera = value
                needSizeUpdate = True
                firstFrame = True

                ' atach event
                If m_camera IsNot Nothing Then
                    AddHandler m_camera.NewFrame, AddressOf Me.pCameraWindow_NewFrame
                End If

                ' unlock
                Monitor.[Exit](Me)
            End Set
        End Property

        ' Constructor
        Public Sub New()
            InitializeComponent()

            SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
        End Sub

#Region "Windows Form Designer generated code"
        Private Sub InitializeComponent()
            Me.SuspendLayout()
            Me.ResumeLayout(False)

        End Sub
#End Region

        ' Paint control
        Protected Overrides Sub OnPaint(pe As PaintEventArgs)
            If (needSizeUpdate) OrElse (firstFrame) Then
                UpdatePosition()
                needSizeUpdate = False
            End If

            ' lock
            Monitor.Enter(Me)

            Dim g As Graphics = pe.Graphics
            Dim rc As Rectangle = Me.ClientRectangle

            If m_camera IsNot Nothing Then
                Try
                    m_camera.Lock()

                    ' draw frame
                    If m_camera.LastFrame IsNot Nothing Then
                        g.DrawImage(m_camera.LastFrame, rc.X + 1, rc.Y + 1, rc.Width - 2, rc.Height - 2)
                        firstFrame = False
                    Else
                        ' Create font and brush
                        Dim drawFont As New Font("Arial", 12)
                        Dim drawBrush As New SolidBrush(Color.White)

                        g.DrawString("Connecting ...", drawFont, drawBrush, New System.Drawing.PointF(5, 5))

                        drawBrush.Dispose()
                        drawFont.Dispose()
                    End If
                Catch generatedExceptionName As Exception
                Finally
                    m_camera.Unlock()
                End Try
            End If

            ' unlock
            Monitor.[Exit](Me)

            MyBase.OnPaint(pe)
        End Sub
        Public Function getImage() As Image

            If Not m_camera Is Nothing Then
                If Not m_camera.LastFrame Is Nothing Then
                    Return m_camera.LastFrame
                End If
            End If

            Return Nothing

        End Function
        ' Update position and size of the control
        Public Sub UpdatePosition()
            ' lock
            Monitor.Enter(Me)

            If (m_autosize) AndAlso (Me.Parent IsNot Nothing) Then
                Dim rc As Rectangle = Me.Parent.ClientRectangle
                Dim width As Integer = 320
                Dim height As Integer = 240

                If m_camera IsNot Nothing Then
                    m_camera.Lock()

                    ' get frame dimension
                    If m_camera.LastFrame IsNot Nothing Then
                        width = m_camera.LastFrame.Width
                        height = m_camera.LastFrame.Height
                    End If
                    m_camera.Unlock()
                End If

                '
                Me.SuspendLayout()
                Me.Location = New Point((rc.Width - width - 2) \ 2, (rc.Height - height - 2) \ 2)
                Me.Size = New Size(width + 2, height + 2)

                Me.ResumeLayout()
            End If
            ' unlock
            Monitor.[Exit](Me)
        End Sub

        ' On new frame ready
        Private Sub pCameraWindow_NewFrame(sender As Object, e As System.EventArgs)
            Invalidate()
        End Sub

    End Class
End Namespace

Thank you for the help!

You need to change the Root Namespace for your project or override it. When you wrap your class in a Namespace block (eg Namespace FrameGrabber ), the given namespace is relative to the root namespace for your project. In other words, if your root namespace is WindowsApplication1 , then when you say Namespace FrameGrabber , all the enclosed types will actually be in the WindowsApplication1.FrameGrabber namespace.

If you want to override the root name space for one section of code, you can use the Global keyword so that the namespace declaration is not relative, like this:

Namespace Global.FrameGrabber
    ' ...
End Namespace

Using the Global keyword in your namespace declaration, like that, to override the root namespace seems to be a recent addition to VB.NET, though. From what I can tell, based on the inclusion of the information about it in the MSDN article , support for that was added in Visual Studio 2012. You can also find information on it in this MSDN article :

The Global keyword can also be used in a Namespace statement. This lets you define a namespace out of the root namespace of your project. For more information, see the "Global Keyword in Namespace Statements" section in Namespaces in Visual Basic.

Another option is to remove the root namespace from your project properties and then declare the full namespace on each and every code file in your project. The setting can be found in the project settings designer screen: My Project > Application > Root namespace .

Either that or come up with a naming convention that is more conducive to VB.NET's eccentricity. For instance, if you made the root namespace for your project your company name, then MyCompany.FrameGrabber would certainly make more sense than WindowsApplication1.FrameGrabber .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM