将事件分配给另一个wpf窗口 [英] assign an event to another wpf window

查看:108
本文介绍了将事件分配给另一个wpf窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了WPF4这本书的活动部分,但是我找不到我要找的东西。



例如我有两个窗口(两个都是打开的)。



我在第一个上有一个按钮和一个文本框在第二个(数字作为内容)。



每次我点击按钮我想要文本框'' s号增加



期待您的帮助和建议!

解决方案

建议......真的听起来很有趣,如果遇到这样的问题。



首先,分配事件一词表明对事物本质缺乏了解。事件永远不会分配。在某些类中声明,并且您永远不能明确地为它分配任何内容,而且,您永远不能初始化它。每次向其调用列表添加处理程序时,实际上都会创建事件实例。当某些代码已经添加了一个事件处理程序时,看起来当您将另一个处理程序添加到事件的同一个实例时,您可能会修改它,但令人惊讶的是,情况并非如此。 事件实例是不可变每次添加处理程序(使用+ =运算符)或删除它( - +运算符)时,实际上都会创建一个全新的事件实例并进行修改调用列表。这种行为的原因之一,如在其他不可变对象的情况下(字符串是一个众所周知的例子)是多线程优化。



最后,你可以调用一个事件实例,但仅在声明类中。这个重要限制制作事件实例与常规委托实例不同,重要的防呆功能。当你在谈论事件时 System.Windows.Controls.Primitives.ButtonBase.Click http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives。 buttonbase.click%28v = vs.95%29.aspx [ ^ ],您不能直接调用此事件(但是,间接地,您可以编写一个派生自<$的类) c $ c>按钮调用受保护的方法 System.Windows.Controls.Primitives.Button.OnClick http://msdn.microsoft.com/en-us/library/ system.windows.controls.button.onclick%28v = vs.95%29.aspx [ ^ ])。



我认为,现在,您需要了解事件的使用情况。重要的是,没有任务。



您需要做的只是+ =,用于添加事件处理程序。事件处理程序将是在调用事件实例期间调用的委托实例。由于委托实例对传入调用处理方法的对象进行引用,因此处理程序方法可以是任何其他对象的方法,与拥有事件实例的对象无关。如果这是一个窗口,它可以是同一个窗口或任何其他窗口。



唯一的问题是传递表单之间处理所需的引用。在您的情况下,最方便的方法是将引用传递给事件实例,因为它通常已经声明为 public internal 。您需要在内容中添加处理程序,其中对事件实例的引用和拥有 TextBox 的窗口都是已知的。类似于:



 MyWindowmWithButton buttonWindow =  //   ...  

MyWindowWithTextBox numberWindow = // ...

// ...

buttonWindow.myButton.Click + =()= > {
int current;
if (!int.TryParse(numberWindow.MyTextBox.Text, out current){
// 有问题,处理或修复错误
}
numberWindow.MyTextBox.Text =(++ current).ToString();
};





这将有效。同时,创建这样的上下文并不好,两个窗口的内部细节都是可见的,它会违反完美的窗口封装。更精细的解决方案是这样的:你用一个返回引用的方法创建一些接口到事件实例并在拥有该事件的窗口中实现它。将接口实例传递给其他表单并在其上下文中添加处理程序。



-SA

I read the event part of the book WPF4 unleashed but I didn''t find what I''m looking for.

For exemple I have two windows (both are open).

I have a button on the first one and a textbox in the second (with a number as a content) .

Everytime I click the button I want the textbox''s number increased.

Looking forward to your help and suggestions!

解决方案

"Suggestions"… it really sounds funny, in case of such a "problem".

First of all, the expression "assign an event" demonstrates the lack of understanding of the nature of things. The events are never "assigned". The are declared in some class, and you never can assign anything to it explicitly, moreover, you can never initialize it. The event instance is actually created each time you add a handler to its invocation list. When some code already added one event handler, it may seem that when you add another handler to the same instance of the event you modify it, but, surprisingly, this is not the case. Event instanced are immutable. Each time you add a handler (with += operator) or removed it (-+ operator), you actually create a brand new event instance with modified invocation list. One of the reasons for such behavior, as in the case of other immutable objects (string is a well-known example) is multithreading optimization.

Finally, you can invoke an event instance, but only in the declaring class. This one of the important limitation making event instanced different from "regular" delegate instances, an important fool-proof feature. As you are talking about the event System.Windows.Controls.Primitives.ButtonBase.Click, http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.click%28v=vs.95%29.aspx[^], you cannot invoke this event directly (however, indirectly, you can write a class derived from Button to call the protected method System.Windows.Controls.Primitives.Button.OnClick, http://msdn.microsoft.com/en-us/library/system.windows.controls.button.onclick%28v=vs.95%29.aspx[^]).

I think, for now it''s all you need to know on event use. Importantly, no "assignments".

All you need to do is "+=", to be used to add an event handler. The event handler will be a delegate instance invoked during the invocation of the event instance. As delegate instanced carry reference to the object on which the handling method is called, the handler method can be a method of any other object, not related to the one owning the event instance. If this is a window, it can be the same window or any other one.

The only "problem" will be passing the references needed for handling between forms. In your case, the most convenient way would be passing the reference to the event instance, as it is usually already declared as public or internal. You need to add the handler in the content where both the reference to the event instance and the window owning the TextBox are known. Something like:

MyWindowmWithButton buttonWindow = // ...

MyWindowWithTextBox numberWindow = // ...

//...

buttonWindow.myButton.Click += () => {
    int current;
    if (!int.TryParse(numberWindow.MyTextBox.Text, out current) {
        // something's wrong, handle it or fix the bug
    }
    numberWindow.MyTextBox.Text = (++current).ToString();
};



This will work. At the same time, it''s not good to create such context where the internal detail of both windows are visible, it would violate perfect encapsulation of windows. The finer solution would be this: you create some interface with a method returning the referenced to event instance and implemented it in the windows owning that event. Pass just the interface instance to other form and add the handler in its context.

—SA


这篇关于将事件分配给另一个wpf窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆