是否可以为标准类设置事件冒泡和事件调整,而不仅仅是 XAML 类?

发布于 2024-11-02 19:34:03 字数 302 浏览 0 评论 0原文

在 WPF 或 Silverlight 中,是否可以拥有与 GUI 类不关联的标准类的自定义层次结构,并为该层次结构设置通风口冒泡和事件调节?如何 ?

在查看事件时,我没有看到任何与冒泡或隧道真正相关的内容 http://msdn.microsoft.com/en-us/library /system.windows.uielement_events.aspx

In a WPF or Silverlight Is it possible to have a custom hierarchy of standard classes not associated with GUI classes and setup vent Bubbling and Event Tunelling for this hierarchy ? How ?

when looking at events I don't see anything really related to bubbling or tunneling
http://msdn.microsoft.com/en-us/library/system.windows.uielement_events.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭水袖舞倾城 2024-11-09 19:34:03

路由事件要求对象继承 UIElement 类,这几乎注定了它是一个 GUI 控件。

不过,所有此类都可以从代码隐藏(C#/VB/...)中实例化。 XAML 只是提供了一种以声明方式定义对象的便捷方法,它不会执行任何无法通过代码隐藏完成的操作。

Routed events require that the object inherits from UIElement class, which pretty much destines it to be a GUI control.

All such classes can be instantiated from code behind (C#/VB/...) though. XAML just provides a convenient way to declaratively define objects, it doesn't do anything that can't be done from code behind.

如梦初醒的夏天 2024-11-09 19:34:03

除非您创建类似于 EventManager 类为您提供的功能的东西,否则不可能使用 .net 框架。
假设你开始实施。然后你就必须向上遍历&沿着对象图向下(如果您的类层次结构不是树)。为此,您需要以某种方式确定哪些对象实际上是树的一部分,哪些不是。
假设您有以下情况:

class A
{
   public B b;
}

class B
{

}


B b1 = new B();
A a1 = new A() { b = b1 };
A a2 = new A() { b = b1 };

假设 b1 引发冒泡事件。应该向哪位家长冒泡? a1还是a2?
冒泡& WPF 中的隧道Silverlight 之所以能够工作,是因为您的 UIElement 位于树中,而不是像上面的场景那样位于图表中。但任意的类层次结构都是图而不是树。
因此,如果您开始实现类似的东西,它只适用于类似于以下内容的树类。

class Node 
{
   Node parent;
   //other members
}

然后你就会知道如何始终使用“parent”属性来冒泡。

但是,在开始实现 EventManager 之前,我建议您再看一下 RoutedEvents 是什么以及使用它们的场景是什么:http://msdn.microsoft.com/en-us/library/ms742806.aspx
另外,也许您可​​以向我们提供路由事件对于标准(非 ui)类层次结构(树)有用的场景,看看我们是否可以找到 RoutedEvents 的替代方案。

It's not possible using the .net framework, unless you create something similar to the functionality the EventManager class provides you.
Suppose you started implementing. Then you would have to traverse up & down a graph of objects (if your class hierarchy isn't a tree). For this you would somehow need to identify which objects are actually are part of the tree and which aren't.
Suppose you have the following:

class A
{
   public B b;
}

class B
{

}


B b1 = new B();
A a1 = new A() { b = b1 };
A a2 = new A() { b = b1 };

Suppose b1 raises a bubbling event. To which parent should it bubble? a1 or a2?
Bubbling & tunneling in WPF & Silverlight work because you have UIElements which are in a Tree, not in a graph like in the scenario above. But arbitrary class hierachies are graphs not trees.
So if you would start implementing something similar it would only work for a tree class similar to the following.

class Node 
{
   Node parent;
   //other members
}

Then you would know how to always bubble using the "parent" property.

But, before you start implementing your EventManager, I would suggest you take another look at what RoutedEvents are and what are the scenarios to use them: http://msdn.microsoft.com/en-us/library/ms742806.aspx
Also maybe you could provide us with the scenarios where routed events would be useful for a standard (non-ui) class hierachy (tree) to see if we can find an alternative to RoutedEvents.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文