调用GetService后,WF DesignerView为null

时间:2014-07-09 10:48:35

标签: c# workflow-foundation

我正在尝试在我的应用程序中启动一个新的设计器,但我收到错误:

     DesignerView designerView = wd.Context.Services.GetService<DesignerView>();

所以designerView在这里将为null。我不知道自己错过了什么。

这是我的LoadWorkflowFromFile(字符串fileName)方法中的代码。

            workflowFilePathName = fileName;
            workflowDesignerPanel.Content = null;
            WorkflowPropertyPanel.Content = null;
            wd = new WorkflowDesigner();

            wd.Load(workflowFilePathName);
            DesignerView designerView = wd.Context.Services.GetService<DesignerView>();
            designerView.WorkflowShellBarItemVisibility =                              ShellBarItemVisibility.Arguments
                                                           | ShellBarItemVisibility.Imports
                                                          | ShellBarItemVisibility.MiniMap
                                                          | ShellBarItemVisibility.Variables
                                                          | ShellBarItemVisibility.Zoom;
            workflowDesignerPanel.Content = wd.View;
            WorkflowPropertyPanel.Content = wd.PropertyInspectorView;

2 个答案:

答案 0 :(得分:4)

我知道回答可能有点迟,但未来可能会帮助其他人。

大约一周前,我遇到了同样的问题。原来我试图获取DesignerView,而我仍然在包含重新托管的WorkflowDesigner的UserControl的初始化代码中。

我仍然在那里初始化了WorkflowDesigner,但是在将GetService调用移动到Loaded方法之后,它实际上返回了DesignerView:

public partial class MyDesignerControl : UserControl
{
    private WorkflowDesigner wd;
    private string workflowFilePathName;

    protected override void OnInitialized(EventArgs e) {
         base.OnInitialized();
         wd = new WorkflowDesigner();

         wd.Load(workflowFilePathName);
         workflowDesignerPanel.Content = wd.View;

         // this doesn't work here:
         // var designerView = wd.Context.Services.GetService<DesignerView>();
    }

    private void MyDesignerControl_Loaded(object sender, RoutedEventArgs e) {
        // here it works:
        var designerView = wd.Context.Services.GetService<DesignerView>();

        // null check, just to make sure it doesn't explode
        if (designerView != null) {
            designerView.WorkflowShellBarItemVisibility =
                     // ShellBarItemVisibility.Imports | <-- Uncomment to show again
                        ShellBarItemVisibility.MiniMap |
                     // ShellBarItemVisibility.Variables | <-- Uncomment to show again
                     // ShellBarItemVisibility.Arguments | <-- Uncomment to show again
                        ShellBarItemVisibility.Zoom;
        }
    }

    ...
}

使用DesignerView后,WorkflowShellBarItemVisibility部分按预期工作。

(为了完整起见:不要忘记将方法注册到XAML中的相应事件):

<UserControl x:Class="My.Namespace.Here.MyDesignerControl"
             ...
             Loaded="MyDesignerControl_Loaded">

答案 1 :(得分:2)

感谢您发布此内容!我最终采用了与我的代码相匹配的略有不同的方式,但是你引导我朝着正确的方向前进!诀窍是将视图转换为FrameworkElement,以便您可以挂钩已加载的事件

_wf = new WorkflowDesigner();
((System.Windows.FrameworkElement)_wf.View).Loaded += (s, e) =>
{
    var designerView = _wf.Context.Services.GetService<DesignerView>();
    if (designerView != null)
        designerView.WorkflowShellBarItemVisibility =
                    ShellBarItemVisibility.Variables |
                    //ShellBarItemVisibility.Arguments |  
                    //ShellBarItemVisibility.Imports |     
                    ShellBarItemVisibility.PanMode |
                    ShellBarItemVisibility.MiniMap |
                    ShellBarItemVisibility.Zoom;
};