0

我试图在WF 4.5工作流活动中实现浏览文件夹,但省略号按钮没有显示,几乎没有任何反应。Windows工作流设计器4.5(浏览文件夹)中的UITypeEditor

这是我的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,并没有什么区别。

回答

1

我想通了通过查看微软提供了一些样品即Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4

无论如何,在此基础上的信息,我设法显示椭圆形按钮(“...”),一个文本框和工具提示文本框太短而无法显示路径的事件。这还不完美,因为我正在寻找如何添加其他属性来设置'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. 绑定工具提示,即价值

的“重要”的代码是中都包含的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; }