0

I've a ContentView in .NET MAUI with two bindable properties: ListData and ListType. ListData is of type string and contains a serialized JSON object. ListType is a string whose value controls the appearance of the list. The ContentView itself consists mainly of a CollectionView and I'm using the MVVM Community Toolkit.

My question:

Where to create an instance of the viewmodel with the values of the bindable properties and assign it to the BindingContext? The values of the bindable properties are not yet present in the constructor of the ContentView.

Rolf
  • 191
  • 1
  • 4
  • 13
  • 1) Add relevant source code to question. 2) When are the values ready? Show that code also. You might need to explain what code runs when. 3) It should be possible to set the property values later - have you tried? – ToolmakerSteve Jul 02 '23 at 21:40
  • Please describe the problem more clearly and explain what you're actually trying to accomplish. There may be a better way, depending on what problem you're trying to solve (I'm not talking about the technical issue you're facing at the moment). – Julian Jul 04 '23 at 07:27

1 Answers1

0

Where to create an instance of the viewmodel

The answer is you simply don't.

Using dependency injection is what people do those days. In fact - by creating new project in MAUI you are already using it.

Instances are created when and if they are needed. You just set the rules for it.

...and assign it to the BindingContext

This is usually done in the constructor.

Anyway, I see what you are trying to do, and I do not think that this is the right approach for it.

I would either use a Converter, or Template Selector (or something similar).

https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/datatemplate

H.A.H.
  • 2,104
  • 1
  • 8
  • 21