[英]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". 我将在几个不同的项目中使用FrameGrabber,所以我真的更愿意只说“导入FrameGrabber”。
My "FrameGrabber / CameraWindow" is defined like this: 我的“ 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
Thank you for the help! 感谢您的帮助!
You need to change the Root Namespace for your project or override it. 您需要为您的项目更改Root命名空间或对其进行覆盖。 When you wrap your class in a
Namespace
block (eg Namespace FrameGrabber
), the given namespace is relative to the root namespace for your project. 当您将类包装在
Namespace
块(例如Namespace FrameGrabber
)中时,给定的名称空间是相对于项目的根名称空间的。 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. 换句话说,如果您的根命名空间是
WindowsApplication1
,那么当您说Namespace FrameGrabber
,所有包含的类型实际上将在WindowsApplication1.FrameGrabber
命名空间中。
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: 如果要覆盖一段代码的根名称空间,则可以使用
Global
关键字,以便命名空间声明不是相对的,如下所示:
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. 像这样,在名称空间声明中使用
Global
关键字来覆盖根名称空间似乎是VB.NET的新增功能。 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 : 据我所知,基于MSDN文章中有关该信息的信息,Visual Studio 2012中已添加了对此信息的支持。您还可以在此MSDN文章中找到有关此信息:
The Global keyword can also be used in a Namespace statement.
Global关键字也可以在命名空间语句中使用。 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.
有关更多信息,请参见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. 要么这样做,要么提出更有利于VB.NET怪异性的命名约定。 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
. 例如,如果将项目的根名称空间用作公司名称,那么
MyCompany.FrameGrabber
当然比WindowsApplication1.FrameGrabber
更有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.