How to update UI easily on variable change in MAUI
I need to update a lot of text and it is desirable to link the update to a change in variables, I tried to use binding, but as I understood it, it does not suit me
25 Replies
why doesnt it suit you?
its the standard way of doing it
I Googled and did not understand how to do this, as I understood, you can only bind two properties of different objects
idk what you mean different objects but you can bind any dependency property
which is almost anything you need to bind
i recommend reading up about the basics of mvvm first and taking this slower
Data binding and MVVM - .NET MAUI
The Model-View-ViewModel (MVVM) pattern enforces a separation between three software layers — the XAML user interface, called the view, the underlying data, called the model, and an intermediary between the view and the model, called the viewmodel.
I will look
https://youtu.be/sxSk4sOszzs?feature=shared i found this but i need a static class and static vars
Devs School
YouTube
NET MAUI: How to bind a static property to a xaml control?
Get the full .NET MAUI Course: https://courses.devs.school/courses/net-maui
In this video, I am going to show you a way to use a static property on a control in your .NET MAUI projects.
you don't
like i said take this slow and learn everything from the beginning
did you read the article i sent?
do you understand what INotifyPropertyChanged is?
a bit
yes
what is it?
it notifies
it is used to make the user interface understand when it is being updated
yeah it sends a notification with the name of the property that was changed
notification in the form of an event
and things like the ui can subscribe to the event
and it can know what properties in a class that implements INPC change and when
so all you need to do is bind to a property that sends a notification when its changed
thats all there is to it
can you give an example of code
there are lots of examples here
i see but how to use static vars
like i have a class with game logic and i have a static vars to use them anywhere
you dont you make an instance of the class representing the game state
nooo i dont whant to
and how do i make it
you make it not static and create an instance
then you can implement INPC and bind
but how do i make it unique and accessable from other classes
you pass the instance you created to the other classes
usually when constructing them
but since you mentioned this is a game, then a singleton might be fine
or so ive heard about game dev
what can you give me some articles or something
there aren't any official docs
its pretty easy to look up
i found only dependency injection
oh i found
yeah i made a singleton
do you know what an interface is?
i made it like check this
private void BindText()
{
Binding bind = new Binding(); bind.Source = MainGame.GetInstance(); bind.Path = "light"; lightText.SetBinding(Label.TextProperty, bind); } and its working ye
Binding bind = new Binding(); bind.Source = MainGame.GetInstance(); bind.Path = "light"; lightText.SetBinding(Label.TextProperty, bind); } and its working ye
i really recommend learning the fundementals of c# on its own and then learning gui