Search code examples
vb.netentry-point

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


I'm doing a VB.NET WPF application using VS2013, and I am just trying to find and use the right entry point.

I have read tons of answers on this, one saying something and the other saying the opposite. Mainly they say: the entry point of your project is the autogenerated main() you can find in your Application.g.vb. Yes, ok, very nice but...it is a generated file, not a good idea to modify it. So I searched the net on how to implement my own main() method, and the common answer I've found is:

  • Select Application.xaml and change its build action to "Page"
  • Create your own main method in Application.xaml.vb with this signature:

    _ Public Shared Sub Main() Dim app As Application = New Application() app.InitializeComponent() app.Run() End Sub

  • Go to your project properties, untick "Enable applpication framework" and select Sub Main as startup for your application.

And so I have done but I continuously get this error: Error 3 Class 'Application' must implement 'Sub InitializeComponent()' for interface 'System.Windows.Markup.IComponentConnector'.

this is the Application.g.i.vb file it generates:

    #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

so...as Sub InitializeComponent() is there, why the hell do I keep getting this error?

**EDIT:**My Application.xaml.vb is just that:

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

Solution

  • I have managed to reproduce you problem:

    1. Create a new WPF VB.NET application
    2. Create the custom Main in the Application.xaml.vb file as from your post
    3. In the project properties uncheck the Enable Application Framework (automatically the startup object becomes our Main sub)
    4. Set for the Application.xaml file the Build Action property to Page

    At this point, the line that call app.InitializeComponent() is underlined with a red squiggle to signify that the project cannot find the InitializeComponent method. If you try to compile at this point you get the mentioned error.

    Final step:

    In Application.xaml file remove the

      StartupUri="MainWindow.xaml"
    

    Now the squiggle disappears and the compilation ends correctly.

    If you want to start you own main window you could change the Run line with

     app.Run(New MainWindow())