1

I am doing a Simple "Crud" without database, and when i write anything in any of my textBox of my project i have an exception getting thrown.

System.ComponentModel.Win32Exception: 'Window handle is invalid',

This only happens when I write in the textbox, I didn't find any similar question here on Stack Overflow. If someone can help me I'll be very grateful. The complete error is this one:

System.ComponentModel.Win32Exception
  HResult=0x80004005
  Message=Window identifier is invalid
  Source=WindowsBase
  StackTrace:
   at MS.Win32.UnsafeNativeMethods.GetMessageW(MSG& msg, HandleRef hWnd, Int32 uMsgFilterMin, Int32 uMsgFilterMax)
   at System.Windows.Threading.Dispatcher.GetMessage(MSG& msg, IntPtr hwnd, Int32 minMessage, Int32 maxMessage)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.VisualTree.RootSourceFinder.HwndWatcher()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Note: this error I've been having since yesterday, but when I turned on the computer today it wasn't happening. It started after a few runs, the same way it started yesterday, but now even if I restart the pc, it's running

CreateWindow.xalm:

<Window x:Class="Atividade_1_Luz.CreateView"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Atividade_1_Luz"
        mc:Ignorable="d"
        Title="CreateView" Height="450" Width="800">
    <Grid >
        <StackPanel Background="AliceBlue" Orientation="Vertical" Margin="295,60,307,62">
            <TextBlock FontSize="15" TextAlignment="Center" HorizontalAlignment="Center" Margin="0,10,0,0" TextWrapping="Wrap" Text="Register Product" VerticalAlignment="Top" Height="28" Width="138"/>
            <Label VerticalContentAlignment="Center" HorizontalAlignment="Center" Margin="10,5,10,0" Width="100" Height="30" Content="Name" FontSize="10"/>
            <TextBox Margin="25,-10,28,5" Height="24" Width="100" Text="{Binding Name}"
                     VerticalAlignment="Center"
                     HorizontalAlignment="Center"
                     />
            <Label VerticalContentAlignment="Center" HorizontalAlignment="Center" Margin="10,5,10,0" Width="100" Height="30" Content="Category" FontSize="10"/>
            <TextBox Margin="25,-10,28,5" Height="24" Width="100" TextWrapping="Wrap" Text="{Binding Category}"
                     VerticalAlignment="Center"
                     HorizontalAlignment="Center"
                     />
            <Label VerticalContentAlignment="Center" HorizontalAlignment="Center" Margin="10,5,10,0" Width="100" Height="30" Content="Description" FontSize="10"/>
            <TextBox Margin="25,-10,28,5" Height="24" Width="100" TextWrapping="Wrap" Text="{Binding Description}"
                     VerticalAlignment="Center"
                     HorizontalAlignment="Center"
                     />

            <Label VerticalContentAlignment="Center" HorizontalAlignment="Center" Margin="10,5,10,0" Width="100" Height="30" Content="Price" FontSize="10"/>
            <TextBox Margin="25,-10,28,5" Height="24" Width="100" TextWrapping="Wrap" Text="{Binding Price}"
                     VerticalAlignment="Center"
                     HorizontalAlignment="Center"
                     />
            <StackPanel Orientation="Horizontal" Height="45">
                <Button Command="{Binding Command}" Width="50" HorizontalAlignment="Left" Height="20" VerticalAlignment="Center" Content="Back" Margin="30,0,10,0"/>
                <Button Command="{Binding CommandCreate}" Width="50" HorizontalAlignment="Left" Height="20" VerticalAlignment="Center" Content="Create" Margin="20,0,10,0"/>
            </StackPanel>

        </StackPanel>
        <Border CornerRadius="10" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="299" Margin="295,60,0,0" VerticalAlignment="Top" Width="192"/>

    </Grid>
</Window>

UpdateWindow.xalm:

<Window x:Class="Atividade_1_Luz.UpdateView"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Atividade_1_Luz"
        mc:Ignorable="d"
        Title="UpdateView" Height="450" Width="800">
    <Grid>
        
        <StackPanel Background="AliceBlue" Orientation="Vertical" Margin="295,60,307,62">
            <TextBlock FontSize="15" TextAlignment="Center" HorizontalAlignment="Center" Margin="0,10,0,0" TextWrapping="Wrap" Text="Update Product" VerticalAlignment="Top" Height="28" Width="138"/>
            <Label VerticalContentAlignment="Center" HorizontalAlignment="Center" Margin="10,5,10,0" Width="100" Height="30" Content="Name" FontSize="10"/>
            <TextBox Margin="25,-10,28,5" Height="24" Width="100" Text="{Binding Name}"
                     VerticalAlignment="Center"
                     HorizontalAlignment="Center"
                     />
            <Label VerticalContentAlignment="Center" HorizontalAlignment="Center" Margin="10,5,10,0" Width="100" Height="30" Content="Category" FontSize="10"/>
            <TextBox Margin="25,-10,28,5" Height="24" Width="100" TextWrapping="Wrap" Text="{Binding Category}"
                     VerticalAlignment="Center"
                     HorizontalAlignment="Center"
                     />
            <Label VerticalContentAlignment="Center" HorizontalAlignment="Center" Margin="10,5,10,0" Width="100" Height="30" Content="Description" FontSize="10"/>
            <TextBox Margin="25,-10,28,5" Height="24" Width="100" TextWrapping="Wrap" Text="{Binding Description}"
                     VerticalAlignment="Center"
                     HorizontalAlignment="Center"
                     />

            <Label VerticalContentAlignment="Center" HorizontalAlignment="Center" Margin="10,5,10,0" Width="100" Height="30" Content="Price" FontSize="10"/>
            <TextBox Margin="25,-10,28,5" Height="24" Width="100" TextWrapping="Wrap" Text="{Binding Price}"
                     VerticalAlignment="Center"
                     HorizontalAlignment="Center"
                     />
            <StackPanel Orientation="Horizontal" Height="45">
                <Button Command="{Binding Command}" Width="50" HorizontalAlignment="Left" Height="20" VerticalAlignment="Center" Content="Back" Margin="30,0,10,0"/>
                <Button Command="{Binding CommandCreate}" Width="50" HorizontalAlignment="Left" Height="20" VerticalAlignment="Center" Content="Create" Margin="20,0,10,0"/>
            </StackPanel>

        </StackPanel>
        <Border CornerRadius="10" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="299" Margin="295,60,0,0" VerticalAlignment="Top" Width="192"/>
    </Grid>
</Window>

I don't know if this is normal, but there's no problem with this line not being used? Image Local:

Bas H
  • 2,114
  • 10
  • 14
  • 23
  • What is happening in your ViewModel when you edit the text? If you remove the binding and try to type in the text box, does it still throw the error? – Andrew H Dec 16 '21 at 15:25
  • hi thanks for answering, so i just tested without the bindings and the same error happened, and in my modelView i do nothing while the input is inserted, only when the create button for example is pressed, but the program throws the error as soon as I type a letter or 2, I'll put my modelView in the post. – Leonardo Moreira Dec 16 '21 at 15:59
  • actually I'm not able to edit my post, I don't know why :( – Leonardo Moreira Dec 16 '21 at 16:02

1 Answers1

2

I finally found the answer here at this link: Solution

this solution really helped me basically in your visual studio you should go to Tools > Options > Debugging > General > and disable UI Interface Debugging Tools for XAML option.

Hope to help someone with the same problem.