WF DesignerView is null after calling GetService

I try to start a new constructor in my application, but I get an error:

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

therefore designerView will be null here. I don’t know what is 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;
+4
source share
2 answers

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

About a week ago I ran into the same problem. It turned out that I was trying to get the DesignerView, while I was still in the UserControl initialization code that contained the restored WorkflowDesigner.

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">
+4

, ! , , ! , 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;
};
+1

Source: https://habr.com/ru/post/1547716/


All Articles