路由事件和依赖属性 .NET 包装器混淆
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将尝试为您总结一下:
依赖属性:
这是完整的语法,您不必记住它,只需使用 Visual Studio 中的“propdp”片段即可。
“get”必须返回它所引用的类型的值(在我的示例中为 int)。每当您调用
“get”内的代码时,都会对其进行求值。
该集合具有类似的机制,只是您有另一个关键字:“value”,它将是您分配给 MyVariable 的值:
将调用 MyProperty 的“集合”,并且“值”将为“1”。
现在对于 RoutedEvents:
在 C# 中(如在 C++ 中,如果我错了,请纠正我),要订阅事件,您
将调用“add” -->;您正在向堆栈添加一个处理程序。
现在,由于它不会自动进行垃圾收集,并且我们希望避免内存泄漏,因此我们这样做:
以便当我们不再需要它时可以安全地处置我们的对象。
这就是“remove”表达式被求值的时候。
这些机制允许您在单个“get”上执行多项操作,WPF 中广泛使用的示例是:
在实现 INotifyPropertyChanged 的 ViewModel 中,将通知视图中的绑定该属性已更改并且需要检索再次(所以他们会调用“get”)
I'm gonna try to sum it up for you:
Dependency property:
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
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:
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
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:
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:
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")