Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向ICommand(C WPF)添加新方法_C#_Wpf_Mvvm_Relaycommand - Fatal编程技术网

C# 向ICommand(C WPF)添加新方法

C# 向ICommand(C WPF)添加新方法,c#,wpf,mvvm,relaycommand,C#,Wpf,Mvvm,Relaycommand,这是我的助手类,用于我项目中的不同ViewModelClass public class MOpenFileDialog : ViewModelBase { #region Properties private OpenFileDialog _openDialog; public OpenFileDialog Dialog { get { return _openDialog; }

这是我的助手类,用于我项目中的不同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


    }
我想添加另一个方法,在启动openFileCommand时实现, 我做了一次错误的尝试,只是为了解释我想做什么

如有任何帮助,我们将不胜感激。

在以下5个步骤中使用代表:

第一步。创建一个委托。要传递字符串[]字符串数组、文件名数据类型和代码的示例:Opendial.Dialog.Multiselect=true

第二步。使用步骤1中的委托创建事件

第三步。在MOpenFileDialog中随时引发在步骤2中创建的事件

第四步。我不知道你想要什么,但和这个一样

第五步。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
        }
    }

Execute不是一个事件,它是一个方法,您不能订阅it@PavelAnikhouski我知道,我在寻求一种正确的方法来实现这个想法。请反馈答案和您的结果,继续寻找这个问题的好解决方案,@ExecuteAli@Nguyen,非常感谢你的回答,我要试试,告诉你发生了什么,
    // 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
        }
    }