1

x:Shared="false" で共有リソースを使用しようとすると、Visual Studio 2010 の xaml デザイナーに次の例外が表示されます。

System.InvalidOperationException 指定された要素は、既に別の要素の論理的な子です。最初に切断します。

それを回避することは可能ですか (たとえば、共有オブジェクトの同じ作成を行う添付プロパティを実装することによって)?

サンプル xaml:

<Window.Resources>
    <Image x:Key="SharedImage" x:Shared="false" Source="/Images/image.png" />
    <Style x:Key="ImageButton" TargetType="{x:Type Button}">
        <Setter Property="Content" Value="{StaticResource SharedImage}" />
    </Style>
</Window.Resources>
<StackPanel>
    <Button Style="{StaticResource ImageButton}" />
    <Button Style="{StaticResource ImageButton}" />
</StackPanel>

このバグは Visual Studio 2012 で修正されていますか?

4

1 に答える 1

0

残念ながら、これはバグとは見なされません。マイクロソフトが考えを変えない限り、共有したいものはすべてControlTemplate.

あなたの例は次のようになります(テストされていません):

<Window.Resources>
    <ControlTemplate x:Key="ImageButton">
        <Image Source="/Images/image.png" />
    </ControlTemplate>
    <Style TargetType="Button">
        <Setter Property="Content">
            <Setter.Value>
                <Control Template="{StaticResource ImageButton}"/>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<StackPanel>
    <Button />
    <Button />
</StackPanel>
于 2014-01-08T14:35:36.837 に答える