Windows Presentation Framework guidelines¶
See WPF for an introduction to the package. See WPF for a full walkthrough, built around a university grade book. This page lists the WPF-specific practices worth calling out on their own.
- View models inherit from
ReactiveObject. - Use
IActivatableViewModelandWhenActivatedfor lifecycle. See When Activated. - Keep every subscription disposed. See Cleaning up subscriptions and Disposables.
- Convert WPF events to streams instead of attaching event handlers. The extension methods in
ReactiveUI.Primitives.ObservableEventsturn an event into anIObservable<T>. See Events and ObservableEvents. - A window, page or control that needs a view model derives from
ReactiveWindow<TViewModel>,ReactivePage<TViewModel>orReactiveUserControl<TViewModel>. Each of these implementsIViewFor<TViewModel>with aViewModeldependency property. - Host a routed page stack with
RoutedViewHost. Host a single view model withViewModelViewHost. - Starting WPF from something other than its own
Applicationentry point needs a scheduler set by hand. A console host that creates the WPF application on its own thread has no dispatcher yet when the process starts. Once that thread's dispatcher exists, setRxSchedulers.MainThreadSchedulerto a dispatcher-backed sequencer. Do this before anything schedules work onto it.WithWpfScheduler(part ofWithWpf) needs that same dispatcher to already exist. - A control bound without a source-generated
BindusesBindWithValidationinstead. This applies to a control whose target is resolved by name at run time.