代码之家  ›  专栏  ›  技术社区  ›  yoav.str

命令模式是如何工作的?

  •  1
  • yoav.str  · 技术社区  · 14 年前

    我正在做:

           <Button 
                Style="{StaticResource buttonstyle}"
                HorizontalAlignment="Left" Margin="12,21,10,0" 
                VerticalAlignment="Top" Height="78" Width="83" 
                BorderThickness="2" Content="add event"
                Command="{Binding NewEvent}" 
                CommandParameter="This is the report."
            >
            </Button>
    

       public class StringDelegateCommand : ICommand 
    { 
        //methodes without return value
        Action<string> m_ExecuteTargets = delegate { };
        //methodes without parmtters inside
        Func<bool> m_CanExecuteTargets = delegate { return false; };
        //the value whom allows execution 
        bool m_Enabled = false;
    
    
        #region ICommand Members
    
        public bool CanExecute(object parameter)
        {
            Delegate[] targets = m_CanExecuteTargets.GetInvocationList();
            foreach (Func<bool> target in targets)
            {
                m_Enabled = false;
                bool localEnable = target.Invoke();
                if (localEnable)
                {
                    m_Enabled = true;
                    break;
                }
            }
            return m_Enabled;
        }
    
        public event EventHandler CanExecuteChanged = delegate { };
    
        public void Execute(object parameter)
        {
            if (m_Enabled)
                m_ExecuteTargets(parameter != null ? parameter.ToString() : null);
        }
    
        #endregion
    
        public event Action<string> ExecuteTargets
        {
            add
            {
                m_ExecuteTargets += value;
            }
            remove
            {
                m_ExecuteTargets -= value;
            }
        }
    
        public event Func<bool> CanExecuteTargets
        {
            add
            {
                m_CanExecuteTargets += value;
                CanExecuteChanged(this, EventArgs.Empty);
            }
    
            remove
            {
                m_CanExecuteTargets -= value;
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    }
    

    在我的视图模型中(它是在上下文中绑定的,所以东西是绑定的!!!):

     //called in ctor where newEvent is defined : StringDelegateCommand newEvent; 
         private void setNewEventCommand()
        {
            newEvent = new StringDelegateCommand();
            newEvent.CanExecuteTargets += isThereAnotherNewEvent;
            newEvent.ExecuteTargets += exacuteNewEvent;
            NewEvent = newEvent; 
        }
    
        void exacuteNewEvent(string message)
        {
            Window1 w = new Window1();
            w.ShowDialog();
    
        }
    

    当我按一下按钮什么也没发生,我做错什么了?有人能帮我理解我的错误吗。。。

    谢谢您。。。

    我应该提一下,这是康普勒写的:

    System.Windows.Data错误:40:BindingExpression路径错误:“在”“object”“viewModel”(HashCode=18612316)上找不到“NewEvent”属性。BindingExpression:Path=NewEvent;DataItem='viewModel'(HashCode=18612316);目标元素是'Button'(Name='');目标属性是'Command'(类型'ICommand')

    2 回复  |  直到 14 年前
        1
  •  0
  •   Vlad    14 年前

    首先,检查您的绑定是否完全绑定。在运行时,可以在Visual Studio的输出窗口中看到绑定错误。或者您可以在某个地方设置一个断点,然后在调试器中查看按钮的值 Command <Button x:Name="TEST" 将允许在窗口/控件中作为变量访问测试)。

    CanExecute Execute 看看到底发生了什么。

        2
  •  0
  •   Adrian Toman Duncan_m    12 年前

    问题解决了:房产应该是公开的,然后一切正常。