This document mirrors the MVVM instructions for projects that use ReactiveUI.
The API surface is the same but the view models derive from ReactiveUI types and
commands are implemented using ReactiveCommand
. The sample project
DockReactiveUISample
demonstrates these concepts in a working application. For
interface details refer to the Dock API Reference and see
the Advanced Guide for more customization options.
Follow these instructions to create a minimal ReactiveUI based application using Dock.
Create a new Avalonia project
dotnet new avalonia.app -o MyDockApp
cd MyDockApp
Install the Dock packages
dotnet add package Dock.Avalonia
dotnet add package Dock.Model.ReactiveUI
Create a factory and view models
Derive from Dock.Model.ReactiveUI.Factory
and implement CreateLayout
. Your documents and tools should inherit from the ReactiveUI versions of Document
and Tool
.
public class DockFactory : Factory
{
public override IRootDock CreateLayout()
{
var doc = new DocumentViewModel { Id = "Doc1", Title = "Document" };
var tool = new Tool1ViewModel { Id = "Tool1", Title = "Tool1" };
var root = CreateRootDock();
root.VisibleDockables = CreateList<IDockable>(
new DocumentDock
{
VisibleDockables = CreateList<IDockable>(doc),
ActiveDockable = doc
},
new ToolDock
{
VisibleDockables = CreateList<IDockable>(tool),
ActiveDockable = tool
});
return root;
}
}
Initialize the layout using Reactive commands
_factory = new DockFactory();
Layout = _factory.CreateLayout();
_factory.InitLayout(Layout);
Add DockControl
to the main view
<DockControl x:Name="Dock" Layout="{Binding Layout}" />
Run the application
dotnet run
Install-Package Dock.Avalonia
Install-Package Dock.Model.ReactiveUI
Create a factory exactly as with the MVVM version. The DockFactory
in the ReactiveUI sample constructs the layout in the same way.
public override IRootDock CreateLayout()
{
var document1 = new DocumentViewModel { Id = "Document1", Title = "Doc1" };
var tool1 = new Tool1ViewModel { Id = "Tool1", Title = "Tool1" };
var root = CreateRootDock();
root.VisibleDockables = CreateList<IDockable>(
new DocumentDock
{
VisibleDockables = CreateList<IDockable>(document1),
ActiveDockable = document1
},
new ToolDock
{
VisibleDockables = CreateList<IDockable>(tool1),
ActiveDockable = tool1,
Alignment = Alignment.Left
}
);
return root;
}
The feature set matches the MVVM version. Methods like AddDockable
, MoveDockable
, PinDockable
or FloatDockable
are available from FactoryBase
.
// Example: create a command that opens a new document
OpenDocument = ReactiveCommand.Create(() =>
{
var doc = new DocumentViewModel { Id = Guid.NewGuid().ToString(), Title = "New" };
_factory.AddDockable(documentDock, doc);
_factory.SetActiveDockable(doc);
});
All the events shown in the MVVM guide are present here as well. Subscribe to them in the same way using ReactiveUI commands or observables.
Use the ReactiveUI sample as a template when building your own layouts. See the Advanced Guide for details on customizing factory methods and consult the Dock API Reference for the available interfaces.
For an overview of all guides see the documentation index.