Blazor Services
I dont know too much blazor, but I have a .cs file in a /Settings folder, in which I would like to access a service which is storing state. (The service called
State
is stored in /Appearance).
In razor components, i can just use @inject State state
, and then access and modify it using state.<prop>
How can i do the same inside of a standard c# file?4 Replies
(ping if replying)
you want to change a property in your State right ? 🤔 @DeathlyBower959
just add in any ctor your State to inject it and modify it
i think so yea
this is my ThemeState.cs
its a simple state
and i have added it to my services in MainWindow.xaml.cs
Im trying to access it in a .cs file, but im having some trouble
i tried this
but its always null
(i have to set a default value because i initialize this class to get default values)
If you initialize a class yourself like with
new MyClass()
you would need to provide the dependency yourself.
Automated DI only works if the framework creates something for your, like a razor component.
So you would need access to the underlying service provider and use something like public static T? GetService<T> (this IServiceProvider provider);