避免“目标类型'类型名称'不能转换为基类型System.String”

时间:2012-02-24 14:15:14

标签: xml xaml resharper

我正在使用ReSharper 6.1.1并且在我的项目中启用了solution wide analysis会出现错误:

  

目标类型'CustomControls.XSButton'不能转换为基类型System.String

代码编译并运行正常,因为WPF中内置的TypeConverter会处理这个问题,很快就在ReSharper bug report中进行了描述。

请注意,我的解决方案中的程序集中声明了XSButton - 它不是内置类型的WPF。

这是在<Style>标记处导致此错误的xaml代码。

<CustomControls:XSButton
        Content="i" Grid.Column="2" Command="ApplicationCommands.Help"
        Grid.Row="0" Grid.RowSpan="2" ToolTip="Show Help" Visibility="Collapsed">
    <CustomControls:XSButton.Style>
         <Style TargetType="{x:Type CustomControls:XSButton}">
             <Setter Property="Height" Value="26"/>
             <Setter Property="Width" Value="26"/>
             <Setter Property="CornerRadius" Value="13"/>
         </Style>
   </CustomControls:XSButton.Style>
</CustomControls:XSButton>

有什么方法可以避免这种情况:

  • 该属性需要System.String所以我应该替换类型实例的字符串文字?
  • 有没有办法忽略这个ReSharper错误?
  • 是否可以重新排列 xaml以避免此错误?例如。 se属性是直接而不是使用setter?

修改

问题仅在样式标记相同类型的标记时出现,在本例中为“foo:Bar”。请注意,我打开o以任何可能的方式避免此错误?也许完全重新安排了xaml代码。

enter image description here

2 个答案:

答案 0 :(得分:3)

不幸的是,这是R#6.1.1中的一个错误。问题已经修复,修复程序将在下一个主要的R#版本中提供。很抱歉给您带来不便。

你可以简单地解决这个问题:

<foo:Bar Command="ApplicationCommands.Help">
  <!-- just use the separate resource for base style: -->
  <foo:Bar.Resources>
    <Style x:Key="baseStyle" TargetType="{x:Type foo:Bar}">
    </Style>
  </foo:Bar.Resources>

    <foo:Bar.Style>
        <Style TargetType="{x:Type foo:Bar}"
               BasedOn="{StaticResource baseStyle}">
            <Setter Property="VerticalAlignment" Value="Top"/>
        </Style>
    </foo:Bar.Style>
</foo:Bar>

只需将BasedOn样式声明放入单独的资源中。

答案 1 :(得分:1)

要隐藏ReSharper解决方案范围分析的错误,您可以将错误设置为忽略 通过 ReSharper-&gt; Windows-&gt; Soultion Errors Window 打开 Soultion Errors Window
右键单击恼人的错误,然后选择 忽略错误

以下是另一个错误的屏幕截图:

如果要再次启用错误,可以还原错误:

Stopping ignoring error