Skip to main content
 首页 » 编程设计

c#之无法将属性 'False' 中的字符串 'X' 转换为类型 'System.Boolean' 的对象

2024年02月23日20mate10pro

我创建了一个 AttachedProperty,它在测试项目中工作得很好,但在更大的项目中,Attached Property 在一个项目中定义并在其他项目中使用,我遇到了这个奇怪的错误:

System.Windows.Markup.XamlParseException occurred 
    Message=Cannot convert string 'False' in attribute 'IsTextDragSource' 
            to object of type 'System.Boolean'. 
    Error at object 'System.Windows.HierarchicalDataTemplate' in markup file 
   'ServerMonitoringModule;component/view/lists/jobbrowser/jobreportdetailsview.xaml' 
   Line 147 Position 84.  Source=PresentationFramework   
   LineNumber=147   LinePosition=84  NameContext=Resources 
StackTrace: 
        at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition,  Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType) 
        at System.Windows.Markup.XamlParseException.ThrowException(ParserContext parserContext, Int32 lineNumber, Int32 linePosition, String message, Exception innerException) 
        at System.Windows.Markup.XamlTypeMapper.ParseProperty(Object targetObject, Type propType, String propName, Object dpOrPiOrFi, ITypeDescriptorContext typeContext, ParserContext parserContext, String value, Int16 converterTypeId) 
        at System.Windows.Markup.OptimizedTemplateContent.ParseDependencyProperty(String attribValue, Int16 attributeId, Int16 converterTypeId, DependencyProperty& dp, Object& propertyValue) 
        at System.Windows.Markup.OptimizedTemplateContent.LookForShareableRecord(BamlRecord bamlRecord, DependencyProperty& dp, Object& dpValue) 
        at System.Windows.Markup.OptimizedTemplateContent.ReadPotentiallyShareableRecord(BamlRecord bamlRecord) 
        at System.Windows.Markup.OptimizedTemplateContent.ReadRecord(BamlRecord bamlRecord) ... 

Line where error is reported:

<TextBlock Text="{Binding Text}" dscb:TextDragHelper.IsTextDragSource="False"/> 

以及属性的定义:

public class TextDragHelper : DependencyObject 
{ 
    public static readonly DependencyProperty IsTextDragSourceProperty = 
        DependencyProperty.RegisterAttached("IsTextDragSource", 
            typeof(bool), typeof(TextDragHelper), 
            new FrameworkPropertyMetadata(OnDragSourceAdvisorChanged)); 

有没有人遇到过这个问题或者有解决办法?

请您参考如下方法:

谢谢 - BoltClock - 你没有回答问题,但我查看了我的来源并发现我的 setter 方法是:

public static void SetIsTextDragSource(DependencyObject source, object value) 
{ 
    source.SetValue(IsTextDragSourceProperty, value); 
} 

虽然这个以某种方式在单个项目解决方案中起作用,但在多项目解决方案中却不起作用。

这修复了它:

public static void SetIsTextDragSource(DependencyObject source, bool value) 
{ 
    source.SetValue(IsTextDragSourceProperty, value); 
}