簡體   English   中英

沒有項目名稱的VB.NET導入

[英]VB.NET Imports without project name

我的表格僅在使用時有效

Imports WindowsApplication1.FrameGrabber

但是當我使用

Imports FrameGrabber

我將在幾個不同的項目中使用FrameGrabber,所以我真的更願意只說“導入FrameGrabber”。

我的“ FrameGrabber / CameraWindow”的定義如下:

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

感謝您的幫助!

您需要為您的項目更改Root命名空間或對其進行覆蓋。 當您將類包裝在Namespace塊(例如Namespace FrameGrabber )中時,給定的名稱空間是相對於項目的根名稱空間的。 換句話說,如果您的根命名空間是WindowsApplication1 ,那么當您說Namespace FrameGrabber ,所有包含的類型實際上將在WindowsApplication1.FrameGrabber命名空間中。

如果要覆蓋一段代碼的根名稱空間,則可以使用Global關鍵字,以便命名空間聲明不是相對的,如下所示:

Namespace Global.FrameGrabber
    ' ...
End Namespace

像這樣,在名稱空間聲明中使用Global關鍵字來覆蓋根名稱空間似乎是VB.NET的新增功能。 據我所知,基於MSDN文章中有關該信息的信息,Visual Studio 2012中已添加了對此信息的支持。您還可以在此MSDN文章中找到有關信息:

Global關鍵字也可以在命名空間語句中使用。 這使您可以在項目的根名稱空間之外定義名稱空間。 有關更多信息,請參見Visual Basic中命名空間中的“命名空間語句中的全局關鍵字”部分。

另一個選擇是從項目屬性中刪除根名稱空間,然后在項目中的每個代碼文件上聲明完整的名稱空間。 該設置可以在項目設置設計器屏幕中找到: 我的項目 > 應用程序 > 根名稱空間

要么這樣做,要么提出更有利於VB.NET怪異性的命名約定。 例如,如果將項目的根名稱空間用作公司名稱,那么MyCompany.FrameGrabber當然比WindowsApplication1.FrameGrabber更有意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM