我的表单仅在我使用
时有效Imports WindowsApplication1.FrameGrabber
但不是我用的时候
Imports FrameGrabber
我将在几个不同的项目中使用FrameGrabber,所以我真的更喜欢只说“Imports 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
感谢您的帮助!
答案 0 :(得分:1)
您需要更改项目的根名称空间或覆盖它。当您将类包装在Namespace
块(例如Namespace FrameGrabber
)中时,给定的命名空间相对于项目的根命名空间。换句话说,如果您的根命名空间是WindowsApplication1
,那么当您说Namespace FrameGrabber
时,所有包含的类型实际上都在WindowsApplication1.FrameGrabber
命名空间中。
如果要覆盖一段代码的根名称空间,可以使用Global
关键字,以便命名空间声明不是相对的,如下所示:
Namespace Global.FrameGrabber
' ...
End Namespace
在名称空间声明中使用Global
关键字来覆盖根命名空间似乎是VB.NET的最新成员。据我所知,基于在the MSDN article中包含有关它的信息,Visual Studio 2012中添加了对该信息的支持。您还可以在this MSDN article中找到有关它的信息:
Global关键字也可以在Namespace语句中使用。这使您可以从项目的根命名空间中定义命名空间。有关详细信息,请参阅Visual Basic中命名空间中的“命名空间语句中的全局关键字”部分。
另一种选择是从项目属性中删除根命名空间,然后在项目中的每个代码文件上声明完整的命名空间。该设置可在项目设置设计器屏幕中找到:我的项目&gt; 申请&gt; 根名称空间。
或者提出一个更有利于VB.NET偏心的命名约定。例如,如果您为项目创建了公司名称的根命名空间,那么MyCompany.FrameGrabber
肯定比WindowsApplication1.FrameGrabber
更有意义。