我有一个 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/