路由事件和依赖项属性.NET包装器混乱 [英] Routed events and dependency properties .NET wrapper confusion
问题描述
我是WPF的新手,对包装路由事件和依赖项属性的语法感到困惑 我在许多来源中都看到,路由事件和依赖项属性是像这样包装的
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); }
}
我从未见过在C#中添加/删除/设置/获取某种关键字.这些是作为关键字的C#语言的一部分吗,并且因为我不是C ++程序员而没有在C#中工作,所以我从未经历过或与他们合作过?如果不是关键字,那么如果它们不是C#的一部分,那么编译器将如何处理它们以及它们如何工作
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
推荐答案
我将尝试为您总结一下:
I'm gonna try to sum it up for you:
依赖性属性:
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).每当你打电话
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;
评估"get"中的代码.
该集合具有类似的机制,只有您拥有另一个关键字:"value",这将是您分配给MyVariable的值:
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;
将调用MyProperty的集合",值"将为"1".
Will call the "set" of MyProperty and "value" will be "1".
现在为RoutedEvents:
Now for the RoutedEvents:
在C#中(就像在C ++中一样,如果我错了,请纠正我),您可以订阅事件
In C# (as in C++, correct me if i'm wrong), to subscribe to an event, you do
MyProperty.MyEvent += MyEventHandler;
这将称为"add"->您正在向堆栈中添加一个处理程序. 现在,由于不会自动对其进行垃圾收集,并且我们想避免内存泄漏,因此我们可以这样做:
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.
这种机制可以让您一次完成多个操作,在WPF中广泛使用的示例是:
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");
}
}
}
在实现INotifyPropertyChanged的ViewModel中,它将通知View中的绑定属性已更改并且需要再次检索(因此它们将称为"get")
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")
这篇关于路由事件和依赖项属性.NET包装器混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!