【问题标题】:VB.NET getting error: Class 'Application' must implement 'Sub InitializeComponent()'VB.NET 出现错误:类 'Application' 必须实现 'Sub InitializeComponent()'
【发布时间】: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


【解决方案1】:

我已经设法重现你的问题:

  1. 创建一个新的 WPF VB.NET 应用程序
  2. Application.xaml.vb 文件中创建自定义 Main,就像您的帖子一样
  3. 在项目属性中取消选中Enable Application Framework(启动对象自动成为我们的主子)
  4. 将 Application.xaml 文件的 Build Action 属性设置为 Page

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

最后一步:

在 Application.xaml 文件中删除

  StartupUri="MainWindow.xaml"

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

如果您想启动您自己的主窗口,您可以使用

更改运行行
 app.Run(New MainWindow())

【讨论】:

  • 再次感谢史蒂夫。你是对的,这样错误消失了,但 MainWindow 没有启动,所以应用程序刚刚启动,但什么也没有出现。我应该以某种方式手动运行主 xaml 吗?
  • app,Run(new MainWindow())
【解决方案2】:

如果您收到该错误,您可能还没有声明您的应用程序将从您的 Main() 方法开始。所以:

“转到您的项目设置并取消选中 Enable application framework 复选框(如果已选中)并选择 Sub Main 作为 Startup object ,然后将您的 main-method 添加到 Application.xaml.vb (加上记录器和其他东西) 。”

当您完成此操作后,VS 不会创建“自己的”Main 方法,而是调用您在 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

【讨论】:

  • 感谢您的回答 Romias,但我已经阅读了 msdn social 上的那篇帖子,并且我按照那里的建议做了(查看我的帖子,我提到了)
  • 关于您的编辑:也已经阅读了该链接,这就是我写“有人说某事有人说相反”的原因。在您发布的页面末尾,它声明:“..指向启动对象以'Sub Main'和'启用应用程序框架'。这意味着“勾选”MSDN对“取消勾选”的复选框。
  • 好的,如果您阅读并应用了这两种方法但都不起作用......不知道会是什么。
  • 还是感谢您的帮助...我当然会继续努力,找到解决方案后我会发布解决方案。