【问题标题】:silverlight commanding event callback objectsilverlight 命令事件回调对象
【发布时间】:2012-09-27 06:35:19
【问题描述】:

我将按钮传递给我的 MouseEventCommand 类。

我的命令类在这里

    public static class MouseEnterCommand
{
    public static ICommand GetCommand(Button button)
    {
        return button.GetValue(CommandProperty) as ICommand;
    }

    public static void SetCommand(Button button, ICommand command)
    {
        button.SetValue(CommandProperty, command);
    }

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached(
            "Command",
            typeof (ICommand),
            typeof (MouseEnterCommand),
            new PropertyMetadata(OnSetCommandCallback));


    public static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
    {
        Button button = dependencyObject as Button;

        if (button != null)
        {
            MouseEnterChangedBehavior behavior = GetOrCreateObject(button);
            behavior.Command = eventArgs.NewValue as ICommand;
        }

    }

    private static MouseEnterChangedBehavior GetOrCreateObject(Button button)
    {
        MouseEnterChangedBehavior behavior = button.GetValue(MouseEnterCommandBehaviorProperty) as MouseEnterChangedBehavior;
        if (behavior == null)
        {
            behavior = new MouseEnterChangedBehavior(button);
            button.SetValue(MouseEnterCommandBehaviorProperty, behavior);
        }
        return behavior;
    }

    private static readonly DependencyProperty MouseEnterCommandBehaviorProperty =
        DependencyProperty.RegisterAttached(
            "MouseEnterCommandBehavior",
            typeof (object),
            typeof (MouseEnterCommand),
            null);

    public static ICommand GetCommandParameter(Button button)
    {
        return button.GetValue(CommandParameterProperty) as ICommand;
    }

    public static void SetCommandParameter(Button button, object parameter)
    {
        button.SetValue(CommandParameterProperty, parameter);
    }

    public static readonly DependencyProperty CommandParameterProperty =
      DependencyProperty.RegisterAttached(
        "CommandParameter",
        typeof(object),
        typeof(MouseEnterCommand),
        new PropertyMetadata(OnSetCommandParameterCallback));

    public static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
    {
        Button button = dependencyObject as Button;

        if (button != null)
        {
            MouseEnterChangedBehavior behavior = GetOrCreateObject(button);
            behavior.Command = eventArgs.NewValue as ICommand;
        }

    }
}

public class MouseEnterChangedBehavior:CommandBehaviorBase<Button>
{
    public MouseEnterChangedBehavior(Button targetObject) 
        : base(targetObject)
    {
        targetObject.MouseEnter += OnMouseEnter;
    }

    private void OnMouseEnter(object sender, MouseEventArgs e)
    {
        ExecuteCommand();
    }
}

我的silverlight代码在这里

        <Button Width="250"
            Height="60"
            Content="MyButton with MouseEnter"
            commands:MouseEnterCommand.Command="{Binding MouseEnterCommand}"
            commands:MouseEnterCommand.CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
            />

我的Modev视图是这样的

public class MouseEnterViewModel
{
    private ICommand mouseEnterCommand;

    public ICommand MouseEnterCommand
    {
        get
        {
            if(mouseEnterCommand==null)
            {
                mouseEnterCommand =new DelegateCommand<object>(OnButtonMouseEnter);
            }
            return mouseEnterCommand;
        }
    }

    public void OnButtonMouseEnter(object obj)
    {
        // !!!!!!!  obj is coming null ????????????????


    }
}

我的问题是我的 obj 对象即将为 Null。

我在 MouseEnterCommand 类中设置断点,我看到按钮来到这里并且 ExecuteCommand 运行良好。但是在模型视图中,obj 即将为空。

【问题讨论】:

    标签: wpf silverlight binding command prism


    【解决方案1】:

    您能否检查在 OnMouseEnter 事件处理程序中调用的 ExecuteCommand 方法。您不会再从该位置传递发件人对象。

    验证发送者对象是否应该作为输入参数传递给 ExecuteCommand()

    public class MouseEnterChangedBehavior:CommandBehaviorBase<Button>
    {
        public MouseEnterChangedBehavior(Button targetObject) 
            : base(targetObject)
        {
            targetObject.MouseEnter += OnMouseEnter;
        }
    
        private void OnMouseEnter(object sender, MouseEventArgs e)
        {  // check if you can pass sender object as input to this method
            ExecuteCommand();
        }
    }
    

    【讨论】:

    • 是的,我查过了。发件人对象作为按钮出现。你可以从这里看到。 postimage.org/image/xd3ii6f4l
    • 此执行命令还转到 MouseEnterViewModel 的 OnButtonMouseEnter(object obj) 方法。但是 obj 参数即将为空。
    猜你喜欢
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    相关资源
    最近更新 更多