System Exception thrown when changing focus on screens when debuging run program in Visual Studio

Paul Harvey Chiverton Messina 5 Reputation points

Dear everybody.

Using Microsoft Visual Studio Community 2022 (64 bits) - Current Versión 17.9.3 on Windows 10 most recent update, Packaged WinUI with C#

I am programming from scratch a new program (WinUI with C#) in VisualStudio.

I have two screens on my computer. On one screen I have VisualStudio 2022 Community Edition working on a packaged program and on the other screen I have for example Microsoft Edge with a webpage from internet (doesn't really matter what Application I have open on this other screen, it could be the windows file explorer or whatever else).

I run debug the program I am working on from inside Visual Studio and all works fine and I exit the testing of the program.

If while running the program in Debug mode and I click on my other screen with the webpage for example, my program throws always an unhandled exception (below). The combobox SelectionChanged="PuertoSerie_Changed" event gets fired and throws the exception (below).

Why? Am I missing something? My program wants to execute only with the focus on it?


System.NullReferenceException: 'Object reference not set to an instance of an object.'

Microsoft.UI.Xaml.Controls.Primitives.Selector.SelectedItem.get devolvió null.


private void PuertoSerie_Changed(object sender, RoutedEventArgs e)


if (GLOBALES.ProgramaCargado)


if (GLOBALES.AbiertoPuertoSerie == false) // Puerto serie no está abierto


GLOBALES.NombrePuertoSerie = ComboPuertoSerie.SelectedItem.ToString();




MostrarMensajes("PUERTO SERIE AÚN ABIERTO", "Debe cerrar el puerto serie\nantes de cambiar de puerto.", 1);





<Rectangle Grid.Column="8" Grid.ColumnSpan="12" Grid.Row="2" Grid.RowSpan="7" Stroke="{StaticResource BrushGris2}" StrokeThickness="2"

Fill="{StaticResource BrushGris7}" Margin="5,0,5,0" HorizontalAlignment="Stretch" RadiusX="4" RadiusY="4"/> <!-- left, top, right, bottom-->

<StackPanel Grid.Column="9" Grid.ColumnSpan="11" Grid.Row="3" Grid.RowSpan="2" Orientation="Horizontal" Padding="0,0,0,0">

<TextBlock Style="{StaticResource TxtBlock1}">PUERTO SERIE</TextBlock>

<ComboBox Name ="ComboPuertoSerie"

IsEditable="False" Height="26" Width="100" FontSize="12" FontWeight="Bold" ItemsSource="{x:Bind PuertosSerieDisponibles}"

HorizontalContentAlignment="Center" Background="White" Foreground="{StaticResource BrushGris0}" CornerRadius="2"

SelectedIndex="0" BorderBrush="{StaticResource BrushGris0}" BorderThickness="1"


<Button x:Name="UpdatePuertosSerie" VerticalAlignment="Center" Click="ActualizarPS">

<Image Source="Assets/ImagenesBotones/Update.png" Width="15" Height="15" Margin="0,0,0,0" VerticalAlignment="Center"/>



<StackPanel Grid.Column="9" Grid.ColumnSpan="12" Grid.Row="6" Grid.RowSpan="2" Orientation="Horizontal">

<TextBlock Style="{StaticResource TxtBlock1}">VELOCIDAD</TextBlock>

<ComboBox Name ="ComboVelocidadPuertoSerie"

IsEditable="False" Height="26" FontSize="12" FontWeight="Bold"

HorizontalContentAlignment="Center" Background="White" Foreground="{StaticResource BrushGris0}" CornerRadius="2"

BorderBrush="{StaticResource BrushGris0}" BorderThickness="1"

SelectedIndex="8" SelectionChanged="VelocidadPuertoSerie_Changed">


<TextBlock Style="{StaticResource TxtBlock1}">baudios</TextBlock>


Any help appreciated.

Best Regards

Paul Harvey Chiverton

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.
723 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,218 questions
{count} votes