0

I have a workflow with multiple activities that I'm editing on VS2017. This workflow is here: C:/Repo/Workflows/ProjectXyz/Foo/ThisFile.xaml

This workflow is populated with activities, one of those activities is of type MyPathActivity. This activity has a button. When that button is clicked (on design time from ThisFile.xaml), I want it to show a MessageBox with the text C:/Repo/Workflows/ProjectXyz/Foo/ThisFile.xaml, but I don't know how to get that path.

How can I get the path to the xaml file at design time?

All that I've tried got me to the VS debugger path, the project's root path,... but I need the full path to the xaml that I'm editing.

1 Answer 1

0

I've managed to get the xaml path:

We point to the EditingContext (Context property of the ActivityDesigner) and then to its Items property. Those items are a collection of ContextItems of these types:

  • ReadOnlyState
  • AssemblyContextControlItem
  • WorkflowCommandExtensionItem
  • WorkflowFileItem
  • Selection

Now WorkflowFileItem sound a lot like something that would be useful =)

Pointing to the property LoadedFile of that item we get:

C:/Repo/Workflows/ProjectXyz/Foo/ThisFile.xaml

Hurray!


Now the code:

public static class DesignerUtils
{
    public static string GetXamlPathFromActivityDesignerElement<TActivityDesigner
        (TActivityDesigner designer) where TActivityDesigner : ActivityDesigner
    {
        EditingContext editingContext = designer.Context;
        ContextItemManager items = editingContext.Items;
        WorkflowFileItem fileItem = (WorkflowFileItem)items
            .SingleOrDefault(item => item is WorkflowFileItem);

        return fileItem.LoadedFile;
    }
}

Here the use in the click handler:

public void OnPathButtonClick(object sender, RoutedEventArgs e)
{
    string xamlPath = DesignerUtils
        .GetXamlPathFromActivityDesignerElement<MyPathActivityDesigner>
        (sender as FrameworkElement);
    MessageBox.Show(xamlPath);
}

Edit: First version needed the event sender. That is a totally unnecesary step, because we already have the designer using this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.