C
C#

Blazor Services

Blazor Services

DDeathlyBower95911/20/2023
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? (ping if replying)
Xx0rld11/20/2023
you want to change a property in your State right ? šŸ¤” @DeathlyBower959 just add in any ctor your State to inject it and modify it
DDeathlyBower95911/20/2023
i think so yea this is my ThemeState.cs
public class ThemeState
{
public CssBuilder ThemeCSS { get; set; }
public event Action OnChange;

public ThemeState() { ThemeCSS = CssBuilder.FromFile(ThemePath, ":root"); }

public static readonly string WWWRootPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "wwwroot");

public static readonly string ThemePath = Path.Combine(WWWRootPath, "css", "theme.css");

public void ApplyTheme(CssBuilder css)
{
ThemeCSS = css;
OnChange?.Invoke();
}

public void UpdateTheme(string property, string value)
{
ThemeCSS.SetProperty(property, value);
OnChange?.Invoke();
}
}
public class ThemeState
{
public CssBuilder ThemeCSS { get; set; }
public event Action OnChange;

public ThemeState() { ThemeCSS = CssBuilder.FromFile(ThemePath, ":root"); }

public static readonly string WWWRootPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "wwwroot");

public static readonly string ThemePath = Path.Combine(WWWRootPath, "css", "theme.css");

public void ApplyTheme(CssBuilder css)
{
ThemeCSS = css;
OnChange?.Invoke();
}

public void UpdateTheme(string property, string value)
{
ThemeCSS.SetProperty(property, value);
OnChange?.Invoke();
}
}
its a simple state and i have added it to my services in MainWindow.xaml.cs
services.AddSingleton<ThemeState>();

Resources.Add("services", services.BuildServiceProvider());
services.AddSingleton<ThemeState>();

Resources.Add("services", services.BuildServiceProvider());
Im trying to access it in a .cs file, but im having some trouble
private readonly ThemeState _state;

public MyClass(ThemeState state = null)
{
_state = state;
}
private readonly ThemeState _state;

public MyClass(ThemeState state = null)
{
_state = state;
}
i tried this but its always null (i have to set a default value because i initialize this class to get default values)
JJoschi11/21/2023
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);

Looking for more? Join the community!

C
C#

Blazor Services

Join Server
Want results from more Discord servers?
Add your server
Recommended Posts
How to optimize this?I have following: ```cs using System; namespace SquareCalculus { internal class FigureTriangle In VSCode, SDK not Recognized on ChromeOSHi. I just installed the .NET SDK and the Runtime by following the instructions for Ubuntu on the MiNito.AsyncEx vs DotNext.ThreadingWe're currently searching for a nice AsyncAutoResetEvent implementation. We found two suitable impleSequential BlinkersHello everyone! I bought these sequential blinkers that run off an stm32 blue pill board. the only pTrying to use Microsoft.Kiota namespace but not found when importingI am trying to use this method from the Microsoft.Kiota.Abstractions.Extensions namespace: ToFirstChWinUI3 Scheduler CalendarViewhello im trying to make a winui3 calendarview interactive calendar where I can predefine dates in myThe call is ambiguous between the following methods or properties: 'Thread.Thread(ThreadStart)' andi have to upload by filesHelp a noobie out with a simple hangmanHi! New to coding and I need some help to solve this problem. Shall I save the words to a list? AnyC# Image Resizing on Visual Studio CodeI've tried both ``newPic.SizeMode = PictureBoxSizeMode.StretchImage;`` and ``newPic.SizeMode = PictuI'm stuck,visual studio 22, I need help making my invaders/enemies move left to right then downI’m trying to get my invaders/enemies to move left to right then down like in the game space invaderāœ… Where should I store all the sensitive file on .NET project?Hello everyone, May I know where should I store all the credential files that needs to be used by .Unity CodeI have been trying to get my C# code to work in unity for about a week and I am officially lost. I'vHelp! Reporting service in dotnet Core?please don't mind my english, it is not that good. i have created 2 projects one is server -dotnetCatAPI Json deserialization issue.I'm having trouble getting my code to deserialize my json correctly. When I run my Program.cs I get āœ… Any idea why I cant use sendkey?Authorization in microservices archHello everyone, I'm quite new to the NET microservices arhitecture and right now I'm implementing a.āœ… I can't get my runButton button to work on a windows form app```cs if (e.KeyCode == Keys.Enter || (e.Modifiers == Keys.None && sender == runButton)) ``` I can sGit and Google Drivenew git user.. it seems to have your local repositories in a google drive controlled file structure ASP.NET CRUD MODALI created a MVC controller with actions (CRUD) and I would like to know, how to modify this pre geneError