2014-05-22 41 views
0

我有在XAML如下:System.Windows.Input.Icommand在PowerShell中

 <TextBox 
     x:Name="TextBoxButton1" 
     Margin="10,0,10,10" 
     Controls:TextboxHelper.Watermark="Enter Text" 
     Controls:TextboxHelper.ButtonCommand="{Binding BrowseFileAction}" 
     Style="{StaticResource OpenFilTextBox}" 
     /> 

在PowerShell代碼我需要設置「BrowseFileAction」執行以下操作:瀏覽文件。

基本上我想知道如何把下面的C#代碼轉換爲PowerShell的:

private string selectedPath = string.Empty; 

public string SelectedPath 
{ 
    get { return this.selectedPath; } 
    set { this.SetProperty<string>(ref this.selectedPath, value); } 
} 

public ICommand BrowseFileCommand 
{ 
    get { return new RelayCommand(BrowseFileAction); } 
} 

public void BrowseFileAction() 
{ 
    var dialog = new OpenFileDialog(); 
    bool? result = dialog.ShowDialog(); 
    if ((result.HasValue) && (result.Value)) 
    { 
     this.SelectedPath = dialog.FileName; 
    } 
} 

來源 - http://cisart.wordpress.com/2013/10/09/metro-textbox-with-browse-folder-button-and-validation/comment-page-1/

我試圖做到這一點使用添加型,但PowerShell中抱怨缺少類,委託或接口..我相信這必須做一些與C#語法,但我不知道C#所以不能修改添加類型的代碼。

回答

0

您錯過了該代碼周圍的namespaceclass聲明,例如:你需要:

namespace MyNamespace { 
    public class FileAction { 
     private string selectedPath = string.Empty; 
     ... rest of your code here ... 
    } 
} 

但是你也缺少RelayCommand類型的定義。這是一個article that shows to implement RelayCommand。最後,需要將XAML Window的DataContext屬性設置爲FileAction的一個實例。

+0

感謝您的回覆基思..我看着你提供的繼電器命令的鏈接,但似乎太多的C#我包裹了我的頭:)...是否有可能避免使用任何C#代碼,也許可以使用PowerShell中某種構造函數的一些構造函數,例如:new-object system.windows.input.icommand – Kiran

+0

您無法實例化接口。您可以實例化一個實現接口的類的實例。 RelayCommand實現了ICommand接口。此外,在PowerShell中,您將無法實現實現接口的類。你將需要C#。也許你應該看看像ShowUI這樣的PowerShell WPF模塊,它爲你做了很多這樣的工作? http://showui.codeplex.com/ –

+0

感謝清除了一些東西:)...與showui的問題是,它需要在任何一臺執行代碼的計算機上,而且showui不是那麼簡單......我結束了把文本框和一個按鈕放在一個stackpanel中,這種模仿我想要的行爲..它確實掩蓋了鍵入的文本一點點,但我認爲我可以忍受它,所以誰會去使用它大聲笑.. 。 – Kiran