事件处理程序多次订阅

时间:2015-10-22 17:28:38

标签: wpf dependency-properties

我是WPF新手,所以请放轻松。

我有一个使用依赖属性的按钮,以便使用鼠标离开事件,这部分可以正常使用它自己。这是执行此操作的代码...

private View imageViewArray[];
private Random rand;
private int layoutwidth;
private int layoutheight;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rand = new Random();
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    layoutwidth = size.x;
    layoutheight = size.y;
    imageViewArray = new ImageView[10];
    for (int i = 0; i < 10; i++) {
        imageViewArray[i] = new View(this);
        imageViewArray[i].setTag(i);
        randomx = rand.nextInt((layoutwidth - layoutwidth / 3) + layoutwidth / 3);
        randomy = rand.nextInt(layoutheight - layoutheight / 3);
        imageViewArray[i].setX(randomx);
        imageViewArray[i].setY(randomy);
        rlt.addView(imageViewArray[i]);
    }

按钮的xaml:

imageViewArray[i].set?

我也有一些我在互联网上挖出的代码,它基本上显示了当前形式的模式对话框。这段代码太多了我输入这个问题,但简而言之,它创建了一个新的Dialog控件,然后将父控件内容设置为Dialog。我不能比这更具体,因为我不完全理解它是如何工作的。我知道它不会有多大帮助,但这是显示Dialog的方法。

// ==================================
// Mouse Leave Behavior
public static readonly DependencyProperty MouseLeaveProperty = DependencyProperty.RegisterAttached(
    "MouseLeave", typeof(ICommand), typeof(MouseBehaviors), new PropertyMetadata(null, MouseLeaveChanged));

public static ICommand GetMouseLeave(DependencyObject obj) {
    return (ICommand)obj.GetValue(MouseLeaveProperty);
}

public static void SetMouseLeave(DependencyObject obj, ICommand value) {
    obj.SetValue(MouseLeaveProperty, value);
}

private static void MouseLeaveChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
    UIElement uiElement = obj as UIElement;

    if (uiElement != null)
        uiElement.MouseLeave += new MouseEventHandler(uiElement_MouseLeave);
}

static void uiElement_MouseLeave(object sender, MouseEventArgs e) {
    UIElement uiElement = sender as UIElement;
    if (uiElement != null) {
        ICommand command = GetMouseLeave(uiElement);
        if (command != null)
            command.Execute(uiElement);
    }
}

我遇到的问题是:只要将Dialog控件设置为父级的Content属性,就会再次调用MouseLeaveChanged静态方法(即 - 当最后一行上面显示的代码被执行)。那么最终发生的事情是MouseLeave事件第二次被注册(订阅??)到按钮。当Dialog控件关闭时,第三次发生同样的事情。当Dialog控件显示/关闭时,我需要更改什么才能停止MouseLeaveChanged方法再次触发?或者,当MouseLeaveChanged第二次触发时,解决方案是否以某种方式从按钮中删除了MouseLeave事件?

我在MouseLeaveChanged方法中进行了一些调试,并注意到每次调用方法时事件args(DependencyPropertyChangedEventArgs e)都会改变。

当表单最初加载并且MouseLeaveChanged方法触发时,调试e.NewValue会产生以下结果: {} MDCT.RelayCommand     _canExecute:null     _execute:{Method = {Void b__1(System.Object)}}

调试e.OldValue会产生null。

但是,当显示Dialog控件并再次触发MouseLeaveChanged方法时,调试上述值会显示它们已交换。换句话说,e.NewValue产生null,e.OldValue产生MDCT.RelayCommand。据推测,这表明此时注册的鼠标离开事件应该取消订阅(uiElement.MouseLeave - = ?????)?

0 个答案:

没有答案
相关问题