gpt4 book ai didi

没有项目名称的 VB.NET 导入

转载 作者:行者123 更新时间:2023-12-04 16:56:21 24 4
gpt4 key购买 nike

我的表单仅在我使用时有效

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

感谢您的帮助!

最佳答案

您需要更改项目的根命名空间或覆盖它。当你用 Namespace 包裹你的类(class)时块(例如 Namespace FrameGrabber ),给定的命名空间相对于您项目的根命名空间。换句话说,如果您的根命名空间是 WindowsApplication1 ,那么当你说 Namespace FrameGrabber ,所有包含的类型实际上都在 WindowsApplication1.FrameGrabber 中命名空间。

如果要覆盖一段代码的根 namespace ,可以使用 Global关键字,以便命名空间声明不是相对的,如下所示:

Namespace Global.FrameGrabber
' ...
End Namespace

使用 Global不过,在您的命名空间声明中使用关键字来覆盖根命名空间似乎是最近添加到 VB.NET 的。据我所知,基于 the MSDN article 中包含的有关它的信息,Visual Studio 2012 中添加了对此的支持。您还可以在 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.



另一种选择是从项目属性中删除根命名空间,然后在项目中的每个代码文件上声明完整的命名空间。该设置可以在项目设置设计器屏幕中找到: 我的项目 > 申请 > 根命名空间 .

或者提出一个更有助于 VB.NET 的怪癖的命名约定。例如,如果您将项目的根命名空间设为公司名称,则 MyCompany.FrameGrabber肯定比 WindowsApplication1.FrameGrabber 更有意义.

关于没有项目名称的 VB.NET 导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22229496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com