我在xaml中有以下内容:
<TextBox
x:Name="TextBoxButton1"
Margin="10,0,10,10"
Controls:TextboxHelper.Watermark="Enter Text"
Controls:TextboxHelper.ButtonCommand="{Binding BrowseFileAction}"
Style="{StaticResource OpenFilTextBox}"
/>
在powershell代码中,我需要设置&#34; BrowseFileAction&#34;执行以下操作:浏览文件。
基本上我想知道如何将以下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;
}
}
我尝试使用add-type这样做,但powershell抱怨缺少类,委托或接口.. 我确信这必须使用c#语法,但我不知道c#所以无法修改add-type的代码。
答案 0 :(得分:0)
您错过了该代码周围的namespace
和class
声明,例如你需要:
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的实例。