[英]Why registering two identical attached properties works in Silverlight but fails in WPF?
看看這段代碼:
var property = DependencyProperty.RegisterAttached(
"SomeAttachedProperty",
typeof(object),
typeof(View),
new PropertyMetadata(default(object)));
var sameProperty = DependencyProperty.RegisterAttached(
"SomeAttachedProperty",
typeof(object),
typeof(View),
new PropertyMetadata(default(object)));
第二次注冊在WPF中失敗, AgrumentException
表示“已注冊屬性”(這是正確的)。
在反編譯的WPF DependencyProperty
源代碼中,我發現:
FromNameKey key = new FromNameKey(name, ownerType);
lock (Synchronized)
{
if (PropertyFromName.Contains(key))
{
throw new ArgumentException(SR.Get(SRID.PropertyAlreadyRegistered,
name, ownerType.Name));
}
}
但在SL中,我沒有找到任何具有相同名稱和類型的現有屬性的檢查。
所以問題是SL為什么不檢查它,但WPF呢? 是否有一些基本限制或什么?
謝謝。
在沒有靜態Get和Set訪問器的情況下,附加依賴項屬性的注冊是不完整的,甚至在Silverlight中,您也無法在同一個類中編寫它們兩次。
class MayClass
{
public static readonly DependencyProperty SomethingProperty =
DependencyProperty.RegisterAttached(
"Something", typeof(object), typeof(MyClass));
// required
public static object GetSomething(UIElement element)
{
return element.GetValue(SomethingProperty );
}
// required
public static void SetSomething(UIElement element, object value)
{
element.SetValue(SomethingProperty , value);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.