向 ICommand 添加新方法 (C# WPF)
adding a new method to ICommand (C# WPF)
这是我的助手 class,可用于我项目中的不同 ViewModelClass;
public class MOpenFileDialog : ViewModelBase
{
#region Properties
private OpenFileDialog _openDialog;
public OpenFileDialog Dialog
{
get { return _openDialog; }
set
{
_openDialog = value;
RaisePropertyChanged(nameof(Dialog));
}
}
// some other properties
#endregion
#region Constructors
public MOpenFileDialog()
{
OpenFileCommand = new RelayCommand<object>(OpenFile);
Dialog = new OpenFileDialog()
{
FileName = "Documen",
Filter = "All Files|*.*",
};
}
#endregion
#region Methods
public void OpenFile(object param)
{
// somecode
}
// some other methods
#endregion
#region Commands
public ICommand OpenFileCommand { get; set; }
#endregion
}
在 myClassVM 中,我有一个 MOpenFileDialog 的瞬间,我正在尝试添加
OpenFileCommand 的新方法;
public class myClassVM : ViewModelBase
{
#region Properties
public MOpenFileDialog Opendial { get; set; }
#endregion
#region Constructor
public CollectNamesVM()
{
Opendial = new MOpenFileDialog() { };
Opendial.Dialog.Multiselect = true;
//Opendial.OpenFileCommand += CollectFilesFolder; // Wrong Way
}
#endregion
#region Methods
public void CollectFilesFolder()
{
// toDo
}
}
我想添加另一个在 openFileCommand 触发时执行的方法,
我试错了,只是为了解释我想做什么。
任何帮助将不胜感激。
在下面的5步中使用delegate
:
步骤 1。创建一个delegate
。您想要使用代码传递 string[]
(string
数组,data type
of FileNames
的示例:Opendial.Dialog.Multiselect = true
)
步骤 2。使用步骤 1
中的 delegate
创建一个 event
步骤 3。随时在 MOpenFileDialog
中提高在步骤 2 中创建的 event
步骤 4。我不知道你想要什么,但它和这个一样
步骤 5。 CollectFilesFolder
方法的参数与委托的参数相同
// 1. Create a delegate. Example you want to pass an string array
public delegate void OpenFileCommand(string[] fileNames);
public class MOpenFileDialog : ViewModelBase
{
// 2. Create a event using the delegate in step 1
public event OpenFileCommand OnOpenFileCommand;
// 3. Raise the event created in step 2 everywhen you want
public void SomeMethod(string[] fileNames)
{
this.OnOpenFileCommand.Invoke(fileNames);
}
#region Properties
private OpenFileDialog _openDialog;
public OpenFileDialog Dialog
{
get { return _openDialog; }
set
{
_openDialog = value;
RaisePropertyChanged(nameof(Dialog));
}
}
// some other properties
#endregion
#region Constructors
public MOpenFileDialog()
{
OpenFileCommand = new RelayCommand<object>(OpenFile);
Dialog = new OpenFileDialog()
{
FileName = "Documen",
Filter = "All Files|*.*",
};
}
#endregion
#region Methods
public void OpenFile(object param)
{
// somecode
}
// some other methods
#endregion
#region Commands
public ICommand OpenFileCommand { get; set; }
#endregion
}
public class myClassVM : ViewModelBase
{
#region Properties
public MOpenFileDialog Opendial { get; set; }
#endregion
#region Constructor
public CollectNamesVM()
{
Opendial = new MOpenFileDialog() { };
Opendial.Dialog.Multiselect = true;
// 4. I cant know what do you want here, but Its same as this
Opendial.OnOpenFileCommand += CollectFilesFolder(Opendial.Dialog.FileNames); // Wrong Way
}
#endregion
#region Methods
// 5. CollectFilesFolder has params same as the delegate
public void CollectFilesFolder(string[] fileNames)
{
// toDo
}
}
这是我的助手 class,可用于我项目中的不同 ViewModelClass;
public class MOpenFileDialog : ViewModelBase
{
#region Properties
private OpenFileDialog _openDialog;
public OpenFileDialog Dialog
{
get { return _openDialog; }
set
{
_openDialog = value;
RaisePropertyChanged(nameof(Dialog));
}
}
// some other properties
#endregion
#region Constructors
public MOpenFileDialog()
{
OpenFileCommand = new RelayCommand<object>(OpenFile);
Dialog = new OpenFileDialog()
{
FileName = "Documen",
Filter = "All Files|*.*",
};
}
#endregion
#region Methods
public void OpenFile(object param)
{
// somecode
}
// some other methods
#endregion
#region Commands
public ICommand OpenFileCommand { get; set; }
#endregion
}
在 myClassVM 中,我有一个 MOpenFileDialog 的瞬间,我正在尝试添加 OpenFileCommand 的新方法;
public class myClassVM : ViewModelBase
{
#region Properties
public MOpenFileDialog Opendial { get; set; }
#endregion
#region Constructor
public CollectNamesVM()
{
Opendial = new MOpenFileDialog() { };
Opendial.Dialog.Multiselect = true;
//Opendial.OpenFileCommand += CollectFilesFolder; // Wrong Way
}
#endregion
#region Methods
public void CollectFilesFolder()
{
// toDo
}
}
我想添加另一个在 openFileCommand 触发时执行的方法, 我试错了,只是为了解释我想做什么。
任何帮助将不胜感激。
在下面的5步中使用delegate
:
步骤 1。创建一个delegate
。您想要使用代码传递 string[]
(string
数组,data type
of FileNames
的示例:Opendial.Dialog.Multiselect = true
)
步骤 2。使用步骤 1
中的delegate
创建一个 event
步骤 3。随时在 MOpenFileDialog
event
步骤 4。我不知道你想要什么,但它和这个一样
步骤 5。 CollectFilesFolder
方法的参数与委托的参数相同
// 1. Create a delegate. Example you want to pass an string array
public delegate void OpenFileCommand(string[] fileNames);
public class MOpenFileDialog : ViewModelBase
{
// 2. Create a event using the delegate in step 1
public event OpenFileCommand OnOpenFileCommand;
// 3. Raise the event created in step 2 everywhen you want
public void SomeMethod(string[] fileNames)
{
this.OnOpenFileCommand.Invoke(fileNames);
}
#region Properties
private OpenFileDialog _openDialog;
public OpenFileDialog Dialog
{
get { return _openDialog; }
set
{
_openDialog = value;
RaisePropertyChanged(nameof(Dialog));
}
}
// some other properties
#endregion
#region Constructors
public MOpenFileDialog()
{
OpenFileCommand = new RelayCommand<object>(OpenFile);
Dialog = new OpenFileDialog()
{
FileName = "Documen",
Filter = "All Files|*.*",
};
}
#endregion
#region Methods
public void OpenFile(object param)
{
// somecode
}
// some other methods
#endregion
#region Commands
public ICommand OpenFileCommand { get; set; }
#endregion
}
public class myClassVM : ViewModelBase
{
#region Properties
public MOpenFileDialog Opendial { get; set; }
#endregion
#region Constructor
public CollectNamesVM()
{
Opendial = new MOpenFileDialog() { };
Opendial.Dialog.Multiselect = true;
// 4. I cant know what do you want here, but Its same as this
Opendial.OnOpenFileCommand += CollectFilesFolder(Opendial.Dialog.FileNames); // Wrong Way
}
#endregion
#region Methods
// 5. CollectFilesFolder has params same as the delegate
public void CollectFilesFolder(string[] fileNames)
{
// toDo
}
}