【问题标题】:how does the command pattern works?命令模式是如何工作的?
【发布时间】:2011-05-06 06:13:44
【问题描述】:

我在做:

       <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();

    }

当我点击按钮时没有任何反应,我做错了什么?谁能帮我理解我的错误...

谢谢...

EDIT 我应该提一下这是编译器写的:

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“viewModel”(HashCode=18612316)上找不到“NewEvent”属性。绑定表达式:路径=新事件; DataItem='viewModel' (HashCode=18612316);目标元素是 'Button' (Name='');目标属性是“命令”(输入“ICommand”)

【问题讨论】:

    标签: c# wpf binding


    【解决方案1】:

    首先,检查您的绑定是否完全绑定。您可以在运行时在 Visual Studio 的输出窗口中看到绑定错误。或者您可以在某处设置断点,并在调试器中查看按钮的 Command 的值是什么(您需要为按钮命名,以便在调试器中轻松访问它:&lt;Button x:Name="TEST" 将允许访问 TEST 作为窗口/控件中的变量)。

    其次,如果绑定正确,你应该在CanExecuteExecute设置断点,看看到底发生了什么。

    希望对您有所帮助。

    【讨论】:

    • 我希望我的 spp 是 xmal 驱动的,所以当有人替换代码时它是可移植的
    【解决方案2】:

    问题已解决:该属性应该是公开的,然后一切正常。

    【讨论】:

      猜你喜欢
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多