当我在 TextBox 中编写时,我在 WPF 中出现错误“窗口标识符无效”

When I write in a TextBox I have a error 'The window identifier is invalid ' in WPF

提问人:Leonardo Moreira 提问时间:12/16/2021 最后编辑:Bas HLeonardo Moreira 更新时间:12/17/2021 访问量:222

问:

我正在做一个没有数据库的简单“Crud”,当我在项目的任何 textBox 中编写任何内容时,我都会抛出异常。

System.ComponentModel.Win32Exception: '窗口句柄无效',

这只有在我在文本框中写入时才会发生,我在 Stack Overflow 上没有找到任何类似的问题。如果有人能帮助我,我将不胜感激。完整的错误是这样的:

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()

注意:我从昨天开始就一直遇到这个错误,但是当我今天打开计算机时,它没有发生。它在运行几次后开始,就像昨天开始一样,但现在即使我重新启动电脑,它也能运行

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>

我不知道这是否正常,但是不使用这一行没有问题吗?Image Local:

C# WPF XAML 异常 文本框

评论

0赞 Andrew H 12/16/2021
编辑文本时,ViewModel 中发生了什么?如果删除绑定并尝试在文本框中键入内容,是否仍会引发错误?
0赞 Leonardo Moreira 12/16/2021
嗨,感谢您的回答,所以我刚刚在没有绑定的情况下进行了测试,并且发生了同样的错误,在我的 modelView 中,我在插入输入时什么都不做,例如,只有当按下创建按钮时,但是程序会在我输入一个字母或 2 时立即抛出错误,我会把我的 modelView 放在帖子中。
0赞 Leonardo Moreira 12/17/2021
实际上我无法编辑我的帖子,我不知道为什么:(

答:

2赞 Leonardo Moreira 12/17/2021 #1

我终于在这个链接上找到了答案: 解决方案

这个解决方案基本上对我有帮助,在你的Visual Studio中,你应该转到“工具”>“选项”>“调试”>“常规”>,并禁用“适用于XAML的UI接口调试工具”选项。

希望能帮助有同样问题的人。