C# 在粘贴处理程序WPF中显示MessageBox

C# 在粘贴处理程序WPF中显示MessageBox,c#,wpf,event-handling,C#,Wpf,Event Handling,我有一个对话框,用户可以在其中编辑一些字段。有3个特定的验证。对于这些字段,我创建了一个粘贴处理程序,如下所示: DataObject.AddPastingHandler(myTextBox, numericValidatorHandler); Dispatcher.BeginInvoke((Action)delegate(){ MessageBox.Show("Cannot Paste ..."); }, null); 当我试图在处理程序中显示messagebox时,我得到一个异常

我有一个对话框,用户可以在其中编辑一些字段。有3个特定的验证。对于这些字段,我创建了一个粘贴处理程序,如下所示:

DataObject.AddPastingHandler(myTextBox, numericValidatorHandler);
Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);
当我试图在处理程序中显示messagebox时,我得到一个异常。看起来浆糊在不同的线程中运行

Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);
我当时的解决办法是使用backgroundworker并设置两个事件DoWork和RunWorkerCompleted

Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);
在第一个示例中,我使用一些参数设置结果,这些参数通过'Argument'属性从numericValidatorHandler传递

Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);
“numericValidatorHandler”

bw.RunWorkerAsync(args);
Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);

Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);
“DoWorkHandler”

e.Result = e.Argument;
Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);

Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);
“RunWorkerCompletedHandler”

//Here I just use e.Result to create an output message for the messagebox
Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);

是否有一种更简单的方法可以在过去的eventhandler中显示MessageBox?

您可以使用
Dispatcher
类将工作项添加到UI线程工作项队列中。试试这个:

public void DataObjectPastingEventHandler(object sender, DataObjectPastingEventArgs e) 
{
    Dispatcher.CurrentDispatcher.Invoke((Action)delegate()
    {
        MessageBox.Show("Hello");
    });
}
Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);

更新>>>

Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);
有几件事你可以试试。。。第一个是初始化
Dispatcher
,使其位于UI线程上。在
窗口
构造函数或加载的
事件处理程序中调用此函数,您可以确保UI线程正在运行:

Dispatcher uiDispatcher = Dispatcher.CurrentDispatcher;
Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);
如果这不能解决问题,那么您还可以尝试异步运行原始方法:

public void DataObjectPastingEventHandler(object sender, DataObjectPastingEventArgs e) 
{
    Dispatcher.BeginInvoke((Action)delegate()
    {
        MessageBox.Show("Hello");
    }, null);
}
Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);

如果这仍然不起作用,那我就没有主意了。。。我只想找到其他地方来启动
消息框。

就像你们中的一些人所说的那样,使用Dispatcher更简单

Dispatcher.BeginInvoke((Action)delegate(){
    MessageBox.Show("Cannot Paste ...");
}, null);

通过
调度程序
将调用委托回UI线程?我尝试定义一个ThreadStart委托,用调度程序调用它并传递参数。如果我不使用字符串[]作为参数,则它可以工作。我尝试了此解决方案,但出现了另一个异常“无法在调度程序处理挂起时执行此操作”。我无法从我的调度程序访问“CurrentDispatcher”属性。你的暗示很有帮助。通过Dispatcher,只需使用异步。打电话谢谢你的反馈。。。我已经相应地更新了我的答案。如果这个答案有助于解决您的问题,请按照本网站的惯例。