此功能在以前的 .NET 中是否可用

发布于 2024-12-27 04:07:01 字数 990 浏览 3 评论 0原文

我正在查看 Charles Petzold 的一些代码,他正在使用以下内容PropertyChangedCallback 的语法

public static readonly DependencyProperty CenterProperty =
  EllipseGeometry.CenterProperty.AddOwner(
    typeof(CenteredEllipse),
    new FrameworkPropertyMetadata(new Point(0, 0), 
      EllipsePropertyChanged));

如果您这样做,则不起作用,您需要将属性更改回调放入 new new PropertyChangedCallback(EllipsePropertyChanged)。他也在直接编辑变量:

 void EllipsePropertyChanged(DependencyPropertyChangedEventArgs args)
{
  elipGeo.Center = Center;
  elipGeo.RadiusX = RadiusX;
  elipGeo.RadiusY = RadiusY;
  InvalidateMeasure();
}

应该是这样的:

static void EllipsePropertyChnaged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        EllipseGoemetry ellipseGeo = (EllipseGoemetry )obj;
        args.newValue ...
    }

有人知道发生了什么事吗?

I am looking at some code from Charles Petzold, he is using the following syntax for PropertyChangedCallback

public static readonly DependencyProperty CenterProperty =
  EllipseGeometry.CenterProperty.AddOwner(
    typeof(CenteredEllipse),
    new FrameworkPropertyMetadata(new Point(0, 0), 
      EllipsePropertyChanged));

It doesn't work if you do it this way you need put the property changed callback in new new PropertyChangedCallback(EllipsePropertyChanged). He is also editing the variables directly:

 void EllipsePropertyChanged(DependencyPropertyChangedEventArgs args)
{
  elipGeo.Center = Center;
  elipGeo.RadiusX = RadiusX;
  elipGeo.RadiusY = RadiusY;
  InvalidateMeasure();
}

Should it be something like this:

static void EllipsePropertyChnaged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        EllipseGoemetry ellipseGeo = (EllipseGoemetry )obj;
        args.newValue ...
    }

Does someone know what is going on?

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

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

发布评论

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

评论(3

暖风昔人 2025-01-03 04:07:01

第一个是委托类型推断的示例。从此: http: //blogs.msdn.com/b/noahc/archive/2008/09/23/delegate-type-in​​ference-in-c.aspx

直到今天我才知道第 13 行会起作用,你
可以只传递方法名称作为参数和委托类型
就会被推断出来。又好又容易。第#14/#15行对我来说也是新的,
至少是关于使用 () 来表示空参数列表的部分。

The first one is an example of Delegate Type Inference. From this: http://blogs.msdn.com/b/noahc/archive/2008/09/23/delegate-type-inference-in-c.aspx

What I didn't know until today is that line #13 would work, that you
can just pass the method name as the parameter and the delegate type
would be inferred. Nice and easy. Line #14/#15 was also new to me,
well at least the part about using () for an empty parameter list.

画▽骨i 2025-01-03 04:07:01

elipGeo 被定义为类的一个字段,该代码是正确的。

您不需要从方法显式创建委托,这是隐式完成的,此功能是在 C# 2.0 中添加的。

elipGeo is defined as a field of the class, that code is correct.

You do not need to explicity create delegates from methods, that is done implicitly, this feature was added in C# 2.0.

↘人皮目录ツ 2025-01-03 04:07:01

问题是他对两个方法使用相同的名称,因此可能会造成混淆:

static void EllipsePropertyChanged(DependencyObject obj,
  DependencyPropertyChangedEventArgs args)
{
  (obj as CenteredEllipse).EllipsePropertyChanged(args);
}

void EllipsePropertyChanged(DependencyPropertyChangedEventArgs args)
{
  elipGeo.Center = Center;
  elipGeo.RadiusX = RadiusX;
  elipGeo.RadiusY = RadiusY;
  InvalidateMeasure();
}

第一个是委托,第二个只是该类的一个方法,他在委托中调用该方法,在其中强制转换 obj,只是正如它应该做的那样。

The thing is that he uses the same names for two method so it can get confusing:

static void EllipsePropertyChanged(DependencyObject obj,
  DependencyPropertyChangedEventArgs args)
{
  (obj as CenteredEllipse).EllipsePropertyChanged(args);
}

void EllipsePropertyChanged(DependencyPropertyChangedEventArgs args)
{
  elipGeo.Center = Center;
  elipGeo.RadiusX = RadiusX;
  elipGeo.RadiusY = RadiusY;
  InvalidateMeasure();
}

The first one is the delegate, the second one is just a method of the class, which he is calling in the delegate, where he casts obj, just as it supposed to be done.

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