【问题标题】:How to create a Custom Routed Event ? WPF c#如何创建自定义路由事件? WPF C#
【发布时间】:2013-09-05 13:31:44
【问题描述】:

我关注了this tutorial,但我无法将学到的知识应用到我的项目中。

我有一个LineGraph 对象(Dynamic Data Display),我想创建一个在 LineGraph 的粗细等于 0 时引发的事件。

我应该如何按照本教程编写它?

【问题讨论】:

  • 您遇到的具体问题是什么,为什么不能应用本教程?
  • 我不工作 D3 但似乎 LineGraph 对象没有厚度属性?
  • 你能贴一些代码让我们看看问题所在吗?
  • 已经很晚了,但对于其他一些寻求者来说:msdn.microsoft.com/de-de/library/ms752288(v=vs.110).aspx

标签: c# wpf dynamic-data-display


【解决方案1】:

以下是我将如何使用 RoutedEvent:

  1. 创建一个派生自LineGraph 的类,比如CustomLineGraph

    public class CustomLineGraph : LineGraph {
    }
    
  2. 像这样创建我们的路由事件:

    public class CustomLineGraph : LineGraph {
    
        public static readonly RoutedEvent ThicknessEvent = EventManager.RegisterRoutedEvent("Thickness", RoutingStrategy.Bubble, typeof(RoutedEventHandler, typeof(CustomLineGraph));
    
        // .NET event wrapper
        public event RoutedEventHandler Thickness
        {
            add { AddHandler(CustomLineGraph.ThicknessEvent, value); } 
            remove { RemoveHandler(CustomLineGraph.ThicknessEvent, value); }
        }
    }
    
  3. 现在我们重写 StrokeThickness 属性,以便在该属性的值为 0 时引发自定义路由事件。

    public class CustomLineGraph : LineGraph {
    
        public static readonly RoutedEvent ThicknessEvent = EventManager.RegisterRoutedEvent("Thickness", RoutingStrategy.Bubble, typeof(RoutedEventHandler, typeof(CustomLineGraph));
    
        // .NET event wrapper
        public event RoutedEventHandler Thickness
        {
            add { AddHandler(CustomLineGraph.ThicknessEvent, value); } 
            remove { RemoveHandler(CustomLineGraph.ThicknessEvent, value); }
        }
    
        public override double StrokeThickness {
            get { return base.StrokeThickness; }
            set
            {
                base.StrokeThickness = value;
                if (value == 0)
                    RaiseEvent(new RoutedEventArgs(CustomLineGraph.ThicknessEvent, this));
            }
        }
    }
    
  4. 我们完成了!

【讨论】:

    【解决方案2】:

    就个人而言,我通常避免创建事件,而是更喜欢创建delegates。如果出于某些特殊原因您特别需要某个事件,请忽略此答案。我更喜欢使用delegates 的原因是你不需要创建额外的EventArgs 类,我也可以设置自己的参数类型。

    首先,让我们创建一个委托:

    public delegate void TypeOfDelegate(YourDataType dataInstance);
    

    现在是 getter 和 setter:

    public TypeOfDelegate DelegateProperty { get; set; }
    

    现在让我们创建一个匹配delegate的in和out参数的方法:

    public void CanBeCalledAnything(YourDataType dataInstance)
    {
        // do something with the dataInstance parameter
    }
    

    现在我们可以将此方法设置为delegate 的(众多)处理程序之一:

    DelegateProperty += CanBeCalledAnything;
    

    最后,让我们调用我们的delegate...这相当于引发事件:

    if (DelegateProperty != null) DelegateProperty(dataInstanceOfTypeYourDataType);
    

    注意null 的重要检查。就是这样了!如果您想要更多或更少的参数,只需在delegate 声明中添加或删除它们,处理方法...简单。

    【讨论】:

      猜你喜欢
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      相关资源
      最近更新 更多