WinUI 3 application crashes at App.g.i.cs

Peggun 20 Reputation points
2024-09-23T04:58:18.58+00:00

I am trying to create a WinUI 3 Application in C# and my application keeps crashing at App.g.i.cs

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
            UnhandledException += (sender, e) =>
            {
                if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
            };
#endif

Also in the Debug Console, there is an error which I'm not sure what it means Exception thrown: 'System.Runtime.InteropServices.COMException' in WinRT.Runtime.dll WinRT information: The application called an interface that was marshalled for a different thread.

Here are the MainWindow.xaml and MainWindow.xaml.cs files

<Window
    x:Class="UTD.WindowsGUI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:UTD.WindowsGUI"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel>
        <TextBlock x:Name="Block" Text="Helo" />
    </StackPanel>
</Window>
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using UTD.Core.Models;
using Microsoft.UI.Xaml.Media.Animation;
using Windows.Devices.Input;
using UTD.Startup;
using System.Diagnostics;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace UTD.WindowsGUI
{
    /// <summary>
    /// An empty window that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

I have done tons of research and all have left me to a dead end. Any help will be appreciated!

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
803 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Peggun 20 Reputation points
    2024-09-23T13:47:20.06+00:00

    Ok, I have figured out the issue.

    Sometimes when I have cleaned a solution, the simple usings that were pre-generated were missing before I rebuilt it. This included Microsoft.UI. Being the person I am I went ahead and installed the Microsoft.UI.Xaml NuGet package. After installing this on the working solution, the code broke at the same spot. I have no idea why but at least I have fixed it.

    Thanks for all your help Roy! :)


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.