CommunityToolkit.Mvvm Not Exposing Any Methods

RogerSchlueter-7899 1,446 Reputation points
2023-01-18T22:00:26.2733333+00:00

I have addded the CommunityToolkit.Mvvm (Version 8.1.0 to my project via NuGet. It is a wpf, .NET 6.0 project. Here are the relevant parts of my MainWindow ViewModel:

Imports CommunityToolkit.Mvvm.Input
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO

Public Class MainWindowVM
    Inherits ObservableObject
    Implements IDisposable
    ....
    Private _SelectedValue As String
    Public Property SelectedValue As String
        Get
            Return _SelectedValue
        End Get
        Set(value As String)
            SetProperty(_SelectedValue, value)
        End Set
    End Property
    ....
End Class

The SetProperty method gives compile-time error: 'SetProperty' is not declared. It may be inaccessible due to its protection level. I have tried other methods that are supposed to be available but none are recognized. What is missing?

Developer technologies | Windows Presentation Foundation
Developer technologies | .NET | Other
Developer technologies | VB
{count} votes

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2023-01-19T18:29:16.9433333+00:00

    SetProperty is a protected method on the ObservableObject class.

    I notice in your file that you're missing an Import CommunityToolkit.Mvvm.ComponentModel statement. The ObservableObject type is defined there. Without that import then the compiler cannot figure out your base type and hence SetProperty isn't available. That is the root of your issue I believe. You're seeing a cascading compiler error.

    If the editor is showing ObservableObject as being defined then it isn't coming from the toolkit and that is causing the problem instead. Right click the type and go to definition. You should end up in the toolkit metadata. If you end up elsewhere then you have multiple types with the same name.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Stephen Miller 5 Reputation points
    2023-01-22T23:39:38.3866667+00:00

    @Michael_Taylor I do not agree with this answer. I am using CommunityToolkit.Mvvm.ComponentModel and I get very odd results. If I make a change to a view model where I have any ObservableProperty properties defined, it simply stops running any analyzers or doing any sort of code generation.

    When I see the red squiggly line of death, all I do is close the solution and reopen it. This forces the analyzers to run and the missing properties get resolved.

    So, it is not a matter of a missing component in the using definitions, it is something in the process of running or rerunning the analyzers. This is VERY annoying...........

    0 comments No comments

  2. Stephen Miller 5 Reputation points
    2023-01-22T23:46:01.74+00:00

    @Michael_Taylor I do not agree with this answer. I AM using CommunityToolkit.Mvvm.ComponentModel and I get very odd behavior. If I make a change to a view model where I have any ObservableProperty properties defined, it simply stops running any analyzers or doing any sort of code generation. If I am correct, it stopped working when I updated my version from the prerelease version to 8.1.0

    When I see the red squiggly line of death, all I do is close the solution and reopen it. This forces the analyzers to run and the missing properties get resolved. If I tried to do a build before closing the solution, it does not build, but upon simply reopening the solution, it will build without doing anything...

    So, IMO it is not a matter of a missing component in the using definitions (or it could be in the case of the original post), it is something in the process of running or rerunning the analyzers. They stop running... In fact, it looks like the partial classes that are generated by the analyzers get deleted or corrupted somehow. There is still some investigation and debugging on the CommunityToolkit.MVVM side of things required. This is VERY annoying...........


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.