Difference between lambda syntax and VB.NET address
I recently entered the joyous world of VB.NET, but for the life of me, I don't seem to figure out why the following isn't working.
When I write this code here, everything is fine:
MyNavigationCommand = New RelayCommand(AddressOf Navigate)
Private Sub Navigate()
Navigator.NavigateTo(NavigationRoutes.DetailScreen)
End Sub
However, when I try to do the same using the lambda syntax, my code inside the lambda doesn't get hit when I click the button that runs the command.
The following line doesn't work:
MyNavigationCommand = New RelayCommand(Sub() Navigator.NavigateTo(NavigationRoutes.DetailScreen))
This should work exactly the same as my previous approach, right? Or am I missing something?
+3
source to share
1 answer
I'm not sure what is wrong for you. This is my code I wrote to test this:
Sub Main
Dim MyNavigationCommand = New RelayCommand(AddressOf Navigate)
Dim MyNavigationCommand2 = New RelayCommand(Sub() Console.WriteLine("!"))
Navigate
MyNavigationCommand
MyNavigationCommand2
End Sub
Public Delegate Sub RelayCommand
Public Sub Navigate()
Console.WriteLine("!")
End Sub
Running this code produces three lines !
.
0
source to share