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; }