Port a WPF view to a web page
On this page
- Text boxes that write when they lose focus
- Radio buttons with a converter
- INotifyDataErrorInfo and Validation.Errors
- IDataErrorInfo
- Buttons bound to commands
- Dialogs that answer an interaction
- ViewModels that keep WPF-only members
- A fake ViewModel for view tests
- ContentRendered and a Closing handler
- A double-clicked Next
- A ContentControl with a DataTemplate per ViewModel
- Bindings through nested ViewModels
- A view that ran in the application's process
- A ViewModel library that several applications share
- Next steps
Adopt Runic incrementally in WPF and
Host Runic Views in a WPF app cover adoption, the project
layout, RunicViewHost and close guards. This guide covers the view itself:
where each habit of a WPF view goes when the view becomes a web page that
presents the same ViewModel.
Each section pairs a WPF pattern with the Runic feature that replaces it: a
minimal before and after, what behaves differently, and the package README
section with the details. The examples are Angular; each section names the
React, Vue and Svelte equivalent. This guide follows Application SDK 0.24.0.
Three applications ported from WPF to Angular wrote their own version of each of these features, because nothing linked the WPF pattern to the Runic one. Check this list before you write a queue, a guard or a test double by hand.
Text boxes that write when they lose focus #
WPF binds TextBox.Text with UpdateSourceTrigger=LostFocus by default, so
the setter runs once, when the user leaves the box:
<TextBox Text="{Binding Title}" />
<CheckBox IsChecked="{Binding Pinned}" />
Bind the member with injectField and commit: "change", and wire the input
with [runicField] from @runic-artifex/angular/forms:
@Component({
selector: 'editor-page',
imports: [RunicFieldDirective],
template: `<input [runicField]="title"> <input type="checkbox" [runicField]="pinned">`,
})
export class EditorPage {
readonly page = input.required<EditorPageReference>();
readonly editor = injectView(this.page);
readonly title = injectField(this.editor, EditorFields.title, { commit: 'change' });
readonly pinned = injectField(this.editor, EditorFields.pinned, { commit: 'change' });
}
What differs:
- The draft is written on
commit(), which blur calls, so the setter runs once, as withLostFocus. There is no timer, so pass nodelay. - A checkbox, radio or
<select>writes each choice at once, as WPF'sPropertyChangedtrigger does for those controls. - A form's
submit()andcommit(),commitAllFields(), the flush before a window close or a departure, and destroying the component also write the draft. Write a Save button withinjectForm(...).submit(...), which writes every field before its command, instead of a queue of pending writes. - While the user types, the field shows the draft, not the View's value, so an older reply never replaces newer text. A directive that keeps an input's value while it has focus is not needed.
commit: "pause"writes after a pause in typing as well as on blur, and the default,commit: "input", writes each input.injectTextFieldhas no commit mode; useinjectField.
React, Vue and Svelte: useField(view, field, { commit: "change" }) with
inputProps, v-model or attach. Details:
Angular fields,
the field controller's options
and When fields write.
Radio buttons with a converter #
A WPF radio group binds each RadioButton to the same enum property through a
converter:
<RadioButton Content="Small" IsChecked="{Binding Size, Converter={StaticResource EnumToBool}, ConverterParameter=Small}" />
<RadioButton Content="Large" IsChecked="{Binding Size, Converter={StaticResource EnumToBool}, ConverterParameter=Large}" />
Bind one field and put [runicField] on each radio. The radio's value is
the enum member's name:
@Component({
selector: 'editor-page',
imports: [RunicFieldDirective],
template: `
@for (option of sizes; track option) {
<label><input type="radio" name="size" [value]="option" [runicField]="size"> {{ option }}</label>
}`,
})
export class EditorPage {
readonly page = input.required<EditorPageReference>();
readonly editor = injectView(this.page);
readonly size = injectField(this.editor, EditorFields.size);
readonly sizes = EditorFields.size.choices ?? [];
}
What differs:
- No converter: the field's
choicekind maps the enum to its member names, andchoiceslists them in declaration order. - Each radio keeps its own
valueand is checked while that is the field's value. Only the radio that became checked writes, also withcommit: "change".
React: spread size.radioProps(option) on each radio. Vue: v-model on each
radio. Svelte: {@attach size.attach} on each radio. Without a framework,
bindField takes the radios of a group together. Details:
[runicField] and Reactive Forms
and the field controller.
INotifyDataErrorInfo and Validation.Errors #
A WPF view shows the errors that the ViewModel's INotifyDataErrorInfo
reports through Validation.Errors:
<TextBox x:Name="TitleBox" Text="{Binding Title}" />
<TextBlock Text="{Binding (Validation.Errors)[0].ErrorContent, ElementName=TitleBox}" />
The Bridge reads the same INotifyDataErrorInfo. A field's errors signal
holds its member's messages:
<input [runicField]="title">
@for (message of title.errors(); track message) { <p role="alert">{{ message }}</p> }
What differs:
- The state has a
{member}Errorsarray for each member, such astitleErrors, andvalidation, with every error'spathandmessage, also for nested objects and list items. A lookup of messages by property name is not needed. [runicField]setsaria-invalidwhile the member has errors.injectForm(...).valid()is false while any of its fields has errors.- The Bridge runs no validation rules; it reports what the ViewModel's
GetErrorsreturns. A ViewModel that uses ReactiveUI.Validation is read through itsValidationContext.
React, Vue and Svelte: useField(...).errors. Details:
Structured validation
and Validation from .NET.
IDataErrorInfo #
The Bridge does not read IDataErrorInfo, so its errors never reach the page.
Implement INotifyDataErrorInfo instead, for example with CommunityToolkit's
ObservableValidator, or use ReactiveUI.Validation. Both are read as in the
section above. Details:
Structured validation.
Buttons bound to commands #
A WPF button binds a command, and CanExecute enables it:
<Button Command="{Binding RefreshCommand}" Content="Refresh" />
<Button Command="{Binding ArchiveCommand}" CommandParameter="{Binding Id}" Content="Archive" />
injectCommands(view) makes a handle for each command of the View:
@Component({
selector: 'orders-page',
template: `
<button (click)="commands.refresh.run()"
[disabled]="!orders.state()?.canRefresh || commands.refresh.pending()">Refresh</button>
<button (click)="commands.archive.run(row().id)">Archive</button>`,
})
export class OrdersPage {
readonly page = input.required<OrdersPageReference>();
readonly orders = injectView(this.page);
readonly commands = injectCommands(this.orders);
}
What differs:
CanExecuteof a command without a parameter is state,canRefresh; binddisabledto it yourself. A command with a parameter has the querycanArchive(id)on the client instead.- Each handle has its own
pending,errorandfailure.run()never rejects; a failed run ends inerror. Before the View connected it resolves toundefined. - The names and arguments come from the generated client, so setters,
canX(input),startXand commands that explicit publication left out are not offered. - Runs are independent. Don't queue commands so that each waits for the previous one to complete: a command whose completion waits for a later command then never completes.
React, Vue and Svelte: useCommands(view). Details:
Angular commands
and Command handles.
Dialogs that answer an interaction #
A WPF view answers a ReactiveUI Interaction in its code-behind, often with a
dialog:
this.WhenActivated(d => d(ViewModel!.ConfirmDiscard.RegisterHandler(context =>
{
var answer = MessageBox.Show($"Discard the changes to {context.Input.ItemName}?", "Discard", MessageBoxButton.YesNo);
context.SetOutput(answer == MessageBoxResult.Yes);
})));
A page that renders the dialog in its own template uses injectInteraction.
request() is the oldest open request, a handle { input, respond }:
@Component({
selector: 'edit-page',
template: `
@if (discard.request(); as request) {
<app-confirm [text]="request.input.itemName" (yes)="request.respond(true)" (no)="request.respond(false)" />
}`,
})
export class EditPage {
readonly page = input.required<EditPageReference>();
readonly view = injectView(this.page);
readonly discard = injectInteraction(this.view, 'confirmDiscard');
}
What differs:
- Each open request is its own handle, and its
respondanswers that request only. A second click, or an answer after .NET stopped waiting, is ignored, so it never answers the next request. - .NET can stop waiting: the command that asked was cancelled, the deadline
passed or the window closed. The request then ends and
request()moves on, so the dialog closes. - The handler exists only while the View is connected. A request before that is answered by the ViewModel's .NET fallback handler, or fails closed without one.
- To await a dialog service instead, register a handler with
client.interactions.<name>.handle(...).
React, Vue and Svelte: useInteraction(view, name). Details:
A dialog in the template
and Interactions as state.
ViewModels that keep WPF-only members #
A ported ViewModel still has the members its XAML bound to, some with WPF
types, while the page may read one computed record. Explicit publication
publishes only the members marked [RunicInclude]:
[assembly: RunicPublication(RunicPublicationMode.Explicit)]
public sealed partial class ImportResultViewModel : ObservableObject
{
[RunicInclude] public ResultPage Page => BuildPage();
// Kept for the native view and the tests; not published.
public ICollectionView Imported { get; }
[RelayCommand]
[property: RunicInclude]
private Task ExportAsync() => ...;
}
What differs:
- The generator never inspects a member that is left out, so a WPF-typed
member needs no
[RunicIgnore], and a member added later stays on .NET until someone marks it. - On a CommunityToolkit
[RelayCommand]method or[ObservableProperty]field, write[property: RunicInclude]. [RunicPublication]on a class wins over the assembly's, so one ViewModel can keep the default withRunicPublicationMode.Implicit.- Changing the mode changes the generated contract. Rebuild the .NET application and the frontend together.
The page is unchanged; only the generated client has fewer members. Details: Which members are published.
A fake ViewModel for view tests #
A WPF view can be tested with a fake ViewModel behind an interface. The ports
rebuilt that seam with injection tokens, wrapper components and hand-written
doubles that filled every …Errors: [] member.
The generator writes a typed mock next to each client, such as
editor.mock.ts with mockEditor:
beforeEach(() => {
bridge = installMockBridge(createMockBridge());
});
test('saving clears the dirty flag', async () => {
mockEditor(bridge, {
state: { title: 'Groceries', isDirty: true },
commands: { save: () => ({ isDirty: false }) },
});
// Render the component as in production; injectView connects to the mock.
});
What differs:
- The mock is typed from the generated client, so a renamed or retyped ViewModel member breaks the test at compile time.
- It fills
validation, every{member}Errors,can…(available) andis…Executing(idle), so a test states only the ViewModel's own state. - The component needs no test seam: the mock Bridge answers the generated client as the .NET Window does.
The mock is the same for every framework. Details: Testing and Typed mocks.
ContentRendered and a Closing handler #
A WPF view's code-behind knows when it rendered and when its window closes:
ContentRendered += (_, _) => viewModel.OnShown();
Closing += (_, e) => e.Cancel = !viewModel.CanClose();
The ports wrote a paint acknowledgement and a close handshake between the page
and .NET, about 700 lines of TypeScript and C#. Runic has both in the
presented/1 feature:
Page = result;
if (!await RunicPresentation.WaitForPresentedAsync(result, TimeSpan.FromSeconds(2)))
logger.LogDebug("No page showed the result in time.");
What differs:
WaitForPresentedAsynccompletes withtrueonce a page rendered the state that includes every change made before the call, and withfalseat the timeout. A hidden or minimized page reports after its next timer.- When the application closes the window, each page delivers its pending
states, renders them, acknowledges and reports the connection state
closed. The page writes nothing for this. - A Runic Window guards a user close with
GuardCloseandCanCloseAsync. A WPF window that hosts the page keepsGuardWindowCloseon itsNavigationHost. A Runic Window with a close drain stays open until its accepted work has ended.
The page side is the same for every framework. Details: Waiting until the page shows a state, Presented states and window close, Guarding a user close and Closing after the Window's work has drained.
A double-clicked Next #
In WPF the UI thread showed the next wizard page before it handled the next
click, so a double-clicked Next advanced once. A web page sends both clicks
before the next page arrives. Mark the command [RunicPresentationScoped] and
renew the presentation when the page changes:
[RelayCommand]
[RunicPresentationScoped]
private void Next()
{
Step++;
RunicPresentation.Renew(this);
}
The page is unchanged: commands.next.run().
What differs:
- The page sends the epoch of the state it showed when the user clicked. The
second Next was clicked on the first page, whose epoch
Renewended, so it does not run. - A superseded call resolves with the current state, without an error, as if it
had run.
injectCommandandinjectCommandsshow no error; a caller that must know compares the state it got. - A new page in a navigation region begins a new epoch without
Renew. CallRenewwhere the page changes without a new presentation, such as Next and Back. - Setters, checked writes and operation starts are not scoped.
The page side is the same for every framework. Details: Commands scoped to their page and Scoped commands.
A ContentControl with a DataTemplate per ViewModel #
A WPF content area picks a template by the ViewModel's type:
<ContentControl Content="{Binding Main.Current}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type vm:OrdersViewModel}"><local:OrdersView /></DataTemplate>
<DataTemplate DataType="{x:Type vm:SettingsViewModel}"><local:SettingsView /></DataTemplate>
</ContentControl.Resources>
</ContentControl>
On the web, ViewOutlet picks a component from a registry by the page
reference's kind. Narrow the slot's TContent to an interface that only that
slot's ViewModels implement, such as NavigationRegion<IMainPage>, so the
registry lists only them:
const pages = {
orders: OrdersPage,
settings: SettingsPage,
} satisfies ViewRegistry<ShellState['main']>;
// <runic-view-outlet [content]="shell.state()?.main" [registry]="pages" [missing]="missingView" />
// <ng-template #missingView let-page><p role="alert">{{ missingText(page.kind) }}</p></ng-template>
Cost. A content slot's union lists every presented ViewModel that is
assignable to TContent, so a broad TContent makes an exhaustive registry
impractical.
What differs:
ViewRegistrychecks at compile time that the registry has a component for every kind in the slot's union. A cast that skips the check hides a kind with no component.- A kind without a component renders an English alert in a development build;
a production build renders nothing and reports it. The
missingtemplate replaces that alert. - A
NavigationRegion<object>slot is errorRUNICBRIDGE008. A ViewModel that a slot admits but has no View is warningRUNICBRIDGE013. - ViewModels that publish the same members can share one component through a
[RunicSharedClient]interface andregistryEntries.
React, Vue and Svelte: ViewOutlet and their ViewRegistry. Details:
Content slots,
Angular outlet
and
Shared clients.
Bindings through nested ViewModels #
A WPF binding path walks through nested ViewModels:
<MenuItem Header="Open log" Command="{Binding Header.Menu.OpenLogCommand}" />
On the web, each presented View is its own connection. A child ViewModel with
a View of its own is a page reference in its parent's state, and the
component that renders it calls injectView for it. When the child has no page
of its own, forward its commands on the parent ViewModel instead:
public sealed partial class ShellViewModel : ObservableObject
{
[RelayCommand]
private void OpenLog() => Header.Menu.OpenLogCommand.Execute(null);
}
<button (click)="commands.openLog.run()">Open log</button>
Cost. Each nested View is one more connection, and it opens only after its parent's state has arrived.
What differs:
- A component per level, each with its own
injectViewand "not connected" state, follows from presenting each level as a View. Forwarding keeps the menu in the shell's one connection. - A forwarding command like the one above is always available. Give it a
CanExecuteof its own when the child's command can be unavailable. - Each
injectViewconnects its reference and releases it when the component goes away.
React, Vue and Svelte: useView per presented View. Details:
Connecting a View
and
Windows, Views, and composition.
A view that ran in the application's process #
A WPF view could not lose its ViewModel. A page can lose its connection to the
.NET host, and the ports polled isConnected() every second. Follow the
connection instead:
readonly connection = injectBridgeConnection({ lostAfter: 5_000 });
// @if (connection.state() === "lost") { <p role="alert">The application stopped responding.</p> }
// @if (connection.state() === "closed") { <p role="status">The application closed this window.</p> }
What differs:
- The state is
connecting,connected,reconnecting,lostorclosed.lostfollowslostAftermilliseconds without a connection and is not terminal: it becomesconnectedagain when the host comes back. closedmeans the application closed the window, so the page can say so instead of showing a lost connection.- Runic Desktop and CS-WebUI report a dropped connection within about 250 ms.
React, Vue and Svelte: useBridgeConnection. Details:
Angular connection state
and
Connection state.
A ViewModel library that several applications share #
A WPF solution can share a library of ViewModels and their XAML views between
applications. To share their web clients too, the library declares the Views
and generates their TypeScript into a frontend package that other frontends
import. The assembly that declares a ViewModel's Views writes its TypeScript.
dotnet new runic-app -n MyApp --library MyApp.Core creates the whole setup:
the application, the library, the library's frontend package and a JavaScript
workspace of both frontends.
<PropertyGroup>
<RunicApplicationFrontendClientLibrary>true</RunicApplicationFrontendClientLibrary>
<RunicApplicationFrontendDirectory>$(MSBuildProjectDirectory)/../shared-ui</RunicApplicationFrontendDirectory>
</PropertyGroup>
// In the library.
public sealed partial class AboutDialogView : RunicView<AboutDialogViewModel>;
// A component in the shared frontend package.
import type { AboutDialogClient } from './generated/runic-clients.js';
What differs:
- The generated directory gets
runic-clients.ts, which exports every client, shared client and named type andrunicClientContracts, andrunic-mocks.ts, which exports the mocks. The package exports them beside its components and lists@runic-artifex/viewsas a peer dependency. - A client library copies no frontend, and an application that references it
does not copy the library's frontend to its web root. The library's build
builds its package when the package has a
buildscript, copies the*.contract.d.tsfiles and their maps into a compiled package's output, and reports a missing export asRUNICBRIDGE088. - A client library without a View is
RUNICBRIDGE006. - An application maps the library next to its project reference and deletes
its own Views of those ViewModels (
RUNICBRIDGE080reports one it keeps):
<ItemGroup>
<ProjectReference Include="../SharedUi/SharedUi.csproj" />
<RunicApplicationFrontendClientModule Include="SharedUi" Specifier="@acme/shared-ui/generated" />
</ItemGroup>
- The application still generates the C# Bridges of the library's ViewModels
and registers the library's Views in
AddRunicViews(); register the ViewModels yourself, as any ViewModel. It writes no client, contract or mock for them: its own modules import their page functions and page references from the specifier, and the copies an earlier build generated are deleted. - Install the package as a workspace dependency (
"@acme/shared-ui": "workspace:*") or a published version, and dedupe@runic-artifex/viewsin Vite (resolve: { dedupe: ['@runic-artifex/views'] }) so the page loads one runtime.dotnet runic doctorchecks both (views-dedupe,client-modules). An edit to the package rebuilds the application's page. - A package generated from another build of the library fails the frontend's
type check with
TS2344inrunic-client-modules.ts, naming both contracts. Rebuild the library and its package. - A library ViewModel can present only ViewModels of the same library, and a
shared client's implementers need one owner (
RUNICBRIDGE081). A library that maps another library leaves the mapped modules out of its own barrel. - Mapping a client library has not been tried in a WPF application that hosts
Runic Views (
RunicViewHost) yet.
Details: Share ViewModels and their clients through a library, Library packages, A solution with a library in the template's README, and the shared-library-clients example.
Next steps #
- Host Runic Views in a WPF app presents the ported page in the WPF window, next to the native views.
- Forms and validation covers fields, forms and checked writes in all frameworks.
- Pages and navigation covers regions, guards and scoped commands.
- Testing frontends covers the typed mocks.