XAML:属性"资源"设置不止一次

Ada*_*m S 33 wpf xaml

我收到以下错误:

"资源"属性设置不止一次.

这是我的XAML:

<UserControl.Resources>
    <!--Resource dictionaries for framework stuff-->
    <ResourceDictionary>
        <Style x:Key="MultiLineTextBox" TargetType="TextBox">
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="TextWrapping" Value="WrapWithOverflow"/>
        </Style>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/View;component/Common/ResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

    <!--Convertors needed for proper display-->
    <c:CollapsedIfNegative x:Key="CollapseIfNegative"/>
    <c:VisibleIfNegative x:Key="MakeVisibleIfNegative"/>
    <c:ErrorCodeToString x:Key="ConvertErrorCodeToString"/>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

Dan*_*zey 74

.ResourcesXaml中的属性很聪明:它是类型ResourceDictionary但是,如果你没有明确地<ResourceDictionary>在其内容周围添加标记,编译器会神奇地为你假设一个.这就是为什么你通常只需将你的画笔直接放入标记.

但是,你已经开始使用自己的ResourceDictionary- 我怀疑它已经阻止了自动行为 - 因此编译器现在认为你正在尝试设置多个值.如果你像这样重写,你应该得到你想要的结果:

<UserControl.Resources>
    <!--Resource dictionaries for framework stuff-->
    <ResourceDictionary>
        <!--Convertors needed for proper display-->
        <!-- move this INSIDE the ResourceDictionary tag -->
        <c:CollapsedIfNegative x:Key="CollapseIfNegative"/>
        <c:VisibleIfNegative x:Key="MakeVisibleIfNegative"/>
        <c:ErrorCodeToString x:Key="ConvertErrorCodeToString"/>


        <Style x:Key="MultiLineTextBox" TargetType="TextBox">
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="TextWrapping" Value="WrapWithOverflow"/>
        </Style>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/View;component/Common/ResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

  • 这句话值得用黄金来衡量:"如果你没有明确地在其内容中放置一个<ResourceDictionary>标签,那么编译器会神奇地为你设一个" - 非常感谢你. (7认同)