My project is in.NET 6. Why doesn't the following code work?
My understanding is to produce code through attributes, simplifying the coding.
Did I do something wrong? Xaml:
<Window.DataContext>
<self:MainWindowVM />
</Window.DataContext>
<StackPanel >
<Label Content="{Binding Path=FirstLabel ,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged }" Height="50" />
<Button Command="{Binding Path=ExitMe}" Content="Exit" />
</StackPanel>
Codebedhing:
Imports CommunityToolkit.Mvvm.ComponentModel
Imports CommunityToolkit.Mvvm.Input
Class MainWindow
End Class
Partial Public Class MainWindowVM
Inherits ObservableObject
Public Sub New()
Me.FirstLabel = "default value..."
End Sub
<ObservableProperty>
Private FirstLabel As String
<RelayCommand>
Private Sub ExitMe()
Windows.Application.Current.Shutdown()
End Sub
End Class
At run-time I get two error messages:
ExitMe' property not found on 'object' ''MainWindowVM'
BindingExpression path error: 'FirstLabel' property not found on 'object' ''MainWindowVM'
I know the following code will work. But why doesn't the above code work?
Imports CommunityToolkit.Mvvm.ComponentModel
Imports CommunityToolkit.Mvvm.Input
Class MainWindow
End Class
Partial Public Class MainWindowVM
Inherits ObservableObject
Public Sub New()
Me.FirstLabel = "default value..."
Me.ExitMe = New RelayCommand(AddressOf exMe)
End Sub
<ObservableProperty>
Private fLabel As String
Public Property FirstLabel As String
Private Sub Save()
Me.fLabel = FirstLabel
End Sub
<RelayCommand>
Private Sub exMe()
Windows.Application.Current.Shutdown()
End Sub
Public ReadOnly Property ExitMe As ICommand
End Class