Debugging UWP application unhandled exceptions in “App.g.i.cs” generated C# file

Posted: (EET/GMT+2)

 

If you are developing XAML application on the Windows 10 UWP platform, chances are that at some point, your application will stop to an exception in a generated C# code file called "App.g.i.cs" in the method "InitializeComponent".

Unfortunately, these errors can be difficult to track, but there's one strategy that I've found to be effective every time. But first, you need to understand why errors occur, and why for example the Call Stack window in Visual Studio will not help you track the problem location.

The "App.g.i.cs" file is a special file generated during the build process, and contains the InitializeComponent call. This method is responsible for constructing the UI for the page, i.e. loading the XAML code in question. Often, errors like this occur because there are logical errors in the XAML code, but the C# compiler doesn't spot these.

Luckily, the exception that occurs usually provides some hints on why the error occurs. That said, be sure to examine the exception object for potential hints. If that fails, open the offending XAML page/view in Visual Studio's UI designer, and start looking for blue dots in your vertical scrollbar. This indicates logical errors that almost always are the cause for these initialization errors.

Correct the error(s), and try running the application again. It is likely succeed. If not, repeat the exception investigation and XAML corrections, and eventually, you should be fine.

Hope this helps!