Я получаю исключение в моей C#/NET приложение, которое гласит:
CommandConverter - Действительно исключение или .NET Bug
'CommandCoverter' не в состоянии преобразовать 'MyNamespace.MyDerivedFromICommandSubclass' в «системе. Строка».
Что я делаю это довольно прямо вперед, как описано в документации MSDN ICommand:
public class MyDerivedFromICommandSubclass : ICommand
{
// Implement interface
...
}
У меня есть FlowDocument с Hyperlink на нем. Гиперссылке разрешено иметь свойство Command, которое я установил для своей производной ICommand, чтобы при нажатии ссылки было выполнено мое пользовательское действие.
Эта часть работает.
Вот где я попадаю в неприятности: если я выберу гиперссылку и щелкните правой кнопкой мыши Копировать (или нажмите Control-C).
Мгновенно платформа .NET генерирует исключение System.NotSupportedException с подробным описанием выше. Трассировки стека показывает:
на System.ComponentModel.TypeConverter.GetConvertToException (значение объекта, тип DestinationType)
в System.Windows.Input.CommandConverter.ConvertTo (ITypeDescriptorContext контекст, CultureInfo культуры, значение объекта, тип DestinationType)
в этот момент я прибег к Red Gate's free .NET Reflector и смотрел на исходном коде для ConvertTo
:
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null) // We know it isn't, it's System.String
{
throw new ArgumentNullException("destinationType"); // We don't see this exception
}
if (destinationType == typeof(string)) // It is, so control passes in
{
if (value == null) // It isn't, so this condition is skipped
{
return string.Empty; // Confirmed we don't get this return value
}
RoutedCommand command = value as RoutedCommand;
if (((command != null) && (command.OwnerType != null) && IsKnownType(command.OwnerType))
{ // Is a user-defined ICommand a known type? Doubtful. This gets skipped.
return command.Name; // Confirmed we don't get this return value
}
// It has to fall through then if no return is done!
}
throw base.GetConvertToException(value, destinationType); // BOOM!
// value is my custom ICommand and destinationType is System.String
}
Итак, тогда возникает вопрос, так как все это происходит внутри .NET, я делаю что-то неправильно, и если да, то что? Или это ошибка .NET, и если да, есть ли работа?
Спасибо за любую помощь.
Для этого в C#, а не XAML, это делается так: hyperlink.SetValue (HyperlinkHelper.CommandProperty, CustomCommand1); –
Требуемое чтение. http://blog.hackedbrain.com/articles/UnderstandingDependencyObjectAndDependencyProperty.aspx –
И это. Прикрепленные свойства - http://msdn.microsoft.com/en-us/library/ms749011.aspx –