问题描述
我希望通过使用以下代码(取自 here)在我的应用程序中使用附加属性。但是,由于我不知道的某种原因,Visual Studio 中的 Intellisense 不会显示已注册的附加属性。我的应用程序是一个加载项并被限制为类库(.Net Framework 4.7.2)
查看下面的代码并了解附加属性的定义,我认为附加属性会被定义为这样
public static readonly DependencyProperty ValueProperty = DependencyProperty.Registerattached(
"Value",typeof(Property),**typeof(Parent)**,new UIPropertyMetadata(
default(Property),new PropertyChangedCallback(OnValuePropertyChanged),new CoerceValueCallback(OnValuePropertyUpdated)
));
而不是这样
public static readonly DependencyProperty ValueProperty = DependencyProperty.Registerattached(
"Value",**typeof(BaseAttachedProperty<Parent,Property>)**,new CoerceValueCallback(OnValuePropertyUpdated)
));
注意 typeof(Parent)
和 typeof(BaseAttachedProperty<Parent,Property>)
之间的区别。但是,当我在 Visual Studio 类库中使用此代码时,Intellisense 不会显示已定义的附加属性,例如:
public class IsBusyProperty : BaseAttachedProperty<IsBusyProperty,bool>
{
}
Intellisense 显示了神秘信息:local:BaseAttachedProperty`2.Value
我理解它指的是带有两个参数的基类。是否有一些特殊的方法可以使用 WPF 处理泛型和基类,以便将附加属性识别为在此命名空间中并且也可以使用?我的目标是简化应用程序中附加属性的创建和使用。我已经清理、删除 bin
文件夹无济于事。但是,当我编写自己的 AP 时例如:
public class IsBusyProperty
{
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Registerattached("Value",typeof(bool),typeof(IsBusyProperty),new PropertyMetadata(false));
public static bool GetValue(DependencyObject obj)
{
return (bool)obj.GetValue(ValueProperty);
}
public static void SetValue(DependencyObject obj,bool value)
{
obj.SetValue(ValueProperty,value);
}
}
代码编译时没有出现可怕的错误 the attached property is not found in the namespace local:...
或 No implementation exception
!
这是取自 here 的参考代码。
using System;
using System.ComponentModel;
using System.Windows;
namespace Fasetto.Word
{
/// <summary>
/// A base attached property to replace the vanilla WPF attached property
/// </summary>
/// <typeparam name="Parent">The parent class to be the attached property</typeparam>
/// <typeparam name="Property">The type of this attached property</typeparam>
public abstract class BaseAttachedProperty<Parent,Property>
where Parent : new()
{
#region Public Events
/// <summary>
/// Fired when the value changes
/// </summary>
public event Action<DependencyObject,DependencyPropertyChangedEventArgs> ValueChanged = (sender,e) => { };
/// <summary>
/// Fired when the value changes,even when the value is the same
/// </summary>
public event Action<DependencyObject,object> ValueUpdated = (sender,value) => { };
#endregion
#region Public Properties
/// <summary>
/// A singleton instance of our parent class
/// </summary>
public static Parent Instance { get; private set; } = new Parent();
#endregion
#region Attached Property DeFinitions
/// <summary>
/// The attached property for this class
/// </summary>
public static readonly DependencyProperty ValueProperty = DependencyProperty.Registerattached(
"Value",typeof(BaseAttachedProperty<Parent,Property>),new CoerceValueCallback(OnValuePropertyUpdated)
));
/// <summary>
/// The callback event when the <see cref="ValueProperty"/> is changed
/// </summary>
/// <param name="d">The UI element that had it's property changed</param>
/// <param name="e">The arguments for the event</param>
private static void OnValuePropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
{
// Call the parent function
(Instance as BaseAttachedProperty<Parent,Property>)?.OnValueChanged(d,e);
// Call event listeners
(Instance as BaseAttachedProperty<Parent,Property>)?.ValueChanged(d,e);
}
/// <summary>
/// The callback event when the <see cref="ValueProperty"/> is changed,even if it is the same value
/// </summary>
/// <param name="d">The UI element that had it's property changed</param>
/// <param name="e">The arguments for the event</param>
private static object OnValuePropertyUpdated(DependencyObject d,object value)
{
// Call the parent function
(Instance as BaseAttachedProperty<Parent,Property>)?.OnValueUpdated(d,value);
// Call event listeners
(Instance as BaseAttachedProperty<Parent,Property>)?.ValueUpdated(d,value);
// Return the value
return value;
}
/// <summary>
/// Gets the attached property
/// </summary>
/// <param name="d">The element to get the property from</param>
/// <returns></returns>
public static Property GetValue(DependencyObject d) => (Property)d.GetValue(ValueProperty);
/// <summary>
/// Sets the attached property
/// </summary>
/// <param name="d">The element to get the property from</param>
/// <param name="value">The value to set the property to</param>
public static void SetValue(DependencyObject d,Property value) => d.SetValue(ValueProperty,value);
#endregion
#region Event Methods
/// <summary>
/// The method that is called when any attached property of this type is changed
/// </summary>
/// <param name="sender">The UI element that this property was changed for</param>
/// <param name="e">The arguments for this event</param>
public virtual void OnValueChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e) { }
/// <summary>
/// The method that is called when any attached property of this type is changed,even if the value is the same
/// </summary>
/// <param name="sender">The UI element that this property was changed for</param>
/// <param name="e">The arguments for this event</param>
public virtual void OnValueUpdated(DependencyObject sender,object value) { }
#endregion
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)