Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 尝试向UserControl添加控件时发生异常_Wpf_Exception_User Controls_Wpf Controls - Fatal编程技术网

Wpf 尝试向UserControl添加控件时发生异常

Wpf 尝试向UserControl添加控件时发生异常,wpf,exception,user-controls,wpf-controls,Wpf,Exception,User Controls,Wpf Controls,当我试图将控件添加到UserControl列表时,它抛出了“调用线程无法访问此对象,因为另一个线程拥有它。”异常 panel_PanelHolder.Children.Clear(); panel_PanelHolder.Children.Add(usr_panel); 但当我使用下面的代码时 Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>

当我试图将控件添加到UserControl列表时,它抛出了“调用线程无法访问此对象,因为另一个线程拥有它。”异常

        panel_PanelHolder.Children.Clear();
        panel_PanelHolder.Children.Add(usr_panel);
但当我使用下面的代码时

        Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
        {
            panel_PanelHolder.Children.Clear();
            panel_PanelHolder.Children.Add(usr_panel);
        }));
它抛出“异常已被调用的目标抛出。”异常

        panel_PanelHolder.Children.Clear();
        panel_PanelHolder.Children.Add(usr_panel);

我需要的是清除控件,并向面板添加一个控件。我不知道如何尽快解决这个问题。我使用一个STA线程来动态操作WPF窗口控件。有人能帮我吗?thx.

我今天遇到了这个问题。使用调试器,您需要深入挖掘异常树。对于每个异常,可能都有一个内部异常。挖得越深越好。然后查看堆栈跟踪。在我的例子中,我有一个(自定义)用户控件,它在构造过程中导致了null ref异常

        panel_PanelHolder.Children.Clear();
        panel_PanelHolder.Children.Add(usr_panel);

你的情况可能不那么明显。查看最深异常的stacktrace,然后在该代码行设置断点。如果进一步调试没有发现问题,请尝试使用try..catch块来包围有问题的代码行。然后,您可以在本地更深入地挖掘异常。

第一个异常有意义,第二个则不那么有意义。您似乎遇到了另一个问题,只有在解决跨线程问题时才会出现。可能会发布整个错误(包括堆栈跟踪)“异常已由调用的目标引发”异常总是有一些内部异常。你能发布吗?你有多少线程?如果有多个,那么面板控制权归谁所有?对于第二个异常,请编写内部异常