c# - 无法将匿名方法转换为类型 'System.Delegate',因为它不是委托(delegate)类型

标签 c# wpf

我想在 WPF 应用程序的主线程上执行此代码并收到错误我无法弄清楚出了什么问题:

private void AddLog(string logItem)
        {

            this.Dispatcher.BeginInvoke(
                delegate()
                    {
                        this.Log.Add(new KeyValuePair<string, string>(DateTime.Now.ToLongTimeString(), logItem));

                    });
        }

最佳答案

您也可以使用MethodInvoker为了这:

private void AddLog(string logItem)
        {
            this.Dispatcher.BeginInvoke((MethodInvoker) delegate
            {
                this.Log.Add(new KeyValuePair<string, string>(DateTime.Now.ToLongTimeString(), logItem));
            });
        }

关于c# - 无法将匿名方法转换为类型 'System.Delegate',因为它不是委托(delegate)类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15935867/

相关文章:

c# - Chrome cookie 不是最新的

c# - 如何共享项目的代码文档?

c# - 列出tfs中文件夹的所有内容

WPF ComboBox.SelectedValue 为空,但 .SelectedItem 不是; SelectedValuePath 已设置。为什么?

c# - 如何将我的 wpf 应用程序连接到在线数据库?

c# - 将 DataGrid 导出到文本文件

c# - 对新 ihttpfactory 中 httpclient 的生命周期感到困惑

c# - 以非管理员身份远程监控 Windows 服务状态?

wpf - 我有一个 PopupBox,我想更改其图标。目前它默认为 DotsVertical,我希望将其设置为 DotsHorizo​​ntal

c# - 如何从 View 访问我的 View 模型中的数据网格控件?