VB.NET getting error: Class 'Application' must implement 'Sub InitializeComponent()'

VB.NET getting error: Class 'Application' must implement 'Sub InitializeComponent()'

我正在使用 VS2013 做一个 VB.NET WPF 应用程序,我只是想找到并使用正确的入口点。

我已经阅读了很多关于这个的答案,一个说了些什么,另一个说了相反的话。他们主要说:项目的入口点是自动生成的 main(),您可以在 Application.g.vb 中找到它。是的,好的,非常好,但是...它是一个生成的文件,修改它不是一个好主意。所以我在网上搜索了如何实现我自己的 main() 方法,我发现的常见答案是:

所以我已经完成了,但我不断收到此错误: 错误 3 Class '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() 方法启动。所以:

"Go to your project settings and untick the Enable application framework checkbox (if it's ticked) and select Sub Main as the Startup object , then add your main-method to Application.xaml.vb (plus the logger and other stuff)."

完成后,VS 将不会创建 "its own" 主方法,而是调用您在 Application.xaml 中定义的主方法。

取自:https://social.msdn.microsoft.com/Forums/vstudio/en-US/d7ef493f-27be-4f32-8ff4-a014078f572c/custom-systemwindowsapplication-in-vb-net?forum=wpf

还要检查这个:http://ugts.azurewebsites.net/data/UGTS/document/2/3/32.aspx

我成功重现了你的问题:

  1. 创建新的 WPFVB.NET 应用程序
  2. 根据您的 post
  3. Application.xaml.vb 文件中创建自定义 Main
  4. 在项目属性中取消选中 Enable Application Framework(自动启动对象成为我们的 Main sub)
  5. 将 Application.xaml 文件的 Build Action 属性 设置为 Page

此时,调用 app.InitializeComponent() 的行带有红色波浪线下划线,表示项目找不到 InitializeComponent 方法。如果此时尝试编译,则会出现上述错误。

最后一步:

在 Application.xaml 文件中删除

  StartupUri="MainWindow.xaml"

现在曲线消失,编译正确结束。

如果你想开始你自己的主要 window 你可以将 运行 行更改为

 app.Run(New MainWindow())