【问题标题】:Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type in wpf无法将 lambda 表达式转换为类型“System.Delegate”,因为它不是 wpf 中的委托类型
【发布时间】:2014-11-05 06:17:04
【问题描述】:
public void loadtemplist(DataTable dt)
    {
      this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,

          (Delegate) (() => this.loadtemplist1(dt))   //error


          );
    }

public void loadtemplist1(DataTable dt)
    {

-----
-----

}

上面的代码抛出Cannot convert lambda expression to type 'System.Delegate',因为它不是委托类型

【问题讨论】:

    标签: c# lambda


    【解决方案1】:

    您不能将匿名方法直接转换为 System.Delegate - 您需要先将其包装在 Action 中。

    试试这个:

    public void loadtemplist(DataTable dt)
    {
      this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,
    
          new Action(() => { this.loadtemplist1(dt); } )
          );
    }
    

    【讨论】:

      【解决方案2】:

      你应该这样做

      this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, 
                                  new Action(() => this.loadtemplist1(dt)) );
      

      查看演示:https://dotnetfiddle.net/nz9xxD

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多