Up
to this point we have been authoring workflows in .xaml, .cs or .vb files. These
files then get compiled into types that are included in the assembly of the
project and run by the workflow runtime.
While it might look like the format of the source files does not matter, .xaml files offer some distinct advantages over authoring workflows in C# or VB.
While it might look like the format of the source files does not matter, .xaml files offer some distinct advantages over authoring workflows in C# or VB.
- The Workflow Designer works with .xaml files only, workflows authored in C# or VB have no designer support.
- XAML can be loaded and run dynamically without compiling it into an assembly.
Dynamic
workflows provide some interesting possibilities for programs that want to
generate business logic or make a runtime decision on which business logic to
load and run.
From last article The CodeActivity we continue our project, we will modify the HellowWorkflow program to load and run the SayHello.xaml file. Then
we will modify the text of SayHello.xaml and observe the change in the message
the next time we run the application.
- We need to tell Visual Studio to treat SayHello.xaml as content that must be
deployed rather than code that must be built. To do this.
Previously
our class was compiled into a type. To invoke a workflow from a .xaml file we will need to use ActivityXamlServices
to load the .xaml file into memory and create an instance of System.Activities.DynamicActivity that WorkflowInvoker can invoke. Keep in
mind that any assemblies that our .xaml file references must be available when
the workflow is invoked.
- Add
the following namespace directives to the file.C#using System.Activities.XamlIntegration;Visual BasicImports System.Activities.XamlIntegration
- Modify
program.cs to use ActivityXamlServices
and also add a call to Console.ReadKey
this will make it easier to see what is happening with our app when running it
from Windows Explorer. To do this, replace the Main method implementation with the following code:C#static void Main(string[] args){WorkflowInvoker.Invoke(ActivityXamlServices.Load("SayHello.xaml"));Console.ReadKey(false);}Visual BasicShared Sub Main()WorkflowInvoker.Invoke(ActivityXamlServices.Load("SayHello.xaml"))Console.ReadKey(False)End Sub
- Press CTRL+F5 to run the workflow without debugging. The application should run in a console window and print the message “Hello Workflow 4".
- Navigate
to the Bin\Debug directory under
your project folder and locate SayHello.xaml.Locate the SayHello.xaml file in the project’s Bin\Debug directory
- In
Notepad change the Text property of the WriteLine activity to “Hello Workflow 4
XAML” then save and close it.Change the Text property in Notepad
- In
Windows Explorer run HelloWorkflow.exe and it should now say “Hello Workflow 4
from XAML”. Press any key to exit.HelloWorkflow.exe showing a new message from the .xaml file
No comments:
Post a Comment