dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.1k stars 1.17k forks source link

WPF App InitializeComponent is missing #2357

Open dgxhubbard opened 4 years ago

dgxhubbard commented 4 years ago

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

/// <summary> /// Application Entry Point. /// </summary> [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
}

} `

dgxhubbard commented 4 years ago

Also I removed StartupUri from App.xaml.

lindexi commented 4 years ago

Could you format the code?

lindexi commented 4 years ago

Do you forget add this code in your csproj file?

    <ApplicationDefinition Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </ApplicationDefinition>

And the sdk will auto add this code.

dgxhubbard commented 4 years ago

I did not add that. Shouldn't that code be added by vs when the project is created?

rladuca commented 4 years ago

@dgxhubbard The props in WindowsDesktop picks up App.xaml or Application.xaml as the default ApplicationDefinition when it exists in the project directory.

https://github.com/dotnet/wpf/blob/165948b449e9de9fbba9843c2695f32a3212158f/packaging/Microsoft.NET.Sdk.WindowsDesktop/targets/Microsoft.NET.Sdk.WindowsDesktop.props#L35-L51

If you're changing that, you'll need to specify your own ApplicationDefinition as @lindexi said. Generally, if you can't build with dotnet build you're not going to be able to build with Visual Studio.

As far as changing the entry point updating the ApplicationDefinition that's a fair point. That's a Visual Studio question, better suited for @chabiss.

trivalik commented 2 years ago

Is this maybe related to this http://connect.microsoft.com/VisualStudio/feedback/details/363934/app-initializecomponent-is-not-generated-when-startupuri-and-startup-are-missing ? I couldn't find the page but the url text says basically all.

CodingOctocat commented 1 year ago

我还从App.xaml中删除了StartupUri。

I have the same problem, if I remove the StartupUri from App.xaml then the code generated by app.g.i.cs is missing the correct InitializeComponent method. But I haven't had this problem before, strange bugs.

Correct app.g.i.cs/InitializeComponent template:

private bool _contentLoaded;

/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "7.0.10.0")]
public void InitializeComponent() {
    if (_contentLoaded) {
        return;
    }
    _contentLoaded = true;
    System.Uri resourceLocater = new System.Uri("/MyApp;V1.0.0.0;component/app.xaml", System.UriKind.Relative);

    #line 1 "..\..\..\App.xaml"
    System.Windows.Application.LoadComponent(this, resourceLocater);

    #line default
    #line hidden
}
trivalik commented 1 year ago

This sounds like the old connect bug, which was marked as "won't fix".