Hello @Mielesplayz ,
You could try using interactive MVVM and data bindings. Viewmodels typically implement the INotifyPropertyChanged
interface, which provides the ability for a class to raise the PropertyChanged
event whenever one of its properties changes. The data binding mechanism in .NET MAUI attaches a handler to this PropertyChanged
event so it can be notified when a property changes and keep the target updated with the new value.
For more information, please see: Data binding basics - .NET MAUI | Microsoft Learn
Data binding and MVVM - .NET MAUI | Microsoft Learn
You can also refer to the sample :
- Set binding context for the page, see the source code here.
- Add the
Label
whoseText
property is bound to theDisplayText
property ofKeypadViewModel
, see the source code here. - The
KeypadViewModel
needs to implement the theINotifyPropertyChanged
interface, you can refer to the usage of DisplayText property. If you have a model to provide this property, please check the Use pass-through properties with a viewmodel part in learning modules .
The sample shows how the InputString
changes when the AddCharCommand
/ DeleteCharCommand
is triggered, and the DisplayText
will be updated in the set method of InputString
. You can update the UserName
property of your VM, the Label(element is bound to UserName
) will be updated as well.
In addition, you can see the MVVM part in books. The doc is about Xamarin, it's old, but still conceptually valid.
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.