WF DesignerView is null after GetService is invoked

c#, workflow-foundation

Solution

I know that it may be a bit late to answer, but it'll probably help others in the future.

About a week ago, I encountered the same problem. Turned out that I tried to get the DesignerView, while I was still in the initialization code of the UserControl that contained the rehosted WorkflowDesigner.

I still initialized the WorkflowDesigner there, but after moving the GetService call to the Loaded method, it actually returned the 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;
        }
    }

    ...
}

Once I had the DesignerView, the WorkflowShellBarItemVisibility part worked as expected.

(For the sake of completeness: don't forget to register the method to the corresponding event in XAML):

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

Problem

I am trying to start a new designer in my application but i get an error at : ``` DesignerView designerView = wd.Context.Services.GetService<DesignerView>(); ``` so the designerView will be null here. I dodn't know what i am missing. This is the code from my LoadWorkflowFromFile(string fileName) method. ``` 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; ```

Original source