CommunityToolkit (apparently ) not generating internal code

RogerSchlueter-7899 1,346 Reputation points
2023-02-06T20:19:06.5733333+00:00

I've decided to give the CommunityToolkit a try but my code and the parts of the Toolkit are not making a connection.

Here's the relevant parts of my MainWi8ndow:

<Window
    x:Class="MainWindow"

    <Window.DataContext>
        <self:MainWindowVM />
    </Window.DataContext>

    <Label
        Content="{Binding Path=FirstLabel}" />

    <Button
        Command="{Binding Path=ExitMe}"
        Content="Exit" />
</Window>

And the View Model:

Imports CommunityToolkit.Mvvm.ComponentModel
Imports CommunityToolkit.Mvvm.Input

Partial Public Class MainWindowVM
    Inherits ObservableObject
    ....
<RelayCommand>
Private Sub ExitMe()
    Windows.Application.Current.Shutdown()
End Sub
    ....
<ObservableProperty>
Private FirstLabel As String
....
<End Class

At run-time I get two error messages:

ExitMe' property not found on 'object' ''MainWindowVM'

That is obviously a correct error message but that is the way it is done in an example I am using as a guide.

BindingExpression path error: 'FirstLabel' property not found on 'object' ''MainWindowVM'

That one I don't understand. My understanding is that the toolkit generates the Public property from the private declaration. Is that not correct?

Thanks in advance for the help.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,824 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,779 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 48,616 Reputation points Microsoft Vendor
    2023-02-07T03:17:25.8866667+00:00

    You could use the following code.

    
    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
    
    
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. RogerSchlueter-7899 1,346 Reputation points
    2023-02-09T02:44:28.15+00:00

    Using the toolkit in the manner shown by my code is only possible with C#.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.