You can host model-defined WECPOF user interfaces in a WPF runtime application when you want to deliver a desktop application built from your MDriven model, ViewModels, and Actions.
WECPOF is the prototyping engine and runtime assembly used to execute model-defined ViewModel user interfaces and Actions. This page shows how to place a WECPOF window environment and its action menu inside your own WPF Window.
Before you start
Prepare an MDriven Framework project before adding the WPF host:
- Create an ECO project from the Visual Studio project templates.
- Create your model and ViewModels, then run Update Code.
- Configure the persistence mapper that your application will use. Configure remote persistence and synchronization too, if your application requires them.
- Create or evolve the database, or derive the model from an existing database by using the reverse-derive options.
- Add a WPF Window to the application. Use a WPF Page instead when building a browser application.
- Add a reference to
WECPOFLogicin the WPF project.
For a migration walkthrough that starts with a model created in MDriven Designer, see Documentation:Moving your work from MDriven Designer to MDriven Framework.
Choose the host approach
| Approach | Use it when | Setup |
|---|---|---|
WECPOFWindowEnvironment with MenuHandling
|
You want to control where WECPOF windows appear, where the generated action menu appears, and how styles are applied. | Follow the steps on this page. |
WECPOFTabBased
|
You want the WPF application to initially behave like the MDriven Designer prototyper using the tab-based host. | Follow Documentation:Moving your work from MDriven Designer to MDriven Framework. |
Add the WECPOF host to the WPF window
Add the WECPOFLogic XML namespace, a WPF Menu, and a WECPOFWindowEnvironment to the window XAML. The Menu is initially empty; MenuHandling adds menu items for the model Actions at runtime.
The WECPOFWindowEnvironment is the area in which WECPOF hosts the windows opened by your ViewModel Actions.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wecpof="clr-namespace:WECPOFLogic;assembly=WECPOFLogic"
Title="Window1"
Height="314"
Width="354">
<Window.Resources>
<LinearGradientBrush x:Key="WECPOFWinBackgroundBrush"
EndPoint="0,1"
StartPoint="0,0">
<GradientStop Color="#CCC" Offset="0.0" />
<GradientStop Color="#EEE" Offset="1.0" />
</LinearGradientBrush>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="22" />
<RowDefinition />
</Grid.RowDefinitions>
<Menu Grid.Row="0" Name="MainMenu" />
<wecpof:WECPOFWindowEnvironment x:Name="WecpofWinEnv"
Grid.Row="1" />
</Grid>
</Window>
Initialize WECPOF at runtime
Create and activate the application's EcoSpace, enable the WPF UI dequeuer, load the embedded ViewModel definitions, and connect the generated Actions to the WPF menu and window environment.
public partial class Window1 : Window
{
EcoSpace _ecospace;
MenuHandling _MenuHandling;
public Window1()
{
InitializeComponent();
// Create the EcoSpace, or use an EcoSpace that the application already owns.
_ecospace = new EcoProject1.EcoProject1EcoSpace();
_ecospace.Active = true;
// Start the ECO UI dequeuer. Without this, WECPOF UI will not appear.
WPFDequeuer.Active = true;
// Load ViewModel definitions embedded in the application resources.
ViewModelDefinitionsInApplication.Init(_ecospace);
// Create the component that turns model Actions into WPF menu items.
_MenuHandling = new MenuHandling();
// Locate style dictionaries and apply a selected style to this Window.
WecpofWinEnv.PathToStyles = Directory.GetCurrentDirectory() + "\\Styles";
WecpofWinEnv.ResourceDictionaryTargetForStyles(this);
// Populate MainMenu from the runtime Actions and host opened WECPOF windows
// in WecpofWinEnv. The Window supplies the context for shortcut keys.
_MenuHandling.InitMainMenu(
MainMenu,
ViewModelDefinitionsInApplication.GetActionsRuntime(),
_ecospace,
WecpofWinEnv,
this);
WecpofWinEnv.InstallMenuHandling(_MenuHandling);
// Handle the model's Exit framework action by closing the WPF host window.
MenuHandling.OnExit += new EventHandler<EventArgs>(MenuHandling_OnExit);
}
void MenuHandling_OnExit(object sender, EventArgs e)
{
Close();
}
}
What the initialization does
| Code or component | Purpose | Result in the application |
|---|---|---|
_ecospace.Active = true
|
Activates the EcoSpace that owns the runtime model objects and persistence configuration. | The application can work with its model through the active EcoSpace. |
WPFDequeuer.Active = true
|
Starts the ECO UI dequeuer for WPF. | WECPOF can show its UI. If this is not enabled, nothing is shown. |
ViewModelDefinitionsInApplication.Init(_ecospace)
|
Loads ViewModel definitions from embedded application resources. | WECPOF can use the ViewModels defined in the model. |
InitMainMenu(...)
|
Gives MenuHandling the WPF menu, runtime Actions, EcoSpace, window host, and shortcut-key context.
|
Actions defined in the model become menu items in MainMenu.
|
PathToStyles
|
Identifies the Styles directory from the application's current directory.
|
WECPOF can find its style definitions. |
ResourceDictionaryTargetForStyles(this)
|
Identifies the WPF resource target to which the chosen WECPOF style is applied. | The selected style is applied to the host window. |
InstallMenuHandling(...)
|
Connects the WECPOF window environment to the menu handler. | WECPOF windows and menu behavior use the same action handling. |
Define behavior in the model
The WPF code hosts and connects the runtime UI; you define the user-facing behavior in the model.
For example, an Action can bring up a ViewModel-defined UI. If you configure that Action as modal, WECPOF adds OK and Cancel buttons. When the user selects OK, the Action's ExpressionAfterModalOk can read values from the closing ViewModel through variables prefixed with vModalResult_. See Training:What an Action can do for the modal seek/pick/assign pattern and framework Action behavior.
An Action configured as the framework Exit action raises MenuHandling.OnExit in this host. The event handler above closes the WPF window.
Style location
The sample sets PathToStyles to:
<application current directory>\Styles
Ensure that the application's current directory and the Styles directory match this setting. If your application stores styles elsewhere, set PathToStyles to that location before initializing the menu handling.
Example runtime flow
Assume the model contains a global Action named Find Customer that brings up a customer seeker ViewModel.
- The application starts and activates its EcoSpace.
- The application loads the embedded ViewModel definitions.
InitMainMenuadds Find Customer toMainMenu.- The user selects Find Customer.
- WECPOF opens the seeker ViewModel UI inside
WecpofWinEnv. - If the model also contains an Exit framework Action, selecting it raises
OnExitand the host window closes.
Keep model-specific UI behavior, such as search presentation and grid-entry Actions, in the relevant ViewModels and Actions rather than in this WPF host code. For examples of grid actions and other WECPOF UI behavior, see Documentation:WECPOF Goodies.
