c# - 表达式在 vb.net 应用程序中不产生值

标签 c# .net vb.net winforms mvvm

我有一个 Winforms 应用程序,我想在其中使用 MVVM 设计模式:

我关注这个tutorial

这是一篇非常有趣的文章,但我遇到了这个问题:我的应用程序是 vb.net,我将代码 (C#) 转换为 vb.net,它工作正常,除了这个:

C#代码

 protected void ViewModel_Validated(object sender, EventArgs e)
    {
        this.ViewModel.AttachedControls.ToList().ForEach(c => this.errorProvider.SetError(c.Value as Control, ""));
        if (!string.IsNullOrEmpty(this.ViewModel.Error)) {
            this.ViewModel.Messages.ToList().ForEach(message => {
                this.errorProvider.SetError(this.ViewModel.AttachedControls[message.Key] as Control, message.Value);
            });
        }
    } 

Vb.net代码

 Protected Sub ViewModel_Validated(ByVal sender As Object, ByVal e As EventArgs)
        Me.ViewModel.AttachedControls.ToList().ForEach(Function(c) Me.errorProvider.SetError(TryCast(c.Value, Control), ""))
        If Not String.IsNullOrEmpty(Me.ViewModel.[Error]) Then
            Me.ViewModel.Messages.ToList().ForEach(Function(message)
                                                       Me.errorProvider.SetError(TryCast(Me.ViewModel.AttachedControls(message.Key), Control), message.Value)

                                                   End Function)
        End If
    End Sub

问题出在这一行:

Me.ViewModel.AttachedControls.ToList().ForEach(Function(c) Me.errorProvider.SetError(TryCast(c.Value, Control), ""))

错误:

表达式不产生值。

我需要知道

  • 此错误的原因是什么?
  • 我该如何解决?

最佳答案

Function 更改为 Sub
Function 表示返回值的方法,但您的代码:Me.errorProvider.SetError(TryCast(c.Value, Control), "") 没有。

来自 MSDN:

To return a value to the calling code, use a Function procedure; otherwise, use a Sub procedure.

所以尝试:

Me.ViewModel.AttachedControls.ToList().ForEach(Sub(c) Me.errorProvider.SetError(TryCast(c.Value, Control), ""))

还有下一行你需要改变:

Me.ViewModel.Messages.ToList().ForEach(Sub(message)
                                           Me.errorProvider.SetError(TryCast(Me.ViewModel.AttachedControls(message.Key), Control), message.Value)
                                       End Sub)

关于c# - 表达式在 vb.net 应用程序中不产生值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24862265/

相关文章:

.net - Wpf 用户安全策略

java - 在使用 Web 服务的客户端应用程序中使用证书

c# - 从 C# .ashx 运行 cscript.exe 不执行 vbscript 文件中的代码

c# - Fluent Assertions 将字符串与 Guid 进行比较

c# - 如何优雅地停止 System.Threading.Timer?

mysql - 根据另一列检索数据库数据

vb.net - Linq - 从不同列表类型中排除项目

c# - T4 抑制错误消息 "ErrorGeneratingOutput"

c# - System.Web.Razor.dll 未复制到 bin 文件夹

c# - 捕获组减法/求反