Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.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# RaiseCanExecuteChanged在编译的exe中不工作,但在调试时工作_C#_Wpf_Mvvm_Prism_Delegatecommand - Fatal编程技术网

C# RaiseCanExecuteChanged在编译的exe中不工作,但在调试时工作

C# RaiseCanExecuteChanged在编译的exe中不工作,但在调试时工作,c#,wpf,mvvm,prism,delegatecommand,C#,Wpf,Mvvm,Prism,Delegatecommand,我完全不知道这是什么原因 背景:使用Prism框架 我有一个按钮绑定到DelegateCommand 我调用RaiseCanceTechChanged 当我在VisualStudio中以调试模式启动应用程序时,一切都正常运行。该应用程序运行良好 然后,当我通过.exe打开应用程序时,没有调用raisecancecutechanged方法。我不知道为什么会这样。还有谁遇到过类似的问题吗 编辑:当我第一次通过.exe打开应用程序时,调用了raisecancecutechanged(因为我在我的Vi

我完全不知道这是什么原因

背景:使用Prism框架

  • 我有一个按钮绑定到
    DelegateCommand
  • 我调用
    RaiseCanceTechChanged
  • 当我在VisualStudio中以调试模式启动应用程序时,一切都正常运行。该应用程序运行良好

    然后,当我通过.exe打开应用程序时,没有调用
    raisecancecutechanged
    方法。我不知道为什么会这样。还有谁遇到过类似的问题吗


    编辑:当我第一次通过.exe打开应用程序时,调用了
    raisecancecutechanged
    (因为我在我的
    ViewModel
    的构造函数中设置了它)。然而,它再也不会被调用


    代码,以备需要:

    private readonly DelegateCommand _buttonCommand;
    
    public ViewModel()
    {
        _buttonCommand = new DelegateCommand(Button, CanExecuteButton);
    }
    
    public DelegateCommand ButtonCommand
    {
        get { return this._buttonCommand; }
    }
    
    public void Button()
    {
        ... do stuff ...
        _buttonCommand.RaiseCanExecuteChanged();
    }
    
    public bool CanExecuteButton()
    {
        if (some condition)
            return true;
        else
            return false;
    }
    
    <Button x:Name="MyButton" Content="ClickMe"
            Command="{Binding ButtonCommand}">
    
    private readonly DelegateCommand\u按钮命令;
    公共视图模型()
    {
    _buttonCommand=新的DelegateCommand(按钮,CanExecuteButton);
    }
    公共委派命令按钮命令
    {
    获取{返回此。\按钮命令;}
    }
    公共作废按钮()
    {
    …做事。。。
    _buttonCommand.RaiseCanExecuteChanged();
    }
    公共布尔CanExecuteButton()
    {
    如果(某些条件)
    返回true;
    其他的
    返回false;
    }
    

    我甚至绝望地尝试在我的按钮中添加一个
    IsEnabled
    属性,我绑定到
    CanExecuteButton
    。。。无效。

    我在Prism DelegateCommand.CanExecuteChanged事件未被调用时遇到类似问题。。通过查看源代码,它看起来像是因为它不依赖于
    命令管理器。RequerySuggested

    尝试在CanExecuteChange事件如下所示的位置创建自己的命令:

    public RelayCommand : ICommand
    {
        private event EventHandler _canExecuteChanged;
        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
                _canExecuteChanged += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
                _canExecuteChanged -= value;
            }
        }
    
        public void RaiseCanExecuteChanged()
        {
            var handler = _canExecuteChanged;
            if (handler != null)
                handler(this, EventArgs.Empty);
    
        }
    
        // All the other stuff also 
    }
    
    现在,如果WPF检测到UI中的更改,那么CommandManager将在命令上调用CanExecute。如果应用程序引擎中的某些内容发生更改,则可以调用
    RaiseCanecuteChanged
    来更新CanExecute。

    试试看

    Command="{Binding DataContext.ButtonCommand,RelativeSource={RelativeSource FindAncestor, AncestorType=YourView}}" CommandParameter="{Binding}" 
    

    你能发布你的
    按钮命令的代码吗?解决了,但我不知道为什么。它现在起作用了…这似乎没用。