RunicViewHost class

Namespace Runic.Application.Views.Wpf in Runic.Application.Views.Wpf 0.23.0

public class RunicViewHost : Decorator

Inherits or implements Decorator.

Presents an existing model through its generated Runic web View, inside the WPF visual tree.

Remarks

The host opens the web presentation when it is loaded with a RunicViewHost.ViewModel and RunicViewHost.Services, and closes it when it is unloaded or the ViewModel is cleared. A change of model, services or content closes the current presentation and opens a fresh one. Changes run one at a time; a newer change cancels an open in progress. Closing releases the Bridge session first, draining its accepted operations, then the Desktop surface and the host's own Desktop host, then the native child.

Each host starts and disposes its own DesktopHost, with its own listener, unless RunicViewHost.Desktop names one that the application shares between hosts.

When the presentation can't open (no WebView2 Runtime, no registered Bridge, missing frontend), the host shows RunicViewHost.Fallback with the same model as its FrameworkElement.DataContext, sets RunicViewHost.State to RunicViewHostState.Failed and keeps the failure in RunicViewHost.Error.

The host borrows the model, services and model context: it never creates or disposes a ViewModel, a DI scope or a navigation entry. It manages its own Decorator.Child: setting Child is not supported; set the fallback as its content instead. Derive from the host only to style it.

Screen readers see the web View as a pane that contains the page's document. Set AutomationProperties.Name (and HelpText) on the host to name that pane; the host passes them to its RunicWebView.

Constructors

RunicViewHost

public RunicViewHost()

Creates a host.

Fields

ContentRootProperty

public static readonly DependencyProperty ContentRootProperty

Identifies the RunicViewHost.ContentRoot dependency property.

ContentSecurityPolicyProperty

public static readonly DependencyProperty ContentSecurityPolicyProperty

Identifies the RunicViewHost.ContentSecurityPolicy dependency property.

DesktopProperty

public static readonly DependencyProperty DesktopProperty

Identifies the RunicViewHost.Desktop dependency property.

ErrorProperty

public static readonly DependencyProperty ErrorProperty

Identifies the RunicViewHost.Error dependency property.

FallbackProperty

public static readonly DependencyProperty FallbackProperty

Identifies the RunicViewHost.Fallback dependency property.

ModelContextProperty

public static readonly DependencyProperty ModelContextProperty

Identifies the RunicViewHost.ModelContext dependency property.

ServicesProperty

public static readonly DependencyProperty ServicesProperty

Identifies the Services attached dependency property, which descendants inherit.

Remarks

It is ViewHost.ServicesProperty of Runic.Navigation.Wpf: one provider set on a window with RunicViewHost.SetServices or ViewHost.SetServices reaches every RunicViewHost and navigation ViewHost in it.

StartPageProperty

public static readonly DependencyProperty StartPageProperty

Identifies the RunicViewHost.StartPage dependency property.

StateProperty

public static readonly DependencyProperty StateProperty

Identifies the RunicViewHost.State dependency property.

SurfaceOptionsProperty

public static readonly DependencyProperty SurfaceOptionsProperty

Identifies the RunicViewHost.SurfaceOptions dependency property.

ViewModelProperty

public static readonly DependencyProperty ViewModelProperty

Identifies the RunicViewHost.ViewModel dependency property.

WebContentProperty

public static readonly DependencyProperty WebContentProperty

Identifies the RunicViewHost.WebContent dependency property.

Properties

ContentRoot

public string? ContentRoot { get; set; }

Gets or sets the built frontend's directory, relative to the application's base directory. null, the default, is the build's web root: www, or the directory that the build property RunicApplicationFrontendWebRoot names; with RunicApplicationFrontendDelivery=Embedded, the frontend archive the build embedded in the application instead.

ContentSecurityPolicy

public string? ContentSecurityPolicy { get; set; }

Gets or sets the Content-Security-Policy that the presentation's surface sends with its HTML, or null, the default, for none (DesktopSurfaceOptions.ContentSecurityPolicy).

Remarks

It applies to the default surface options and to RunicViewHost.SurfaceOptions that set no policy of their own, as a header of its own next to the surface's frame-ancestors policy. The Bridge needs 'self' scripts and connections and no 'unsafe-eval'; only DesktopSurface.ExecuteJavaScriptAsync, which evaluates a string, needs it. A value with a line break or another control character is rejected. A change reopens the presentation.

Desktop

public DesktopHost? Desktop { get; set; }

Gets or sets an application-owned Desktop host to create the presentation's surface on, or null, the default, for a Desktop host of this host's own.

Remarks

By default each host starts a DesktopHost, with its own listener, when it opens and disposes it when it closes. Set one host on every RunicViewHost, directly or through a Style, to serve them all from one listener: create it once, for example with await DesktopHost.StartAsync(new DesktopHostOptions { LoggerFactory = loggerFactory }), and dispose it after the hosts that use it have unloaded. Each host creates its own surface on it and releases only that surface when it closes; it never disposes the Desktop host. A Desktop host disposed before a host opens makes the open fail: RunicViewHost.State becomes RunicViewHostState.Failed and RunicViewHost.Fallback shows.

Each host still presents through its own RunicWebView: the Desktop host's DesktopHostOptions.WindowHostFactory is not used for these surfaces. A change reopens the presentation.

Error

public Exception? Error { get; }

Gets why the last open failed, while RunicViewHost.State is RunicViewHostState.Failed.

Fallback

public object? Fallback { get; set; }

Gets or sets the content shown when the web presentation fails to open, such as the native View for the model.

ModelContext

public IRunicModelContext? ModelContext { get; set; }

Gets or sets the model context the Bridge uses, or null for the one registered in RunicViewHost.Services.

Remarks

Supply the dispatcher context the navigator uses when navigation and the web View present the same model.

Services

public IServiceProvider? Services { get; set; }

Gets or sets the application's service provider, with the generated Bridges registered.

Remarks

Inherited: set it once on the window with RunicViewHost.SetServices, or on the host from a View that received its provider. The host never disposes it; keep it alive until the host has closed.

StartPage

public string? StartPage { get; set; }

Gets or sets the page to open, relative to RunicViewHost.ContentRoot, or null for index.html; the same as RunicWindowOptions.StartPage of a Runic Window.

State

public RunicViewHostState State { get; }

Gets the presentation state.

Surface

public DesktopSurface? Surface { get; }

Gets the Desktop surface of the open presentation, or null.

SurfaceOptions

public DesktopSurfaceOptions? SurfaceOptions { get; set; }

Gets or sets the complete surface options; when set, RunicViewHost.ContentRoot and RunicViewHost.StartPage are ignored.

ViewModel

public object? ViewModel { get; set; }

Gets or sets the existing ViewModel to present, usually {Binding}.

Remarks

A generated Bridge must be registered for its class or a base class, through AddRunicViews().

WebContent

public DesktopWebContentPolicy? WebContent { get; set; }

Gets or sets what the page may do beyond the application's origins: external navigation, downloads and developer tools. null, the default, is DesktopWebContentPolicy.Default.

Remarks

The same as DesktopWindowOptions.WebContent of a Desktop window. A change reopens the presentation.

Methods

GetServices

public static IServiceProvider? GetServices(DependencyObject element)

Gets the provider set on element for the hosts it contains.

ReloadAsync

public Task ReloadAsync(CancellationToken cancellationToken = default)

Closes the presentation and opens a fresh session for the same model, and returns when it has settled.

Parameters

cancellationToken

Stops the wait; the reload goes on, as RunicViewHost.WaitForPresentationAsync describes.

Remarks

Also retries after a failure. The model, its state and its navigation entry stay.

SetServices

public static void SetServices(DependencyObject element, IServiceProvider? services)

Sets the provider for every host element contains, such as a window.

WaitForPresentationAsync

public Task WaitForPresentationAsync(CancellationToken cancellationToken = default)

Returns when no presentation change is pending: the presentation is open, failed, or closed and released.

Parameters

cancellationToken

Stops the wait without cancelling the presentation change.

Events

StateChanged

public event EventHandler? StateChanged

Raised on the UI thread when RunicViewHost.State changes.