路由事件和依赖属性 .NET 包装器混淆

发布于 2024-12-06 04:50:41 字数 563 浏览 0 评论 0原文

我是 WPF 新手,对路由事件和依赖属性的包装语法感到困惑 我在许多来源中看到路由事件和依赖属性是这样包装的,

// Routed Event
public event RoutedEventHandler Click
{
 add
 {
  base.AddHandler(ButtonBase.ClickEvent, value);
 }
 remove
 {
  base.RemoveHandler(ButtonBase.ClickEvent, value);
 }
}

// Dependency Property
public Thickness Margin
{
 set { SetValue(MarginProperty, value); }
 get { return (Thickness)GetValue(MarginProperty); }
}

我从未见过在 C# 中添加/删除/设置/获取某种关键字。这些是 C# 语言的一部分,作为关键字,但我从未体验过或使用过它们,因为我没有作为专业人士使用 C#,我是一名 C++ 程序员?如果不是关键字,那么编译器如何处理它们(如果它们不是 C# 的一部分)以及它们如何工作

I'm new to WPF and have a confusion about wrapping syntax of routed events and dependency properties
I've seen on many sources that routed events and dependency properties are wrapped like this

// Routed Event
public event RoutedEventHandler Click
{
 add
 {
  base.AddHandler(ButtonBase.ClickEvent, value);
 }
 remove
 {
  base.RemoveHandler(ButtonBase.ClickEvent, value);
 }
}

// Dependency Property
public Thickness Margin
{
 set { SetValue(MarginProperty, value); }
 get { return (Thickness)GetValue(MarginProperty); }
}

I have never seen add / remove / set / get sort of keywords in C#. Are these are part of C# language as Keywords and i never experienced or worked with them because i didn't worked in C# as pro i'm a C++ programmer? If not keywords then how they are handled by compiler if they are not part of C# and how they are working

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

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

发布评论

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

评论(1

顾忌 2024-12-13 04:50:41

我将尝试为您总结一下:

依赖属性:

public int MyProperty
{
    get { return (int)GetValue(MyPropertyProperty); }
    set { SetValue(MyPropertyProperty, value); }
}

// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
    DependencyProperty.Register("MyProperty", typeof(int), typeof(MyClass), new UIPropertyMetadata(MyDefaultValue));

这是完整的语法,您不必记住它,只需使用 Visual Studio 中的“propdp”片段即可。
“get”必须返回它所引用的类型的值(在我的示例中为 int)。每当您调用

int MyVar = MyProperty;

“get”内的代码时,都会对其进行求值。
该集合具有类似的机制,只是您有另一个关键字:“value”,它将是您分配给 MyVariable 的值:

MyProperty = 1;

将调用 MyProperty 的“集合”,并且“值”将为“1”。

现在对于 RoutedEvents:

在 C# 中(如在 C++ 中,如果我错了,请纠正我),要订阅事件,您

MyProperty.MyEvent += MyEventHandler;

将调用“add” -->;您正在向堆栈添加一个处理程序。
现在,由于它不会自动进行垃圾收集,并且我们希望避免内存泄漏,因此我们这样做:

MyProperty.MyEvent -= MyEventHandler;

以便当我们不再需要它时可以安全地处置我们的对象。
这就是“remove”表达式被求值的时候。

这些机制允许您在单个“get”上执行多项操作,WPF 中广泛使用的示例是:

private int m_MyProperty;
public int MyProperty
{
   get
   {
      return m_MyProperty;
   }
   set
   {
      if(m_MyProperty != value)
      {
         m_MyProperty = value;
         RaisePropertyChanged("MyProperty");
       }
    }
}

在实现 INotifyPropertyChanged 的​​ ViewModel 中,将通知视图中的绑定该属性已更改并且需要检索再次(所以他们会调用“get”)

I'm gonna try to sum it up for you:

Dependency property:

public int MyProperty
{
    get { return (int)GetValue(MyPropertyProperty); }
    set { SetValue(MyPropertyProperty, value); }
}

// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
    DependencyProperty.Register("MyProperty", typeof(int), typeof(MyClass), new UIPropertyMetadata(MyDefaultValue));

That's the full syntax, you don't have to memorize it, just use the "propdp" snippet in Visual Studio.
The "get" must return a value of the type it refers to (in my example, int). Whenever you call

int MyVar = MyProperty;

The code inside "get" is evaluated.
The set has a similar mechanism, only you have another keyword: "value" which will be the value you assign to MyVariable:

MyProperty = 1;

Will call the "set" of MyProperty and "value" will be "1".

Now for the RoutedEvents:

In C# (as in C++, correct me if i'm wrong), to subscribe to an event, you do

MyProperty.MyEvent += MyEventHandler;

That will call the "add" --> you're adding a handler to the stack.
Now since it is not automatically garbage-collected, and we want to avoid memory leaks, we do:

MyProperty.MyEvent -= MyEventHandler;

So that our object can be safely disposed of when we don't need it anymore.
That's when the "remove" expression is evaluated.

Those mechanism allow you to do multiple things on a single "get", a widely used example in WPF would be:

private int m_MyProperty;
public int MyProperty
{
   get
   {
      return m_MyProperty;
   }
   set
   {
      if(m_MyProperty != value)
      {
         m_MyProperty = value;
         RaisePropertyChanged("MyProperty");
       }
    }
}

Which, in a ViewModel that implements INotifyPropertyChanged, will notify the bindings in your View that the property has changed and need to be retrieved again (so they will call the "get")

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