繁体   English   中英

Windows Workflow Designer 4.5中的UITypeEditor(浏览文件夹)

[英]UITypeEditor in Windows Workflow Designer 4.5 (Browse for folder)

我正在尝试在WF 4.5工作流程活动中实现“浏览”文件夹,但是省略号按钮没有显示,几乎没有任何反应。

这是我的UITypeEditor类:

public class BrowseForFolderEditor : UITypeEditor
{
    public override object EditValue(ITypeDescriptorContext context, 
         IServiceProvider provider, object value)
    {
        string folderName = string.Empty;
        BrowseForFolderAttribute browseForFolderAttribute = null;

        if (value is string)
        {
            if (context?.PropertyDescriptor != null)
            {
                browseForFolderAttribute =
                    (BrowseForFolderAttribute)
                context.PropertyDescriptor.Attributes[typeof(BrowseForFolderAttribute)];
            }

            var browse = new FolderBrowserDialogEx
            {
                Description = browseForFolderAttribute?.Description,
                ShowNewFolderButton = true,
                ShowEditBox = true,
                SelectedPath = folderName,
                ShowFullPathInEditBox = false,
                RootFolder = Environment.SpecialFolder.MyComputer
            };

            var result = browse.ShowDialog();

            if (result == DialogResult.OK)
                folderName = browse.SelectedPath;

            return folderName;
        }

        // Return whatever value if it wasn't a string - Should never occur!
        return value;
    }

    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal; //base.GetEditStyle(context);
    }

    public class BrowseForFolderAttribute : Attribute
    {
        public BrowseForFolderAttribute(string description)
        {
            this.Description = description;
        }

        public string Description { get; set; }
    }
}

这就是我在Activity声明代码的方式:

    [Description("Select the folder where the files will be 
     copied/moved to.")]
    [Category("Folders")]
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [BrowseForFolderEditor.BrowseForFolder("Select the folder where the files will be 
     copied/moved to.")]
    [Editor(typeof(BrowseForFolderEditor), typeof(UITypeEditor))]
    public string TargetPath { get; set; }

我不知道这有什么区别,但是我的工作流程ActivityNativeActivity类型。

该属性显示在属性网格中,但仅显示为没有椭圆形按钮的文本框。

任何帮助,将不胜感激。

UPDATE-1:

问题与它是NativeCodeActivity无关,因为我刚刚将代码更改为CodeActivity ,并且没有区别。

我通过查看Microsoft提供的一些示例(即Windows .NET Framework 4的Windows Communication Foundation(WCF)和Windows Workflow Foundation(WF)示例)得出了答案。

无论如何,基于此信息,在文本框太短而无法显示路径的情况下,我设法显示了椭圆按钮('...'),文本框和工具提示。 尚不完美,因为我正在寻找如何添加其他属性来设置“ BrowseForFolder”对话框的其他属性的方法,但我想我应该分享自己的发现。

我的编辑器的定义如下:

public class BrowseForFolderEditor : DialogPropertyValueEditor
{
    public BrowseForFolderEditor()
    {
        // Create a textbox, a '...' button and a tooltip.
        string template = @"
            <DataTemplate
                xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
                xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
                xmlns:pe='clr-namespace:System.Activities.Presentation.PropertyEditing;assembly=System.Activities.Presentation'>
                <DockPanel LastChildFill='True'>
                    <pe:EditModeSwitchButton TargetEditMode='Dialog' Name='EditButton' DockPanel.Dock='Right'>...</pe:EditModeSwitchButton>
                    <TextBlock Text='{Binding Value}' Margin='2,0,0,0' VerticalAlignment='Center'>
                        <TextBox.ToolTip>
                            <ToolTip>
                                <TextBlock Text='{Binding Value}' Margin='2' VerticalAlignment='Center' HorizontalAlignment='Left'/>
                            </ToolTip>
                        </TextBox.ToolTip>
                    </TextBlock>
                </DockPanel>
            </DataTemplate>";

        using (var sr = new MemoryStream(Encoding.UTF8.GetBytes(template)))
        {
            this.InlineEditorTemplate = XamlReader.Load(sr) as DataTemplate;
        }
    }

    public override void ShowDialog(PropertyValue propertyValue, 
      IInputElement commandSource)
    {
        var browse = new FolderBrowserDialog
        {
            ShowNewFolderButton = true,
            SelectedPath = propertyValue.StringValue,
            Description = "Select Target Folder:",
            RootFolder = Environment.SpecialFolder.MyComputer
        };

        if (browse.ShowDialog() == DialogResult.OK)
        {
            propertyValue.Value = browse.SelectedPath;
        }

        browse.Dispose();
    }
}

我不会过多介绍有关XAML详细信息,但需要注意以下几点:

  1. 如何设置InlineEditorTemplate,即将其设置为字符串中预定义的XAML。
  2. 绑定文本框,即值
  3. 工具提示的绑定,即价值

Activity构造函数中包含的“重要”代码是:

AttributeTableBuilder builder = new AttributeTableBuilder();

builder.AddCustomAttributes(typeof(CopyMoveFiles), "TargetPath",
            new EditorAttribute(
            typeof(BrowseForFolderEditor), 
            typeof(DialogPropertyValueEditor)));

MetadataStore.AddAttributeTable(builder.CreateTable());

其中TargetPath表示将在PropertyGrid显示的字符串属性。

肯定还有改进的余地,但这只是一个开始,而且看起来效果很好。 值得一提的是,添加各种参考文献很痛苦。 我再也不能浪费时间了,但是即使按照Microsoft项目添加了所有引用之后,它仍然无法正常工作,最终还是成功了,所以我仍然不确定为什么会这样,这很可惜但是,如果有人尝试过并能指出哪个参考给他们带来了麻烦,我很想知道。

希望这可以帮助。

更新:

如果您不想在构造函数中以编程方式定义属性,则可以简单地使用属性:

[Editor(typeof(BrowseForFolderEditor), typeof(DialogPropertyValueEditor))]
public string TargetPath { get; set; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM