C
C#5mo ago
Sergey5588

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
sibber
sibber5mo ago
why doesnt it suit you? its the standard way of doing it
Sergey5588
Sergey5588OP5mo ago
I Googled and did not understand how to do this, as I understood, you can only bind two properties of different objects
sibber
sibber5mo ago
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
sibber
sibber5mo ago
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.
Sergey5588
Sergey5588OP5mo ago
I will look
Sergey5588
Sergey5588OP5mo ago
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.
sibber
sibber5mo ago
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?
Sergey5588
Sergey5588OP5mo ago
a bit yes
sibber
sibber5mo ago
what is it?
Sergey5588
Sergey5588OP5mo ago
it notifies it is used to make the user interface understand when it is being updated
sibber
sibber5mo ago
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
Sergey5588
Sergey5588OP5mo ago
can you give an example of code
sibber
sibber5mo ago
there are lots of examples here
Sergey5588
Sergey5588OP5mo ago
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
sibber
sibber5mo ago
you dont you make an instance of the class representing the game state
Sergey5588
Sergey5588OP5mo ago
nooo i dont whant to and how do i make it
sibber
sibber5mo ago
you make it not static and create an instance then you can implement INPC and bind
Sergey5588
Sergey5588OP5mo ago
but how do i make it unique and accessable from other classes
sibber
sibber5mo ago
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
Sergey5588
Sergey5588OP5mo ago
what can you give me some articles or something
sibber
sibber5mo ago
there aren't any official docs its pretty easy to look up
Sergey5588
Sergey5588OP5mo ago
i found only dependency injection oh i found yeah i made a singleton
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace IdleLight
{
[AddINotifyPropertyChangedInterface]
public partial class MainGame
{


private static MainGame _instance;
private MainGame() { }

public static MainGame GetInstance()
{
if(_instance == null)
{
_instance = new MainGame();
}


return _instance;

}






public int light { get; set; } = 0;
public int lightPerTick { get; set; } = 1;



}




}
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace IdleLight
{
[AddINotifyPropertyChangedInterface]
public partial class MainGame
{


private static MainGame _instance;
private MainGame() { }

public static MainGame GetInstance()
{
if(_instance == null)
{
_instance = new MainGame();
}


return _instance;

}






public int light { get; set; } = 0;
public int lightPerTick { get; set; } = 1;



}




}
sibber
sibber5mo ago
do you know what an interface is?
Sergey5588
Sergey5588OP4mo ago
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
sibber
sibber4mo ago
i really recommend learning the fundementals of c# on its own and then learning gui
Want results from more Discord servers?
Add your server