dotnet / dotnet/wpf

WPF App InitializeComponent is missing

Open
#2,357 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

I am running VS 2019 16.4.1 on net core 3.1. I select create new project
"WPF App (Net Core" and the project is generated. Then I want my own startup
object, I select create new class "EntryPoint" and place the code below in it. Then I select
project entry point "EntryPoint" and compile. I get the error:

'App' does not contain a definition for 'InitializeComponent' and no accessible extension method 'InitializeComponent' accepting a first argument of type 'App' could be found (are you missing a using directive or an assembly reference?)

I looked at App.g.cs and even though I replaced the startup object with mine I still see a main being generated. How do I get InitializeComponent to be generated?

**App.g.cs**

` ///
/// Application Entry Point.
///
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.8.1.0")]
public static void Main() {
Gt.ServerConnect.App app = new Gt.ServerConnect.App();
app.Run();
}
`

**EntryPoint**

`using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace Gt.ServerConnect
{
public class EntryPoint
{
public static string Name
{
get { return "Gt.Lite"; }
}

// All WPF applications should execute on a single-threaded apartment (STA) thread
[STAThread]
public static void Main ( string [] args )
{

var app = new App ();
if ( app == null )
throw new NullReferenceException ( "Failed to create app" );

app.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler ( Current_DispatcherUnhandledException );
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler ( CurrentDomain_UnhandledException );

try
{
app.InitializeComponent ();

app.MainWindow = new MainWindow ();

app.Run ();
}
catch ( Exception ex )
{
string msg = ex.Message;
Debug.WriteLine ( "Exception: " + msg );

}

Process.GetCurrentProcess ().Kill ();
}

#region Internal Exception Handlers

static object _classLock = new object ();
static object ClassLock
{
get { return _classLock; }
}

static bool _closing = false;
static bool Closing
{
get
{
bool closing = false;

lock ( ClassLock )
{
closing = _closing;
}

return closing;
}

set
{
lock ( ClassLock )
{
_closing = value;
}
}
}

private static void Current_DispatcherUnhandledException ( object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e )
{
e.Handled = true;

var ex = e.Exception;

if ( ex.InnerException != null )
ex = ex.InnerException;

if ( Closing )
return;

Application.Current.Shutdown ();
}

public static void CurrentDomain_UnhandledException ( object sender, UnhandledExceptionEventArgs e )
{
var ex = e.ExceptionObject as Exception;

if ( ex.InnerException != null )
ex = ex.InnerException;

Application.Current.Shutdown ();
}

#endregion
}

}
`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.