【发布时间】:2016-03-06 12:53:30
【问题描述】:
我正在使用 VS2013 做一个 VB.NET WPF 应用程序,我只是想找到并使用正确的入口点。
我已经阅读了大量关于此的答案,一个说什么,另一个说相反。他们主要说:您的项目的入口点是您可以在 Application.g.vb 中找到的自动生成的 main()。是的,好的,非常好,但是...它是一个生成的文件,修改它不是一个好主意。于是我在网上搜索了如何实现自己的 main() 方法,我找到的常见答案是:
- 选择 Application.xaml 并将其构建操作更改为“Page”
-
使用此签名在 Application.xaml.vb 中创建您自己的 main 方法:
_ 公共共享子 Main() 将应用程序调暗为应用程序 = 新应用程序() app.InitializeComponent() 应用程序运行() 结束子
转到您的项目属性,取消选中“启用应用程序框架”并选择 Sub Main 作为您应用程序的启动。
所以我已经完成了,但我不断收到此错误: 错误 3 类“Application”必须为接口“System.Windows.Markup.IComponentConnector”实现“Sub InitializeComponent()”。
这是它生成的 Application.g.i.vb 文件:
#ExternalChecksum("..\..\Application.xaml","{406ea660-64cf-4c82-b6f0-42d48172a799}","DB788882721B2B27C90579D5FE2A0418")
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:4.0.30319.42000
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict Off
Option Explicit On
Imports System
Imports System.Diagnostics
Imports System.Windows
Imports System.Windows.Automation
Imports System.Windows.Controls
Imports System.Windows.Controls.Primitives
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Ink
Imports System.Windows.Input
Imports System.Windows.Markup
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Effects
Imports System.Windows.Media.Imaging
Imports System.Windows.Media.Media3D
Imports System.Windows.Media.TextFormatting
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Windows.Shell
'''<summary>
'''Application
'''</summary>
<Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Public Class Application
Inherits System.Windows.Application
Implements System.Windows.Markup.IComponentConnector
Private _contentLoaded As Boolean
'''<summary>
'''InitializeComponent
'''</summary>
<System.Diagnostics.DebuggerNonUserCodeAttribute(), _
System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")> _
Public Sub InitializeComponent()
#ExternalSource("..\..\Application.xaml",4)
Me.StartupUri = New System.Uri("MainWindow.xaml", System.UriKind.Relative)
#End ExternalSource
If _contentLoaded Then
Return
End If
_contentLoaded = True
Dim resourceLocater As System.Uri = New System.Uri("/FatLink;component/application.xaml", System.UriKind.Relative)
#ExternalSource("..\..\Application.xaml",1)
System.Windows.Application.LoadComponent(Me, resourceLocater)
#End ExternalSource
End Sub
<System.Diagnostics.DebuggerNonUserCodeAttribute(), _
System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0"), _
System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never), _
System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes"), _
System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity"), _
System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")> _
Sub System_Windows_Markup_IComponentConnector_Connect(ByVal connectionId As Integer, ByVal target As Object) Implements System.Windows.Markup.IComponentConnector.Connect
Me._contentLoaded = True
End Sub
End Class
所以...既然有 Sub InitializeComponent(),为什么我会一直收到这个错误?
**编辑:**我的 Application.xaml.vb 就是这样:
Partial Public Class Application
<System.STAThreadAttribute(), _
System.Diagnostics.DebuggerNonUserCodeAttribute(), _
System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")> _
Public Shared Sub Main()
Dim app As Application = New Application()
app.InitializeComponent()
app.Run()
End Sub
End Class
【问题讨论】:
-
你的整个代码包含Main函数是?
-
我刚刚将 Sub Main 添加到类应用程序中,仍然没有在该文件中执行任何其他操作。顺便说一句,我已经编辑了帖子以添加 while 文件。
-
部分公共类申请
-
不幸的是我已经尝试过了,但它不起作用。顺便说一句,编辑我的帖子以添加“部分公开”是有道理的。
-
如果您想发牢骚、抱怨和咆哮,请在您自己的网站上写一篇博文。这里不是合适的地方。
标签: vb.net entry-point