WF4.0 基础篇 (十八) Flowchar
本节主要介绍WF4 中 Flowchart的使用
本文例子下载:
https://files.cnblogs.com/foundation/FlowcharSample.rar
本文例子说明
Flowchart 说明
Flowchart
类名 | System.Activities.Statements.Flowchart |
文件 | System.Activities.dll |
结构说明 | 继承 NativeActivity 是一个 sealed类 override 了 [CacheMetadata方法] 与 [Execute方法] [Variables]属性 的类型为[ Collection<Variable>] [StartNode]属性 的类型为[FlowNode] [Nodes]属性 的类型为[Collection<FlowNode>] |
功能说明 |
FlowDecision
类名 | System.Activities.Statements.FlowDecision |
文件 | System.Activities.dll |
结构说明 | 继承 FlowNode 是一个 sealed类 [Condition]属性 的类型为[Activity<bool>] [False]属性 的类型为[FlowNode] [True]属性 的类型为[FlowNode] |
功能说明 |
FlowSwitch
类名 | System.Activities.Statements.FlowSwitch |
文件 | System.Activities.dll |
结构说明 | 继承 FlowNode 是一个 sealed类 [Expression]属性 的类型为[Activity<object>] [Default]属性 的类型为[FlowNode] [Cases]属性 的类型为[IDictionary<object, FlowNode>] |
功能说明 |
FlowStep
类名 | System.Activities.Statements.FlowStep |
文件 | System.Activities.dll |
结构说明 | 继承 FlowNode 是一个 sealed类 [Action]属性 的类型为[Activity] [Next]属性 的类型为[FlowNode] |
功能说明 |
例
Flowchart基本使用
工作流 | |
宿主 | //===================================================
WorkflowApplication instance = null;
private void button_triggering_Click(object sender, RoutedEventArgs e) { string bookName = textBox_bookmark.Text; string inputValue = textBox_value.Text;
if (instance != null) { if (instance.GetBookmarks().Count(p => p.BookmarkName == bookName) == 1) { instance.ResumeBookmark(bookName, inputValue); } else { foreach (var v in instance.GetBookmarks()) { System.Console.WriteLine("-------请从下面选项中选择一BookmarkName---------------------------"); System.Console.WriteLine("BookmarkName:{0}:,OwnerDisplayName:{1}", v.BookmarkName, v.OwnerDisplayName); System.Console.WriteLine("================================"); } } } else { MessageBox.Show("没有创建实例"); }
}
void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { instance = null; System.Console.WriteLine("workflowCompleted:{0}", e.CompletionState.ToString()); }
void aborted(WorkflowApplicationAbortedEventArgs e) { instance = null; System.Console.WriteLine("aborted ,Reason:{0}", e.Reason.Message); }
UnhandledExceptionAction unhandledExceptionl(WorkflowApplicationUnhandledExceptionEventArgs e) { System.Console.WriteLine("unhandledException:{0}", e.UnhandledException.Message); return UnhandledExceptionAction.Cancel; } void workflowIdel(WorkflowApplicationIdleEventArgs e) { System.Console.WriteLine("Idle:{0}", e.InstanceId);
System.Console.WriteLine("--------BookmarkName---------------------------"); foreach (var item in e.Bookmarks) { System.Console.WriteLine("{0}", item.BookmarkName); } System.Console.WriteLine("================================"); } //==================================================
private void button_baseFlowchartWorkflow_Click(object sender, RoutedEventArgs e) { instance = new WorkflowApplication(new FlowcharLibrary.baseFlowchartWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted); instance.OnUnhandledException = unhandledExceptionl; instance.Aborted = aborted; instance.Idle = workflowIdel; instance.Run();
} |
结果 |
FlowDecision 例子
工作流 | |
bookmark | public sealed class bookmark<T> : NativeActivity<T> { public InArgument<string> bookmarkName { get; set; }
protected override bool CanInduceIdle { get { return true; } } protected override void Execute(NativeActivityContext context) { string bookmark = context.GetValue(bookmarkName); context.CreateBookmark(bookmark, new BookmarkCallback(bookmarkCallback)); System.Console.WriteLine("创建bookmark:{0}", bookmark); } void bookmarkCallback(NativeActivityContext context, Bookmark bookmark, object obj) {
if (typeof(T) == typeof(Int32)) { this.Result.Set(context, int.Parse(obj.ToString()) ); return; }
if (typeof(T) == typeof(string)) { this.Result.Set(context, obj.ToString()); return; }
this.Result.Set(context,obj); } } |
宿主 | //===================================================
WorkflowApplication instance = null;
private void button_triggering_Click(object sender, RoutedEventArgs e) { string bookName = textBox_bookmark.Text; string inputValue = textBox_value.Text;
if (instance != null) { if (instance.GetBookmarks().Count(p => p.BookmarkName == bookName) == 1) { instance.ResumeBookmark(bookName, inputValue); } else { foreach (var v in instance.GetBookmarks()) { System.Console.WriteLine("-------请从下面选项中选择一BookmarkName---------------------------"); System.Console.WriteLine("BookmarkName:{0}:,OwnerDisplayName:{1}", v.BookmarkName, v.OwnerDisplayName); System.Console.WriteLine("================================"); } } } else { MessageBox.Show("没有创建实例"); }
}
void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { instance = null; System.Console.WriteLine("workflowCompleted:{0}", e.CompletionState.ToString()); }
void aborted(WorkflowApplicationAbortedEventArgs e) { instance = null; System.Console.WriteLine("aborted ,Reason:{0}", e.Reason.Message); }
UnhandledExceptionAction unhandledExceptionl(WorkflowApplicationUnhandledExceptionEventArgs e) { System.Console.WriteLine("unhandledException:{0}", e.UnhandledException.Message); return UnhandledExceptionAction.Cancel; } void workflowIdel(WorkflowApplicationIdleEventArgs e) { System.Console.WriteLine("Idle:{0}", e.InstanceId);
System.Console.WriteLine("--------BookmarkName---------------------------"); foreach (var item in e.Bookmarks) { System.Console.WriteLine("{0}", item.BookmarkName); } System.Console.WriteLine("================================"); } //================================================== private void button_FlowDecisionWorkflow_Click(object sender, RoutedEventArgs e) { instance = new WorkflowApplication(new FlowcharLibrary.FlowDecisionWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted); instance.OnUnhandledException = unhandledExceptionl; instance.Aborted = aborted; instance.Idle = workflowIdel; instance.Run(); }
|
结果 |
FlowSwitch 例子
工作流 | |
bookmark | public sealed class bookmark<T> : NativeActivity<T> { public InArgument<string> bookmarkName { get; set; }
protected override bool CanInduceIdle { get { return true; } } protected override void Execute(NativeActivityContext context) { string bookmark = context.GetValue(bookmarkName); context.CreateBookmark(bookmark, new BookmarkCallback(bookmarkCallback)); System.Console.WriteLine("创建bookmark:{0}", bookmark); } void bookmarkCallback(NativeActivityContext context, Bookmark bookmark, object obj) {
if (typeof(T) == typeof(Int32)) { this.Result.Set(context, int.Parse(obj.ToString()) ); return; }
if (typeof(T) == typeof(string)) { this.Result.Set(context, obj.ToString()); return; }
this.Result.Set(context,obj); } } |
宿主 | //===================================================
WorkflowApplication instance = null;
private void button_triggering_Click(object sender, RoutedEventArgs e) { string bookName = textBox_bookmark.Text; string inputValue = textBox_value.Text;
if (instance != null) { if (instance.GetBookmarks().Count(p => p.BookmarkName == bookName) == 1) { instance.ResumeBookmark(bookName, inputValue); } else { foreach (var v in instance.GetBookmarks()) { System.Console.WriteLine("-------请从下面选项中选择一BookmarkName---------------------------"); System.Console.WriteLine("BookmarkName:{0}:,OwnerDisplayName:{1}", v.BookmarkName, v.OwnerDisplayName); System.Console.WriteLine("================================"); } } } else { MessageBox.Show("没有创建实例"); }
}
void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { instance = null; System.Console.WriteLine("workflowCompleted:{0}", e.CompletionState.ToString()); }
void aborted(WorkflowApplicationAbortedEventArgs e) { instance = null; System.Console.WriteLine("aborted ,Reason:{0}", e.Reason.Message); }
UnhandledExceptionAction unhandledExceptionl(WorkflowApplicationUnhandledExceptionEventArgs e) { System.Console.WriteLine("unhandledException:{0}", e.UnhandledException.Message); return UnhandledExceptionAction.Cancel; } void workflowIdel(WorkflowApplicationIdleEventArgs e) { System.Console.WriteLine("Idle:{0}", e.InstanceId);
System.Console.WriteLine("--------BookmarkName---------------------------"); foreach (var item in e.Bookmarks) { System.Console.WriteLine("{0}", item.BookmarkName); } System.Console.WriteLine("================================"); } //==================================================
private void button_FlowSwitchWorkflow_Click(object sender, RoutedEventArgs e) { instance = new WorkflowApplication(new FlowcharLibrary.FlowSwitchWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted); instance.OnUnhandledException = unhandledExceptionl; instance.Aborted = aborted; instance.Idle = workflowIdel; instance.Run(); } |
结果 |
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)