Avoiding “Target type 'type name' is not convertible to base type System.String”

纵饮孤独 提交于 2019-12-11 11:58:41

问题


I am using ReSharper 6.1.1 and having the solution wide analysis enabled in my project shows up with an error:

Target type 'CustomControls.XSButton' is not convertible to base type System.String

The code compiles and runs fine since a built in TypeConverter in WPF takes care of this, shortly described in a ReSharper bug report.

Note that the XSButton is declared in an assembly in my solution - It's not a WPF built in type.

This is the xaml code that causes this error at the <Style> tag.

<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>

Is there any way to avoid this:

  • The attribute expects a System.String so what string literal should I replace the type instance with?
  • Is there any way to ignore this ReSharper error?
  • Is thare any way to rearange the xaml to avoid this error? E.g. se attributes directl rather than using setters?

EDIT

The problem only appears when the style tag is inside a tag of the same type, in this case "foo:Bar". Note that I'm open o avoiding this error any way possible? Perhaps completely rearrange the xaml code.


回答1:


Unfortunately, it's a bug in R# 6.1.1. Issue is already fixed and a fix will be available in a next major R# version. Sorry for the inconvenience.

You can really simply workaround it like this:

<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>

Just put the BasedOn style declaration into separate resource.




回答2:


To just hide the error for ReSharper's solution wide analyzation you can set your error to be ignored.
Open Soultion Errors Window by ReSharper->Windows->Soultion Errors Window.
Right-click annoying error and select Ignore Error.

Here is a screenshot with another error as an example:

If you want to enable the error again you can revert the error ignoring:



来源:https://stackoverflow.com/questions/9432174/avoiding-target-type-type-name-is-not-convertible-to-base-type-system-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!