Just to mention that you can invoke an instance .Net method by utilizing Blazor JavaScript interop, have a look at the following MS documentation.
But to answer your question, as Robert pointed in a comment, using the observer pattern or a simple Event
can satisfy your need:
static class MyStaticClass
{
public static EventHandler<string> MyPropertyChanged;
private static string _myProperty;
public static string MyProperty
{
get => _myProperty;
set
{
if (_myProperty != value)
{
_myProperty = value;
MyPropertyChanged?.Invoke(typeof(MyStaticClass), _myProperty);
}
}
}
}
And then you can subscribe to this event in your non static class:
public class MyClass
{
public MyClass()
{
MyStaticClass.MyPropertyChanged += MyPropertyChanged;
}
private void MyPropertyChanged(object sender, string e)
{
Console.WriteLine($"Changed value is {e}");
}
}
But the problem with subscribing to a static class event will be the potential memory leak if your MyClass
instance does not live throughout the app's life cycle, while the static event is. Have a look at Jon Skeet's explanation on this. You can try unsubscribing from the event when you are done with the MyClass
instance. Additionally, for a Blazor component, have a look at Component disposal with IDisposable and IAsyncDisposable where you might be able to unsubscribe in the component disposal. You can also try implementing Weak Event Pattern to overcome this issue.