Skip to content

API reference Obsidian.API.Client.Razor

ModComponent class

The base of a mod's Razor components, in place of Blazor's ComponentBase, with the same lifecycle: put @inherits ModComponent in the mod's _Imports.razor. The client renders mods' components only if they inherit it.

public abstract class ModComponent : IComponent, IHandleEvent, IHandleAfterRender, IDisposable

In Obsidian.API.Client.

Remarks

The client runs the component's code, its lifecycle methods, its rendering and its event handlers, on the game's thread inside a call into its mod, as it does a ModScreen's: once the mod unloads it no longer runs, and if it throws, the screen it's on is logged and closed. An async lifecycle method or handler stays inside the call until its task completes.

For that, the component can't implement Blazor's component interfaces (IComponent, IHandleEvent, IHandleAfterRender, IDisposable, IAsyncDisposable) itself: clean up in ModComponent.OnDispose rather than with @implements IDisposable. Mod components cannot use [Inject] or @inject: Blazor invokes those setters outside the guarded lifecycle. Obtain the screen's context through its ScreenContext cascading parameter instead.

Constructors

protected ModComponent();

Methods

BuildRenderTree(RenderTreeBuilder)

protected virtual void BuildRenderTree(RenderTreeBuilder builder);

Renders the component: what its Razor markup compiles to.

DispatchExceptionAsync(Exception)

protected Task DispatchExceptionAsync(Exception exception);

Fails the component with an exception from work outside its lifecycle, like a background task, which closes its screen as if the component had thrown it.

InvokeAsync(Action)

protected Task InvokeAsync(Action work);

Runs work on the game's thread, inside a call into the mod, like from another thread.

InvokeAsync(Func<Task>)

protected Task InvokeAsync(Func<Task> work);

Runs work on the game's thread, inside a call into the mod, like from another thread.

OnAfterRender(bool)

protected virtual void OnAfterRender(bool firstRender);

Runs after each render; firstRender is true the first time.

OnAfterRenderAsync(bool)

protected virtual Task OnAfterRenderAsync(bool firstRender);

Runs after ModComponent.OnAfterRender.

OnDispose()

protected virtual void OnDispose();

Runs as the component is removed, like when its screen closes, unless its mod is unloading.

OnInitialized()

protected virtual void OnInitialized();

Runs once, when the component first gets its parameters, like Blazor's OnInitialized.

OnInitializedAsync()

protected virtual Task OnInitializedAsync();

Runs once after ModComponent.OnInitialized; while its task runs, the component renders as it is.

OnParametersSet()

protected virtual void OnParametersSet();

Runs each time the component gets its parameters, before it renders.

OnParametersSetAsync()

protected virtual Task OnParametersSetAsync();

Runs after ModComponent.OnParametersSet; the component renders again when its task completes.

ShouldRender()

protected virtual bool ShouldRender();

Whether the component renders when asked to, after its first render.

StateHasChanged()

protected void StateHasChanged();

Renders the component again, like after changing a field from a payload handler (through ModComponent.InvokeAsync, on the game's thread).

On this page