[英]Converting C# fluent interface to code in VB.NET
I have the following C# code which I'm trying to convert to VB.NET. 我有以下C#代码,正尝试将其转换为VB.NET。 I also want to replace the "Console.WriteLine"s with callbacks.
我也想用回调替换“ Console.WriteLine”。
I think and hope I've managed to convert it to some extent: 我认为并希望我能够在某种程度上进行转换:
nInteractorsAgent
.AddInteractorFor(currentWindowBounds, Literals.RootId, 0, currentWindowHandle.ToString(), "MyFirstActivatable")
.WithActivatable()
.HasActivationFocus(id => Console.WriteLine("Id: {0} got activation focus.", id))
.SetTentativeFocusEnabled(true)
.HasTentativeFocus(id => Console.WriteLine("Id: {0} got tentative activation focus.", id))
.WhenFocusChanged((id, hasTentativeFocus, hasActivationFocus) => { })
.LostFocus(id => Console.WriteLine("Id: {0} lost focus.", id))
.WhenActivated(id => Console.WriteLine("Id: {0} activated", id));
My approach in VB.NET is this: 我在VB.NET中的方法是:
Dim v1 As VirtualInteractor = nInteractorsAgent.AddInteractorFor(currentWindowBounds, Literals.RootId, 0, "0", "MyFirstActivatable").WithActivatable().HasActivationFocus(Sub() HasActivationFocus(v1.Id)).SetTentativeFocusEnabled(True).HasTentativeFocus(Sub() HasTentativeFocus(v1.Id)).WhenFocusChanged(Sub() WhenFocusChanged(v1.Id, False, False)).LostFocus(Sub() LostFocus(v1.Id)).WhenActivated(Sub() WhenActivated(v1.Id))
Public Shared Sub HasActivationFocus(ByVal id As String)
End Sub
Public Shared Sub HasTentativeFocus(ByVal id As String)
End Sub
Public Shared Sub WhenFocusChanged(ByVal id As String, ByVal hasTentativeFocus As Boolean, ByVal hasActivationFocus As Boolean)
End Sub
Public Shared Sub WhenFocusChanged()
End Sub
Public Shared Sub LostFocus(ByVal id As String)
End Sub
Public Shared Sub WhenActivated(ByVal id As String)
End Sub
However, the compiler tells me that something is wrong about 但是,编译器告诉我有关某些问题
.WhenFocusChanged(Sub() WhenFocusChanged(v1.Id, False, False))
and 和
.WhenActivated(Sub() WhenActivated(v1.Id))
The compiler tells me that there's no version of WhenFocusChanged. 编译器告诉我,WhenFocusChanged没有版本。 The declarations are these:
声明如下:
<Extension>
Public Shared Function WhenFocusChanged(behaviors As IEnumerable(Of ActivatableBehavior), action As ActivationFocusChangedCallback) As IEnumerable(Of ActivatableBehavior)
<Extension>
Public Shared Function WhenFocusChanged(behavior As ActivatableBehavior, action As ActivationFocusChangedCallback) As ActivatableBehavior
Can anybody show me what I'm doing wrong here and how to do it correctly? 有人可以告诉我我在这里做错了什么以及如何正确地做吗? I don't know which information apart from the given is important for readers in this case.
在这种情况下,我不知道除了给定的信息外,哪些信息对读者也很重要。
Thank you. 谢谢。
That won't work like the original code. 那不会像原始代码那样工作。 You need a mix of lambda and delegate .
您需要混合使用lambda和委托 。 Here's a quick example of something similar in vb.net.
这是vb.net中类似内容的快速示例。
Module Module1
Sub Main()
Dim o As New Test
o.DoSomething(Sub(paramName) Console.WriteLine("The id is {0}", paramName))
End Sub
End Module
Class Test
Public Property ID As Integer = 10
Delegate Sub SomeDelegate(ByVal id As Integer)
Public Function DoSomething(ByVal f As SomeDelegate) As Test
f(Me.ID)
Return Me
End Function
End Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.