Are QueryProperties supposed to work with [ObservableProperty]? #546
-
I'm working through learning .NET MAUI and using the MVVM toolkit. As I'm reaching the navigation stage and parameter passing, I'm running into issues where I have to manually call I can build a new project to repro this if it's not obvious from the description below. I have a simple 2-page app. The primary page is a list using a CollectionView. When you tap a cell, it navigates ( All pages are setup using MVVM/DI, and the detail page should simply display the name. The VM has the property
So when the VM is instantiated, After some digging, I implemented
This doesn't seem intentional -- but I'm having a hard time finding any documentation hinting why this is happening. Am I misunderstanding some part of one or the other of these features? Is there something special that needs to be done in the lifecycle when the |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 8 replies
-
I can see two potential issues which may be both causing your overall problem.
As an added piece of advice I would use And a further hopefully bit of helpful information is that this: [ObservableProperty]
public Character character; Is not actually the property it's the backing field, the MVVM toolkit will generate you a [ObservableProperty]
private Character character;
Also please let me know if this helps or if some of my assumptions are wrong 🙂 |
Beta Was this translation helpful? Give feedback.
-
None of that seems to have had any impact. I had tried most of your first suggestion previously (some of the public/private and naming was just where I ended up trying various permutations). I will try to spend some time and throw together a reproducible project next. |
Beta Was this translation helpful? Give feedback.
-
I took some time this morning and wrote up the reproduction - and am glad I did, it helped point me toward the actual problem... which is that the Character, when created via 'new', is actually a sub-class of Character. So the problem seems to be that if you have a collection view page with, let's say, a collection of You can see the repository here: https://github.com/DonThompson/communitytoolkitrepro1 I've tried to document it as best I can in both the readme and with comments in DetailViewModel.cs. Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
If anyone in the future is stuck here, the comments above led us to these conclusions:
|
Beta Was this translation helpful? Give feedback.
-
Closed as answered |
Beta Was this translation helpful? Give feedback.
If anyone in the future is stuck here, the comments above led us to these conclusions:
[QueryProperty]
requires that you are using a somewhat (untested by me here) primitive type OR that your type implementsIConvertible
. If you're using derived classes and may pass those as parameters through the navigation system, I would just assume the[QueryProperty]
is off limits.IQueryAttributable
yourself. It is crucial that when assigning the values, if they are[ObservableProperty]
, you assign them to the Property (the capitalized one), not the Field (the lowercase one), otherwise change notification doesn't work.