in a wpf project,How to add code in to the App's InitializeComponent method

兰树豪 381 Reputation points
2023-03-01T08:06:33+00:00

User's image

see the picture ,try to add the code to InitializeComponent or Main function , both no use, doesn't work

Developer technologies | Windows Presentation Foundation
Developer technologies | .NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2023-03-01T08:49:19.1166667+00:00

    To execute a function at startup, add this attribute to App.xaml:

    <Application 
       . . . 
       Startup="Application_Startup">
        
    </Application>
    

    Then define the function in App.xaml.cs:

    public partial class App : Application
    {
        private void Application_Startup( object sender, StartupEventArgs e )
        {
            MessageBox.Show( "Hello!" );
        }
    }
    

    Or use the events or constructor of the main window.

    The .g.cs and .g.i.cs files are generated and overwritten by system and cannot be edited manually.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.