Discovering open source WPF components - Orc.WorkspaceManagement

In the discovering open source WPF components series, I will look into useful open source components that can be used to create WPF apps. This weeks component is Orc.WorkspaceManagement.

Workspaces are a great way to empower your end-users to customize the layout of the screen to their own good. There are a lot of different components that can be customized inside an application (some views need settings to be stored, others should be ignored, etc). Orc.WorkspaceManagement provides a nice wrapper around this so whenever the user wants to store a workspace, each component can be stored separately using providers.

Workspace Management in action

Retrieving a list of workspaces

var allWorkspaces = _workspaceManager.Workspaces;

foreach (var workspace in allWorkspaces)
    Console.WriteLine("Title: " + workspace.Title);
    Console.WriteLine("Can edit: " + workspace.CanEdit);
    Console.WriteLine("Can delete: " + workspace.CanDelete);

Retrieving the current workspace

Retrieving the currently selected workspace is very easy:

var workspace = _workspaceManager.Workspace;

To get a typed instance:

var workspace = _workspaceManager.GetWorkspace<MyWorkspace>();

Implementing providers

Providers are classes that do the hard work. They will take care of actually saving and loading the state of a specific workspace. Below is a provider that will store the IsMinimized state of a ribbon:

public class RibbonWorkspaceProvider : WorkspaceProviderBase
    private readonly Ribbon _ribbon;

    public RibbonWorkspaceProvider(Ribbon ribbon)
        Argument.IsNotNull(() => ribbon);

        _ribbon = ribbon;

    public override async Task ProvideInformationAsync(IWorkspace workspace)
        workspace.SetWorkspaceValue("Ribbon.IsMinimized", _ribbon.IsMinimized);

    public override async Task ApplyWorkspaceAsync(IWorkspace workspace)
        _ribbon.IsMinimized = workspace.GetWorkspaceValue("Ribbon.IsMinimized", false);

Storing and saving workspaces

The last step is storing workspaces and saving workspaces to disk. Since all the hard work is done by the IWorkspaceManager, it’s very easy to implement this final step.

Storing the workspace means the workspace manager will call all providers to gather their data so the UI state as it’s currently visible on the screen will be persisted into the current active workspace:

await _workspaceManager.StoreWorkspaceAsync();

To save all workspaces in the workspace manager to disk, use the following code: