1

My problem seemed simple: I have a viewbox on my mainpage and depending on the theme chosen (theme1 or theme2) I want to show the appropriate image (WhiteImage or BlackImage, both defined as SVG Canvases in myImages.xaml under resources).

This is what I thought I could do:

<Viewbox x:Name="myCustomImage"
VerticalAlignment="Top"
Height="24"
Margin="0,0,10,0"
Child="{StaticResource myImage}" />

Then I tried defining the canvas myImage in theme1.xaml (and theme2.xaml similarly)as follows:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:myApp">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="../Resources/myImages.xaml" />
    </ResourceDictionary.MergedDictionaries>

    ...
    <Color x:Key="myForegroundcolor">#FF63398F</Color>  
    ...
    <Canvas x:Key="myImage">WhiteImage</Canvas>

</ResourceDictionary>

This throws an error message-A value of type 'String' cannot be added to a collection or dictionary of type 'UI ElementCollection'.

Is this the right approach in the first place? If yes, how do I fix this error?

Edit: I followed Dean's advice (thanks) below. This is what I did:

Changed resourcetheme1.xaml as follows:

<Canvas x:Key="myImage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Width="200"
        Height="100">
    <ContentControl Content="{StaticResource WhiteImage}" />

    <Canvas>
        <Canvas>
            <Path Fill="#FFFFFFFF"
                    Data=... />         
            <Path Fill="#FFFFFFFF"
                    Data=... />
        </Canvas>
    </Canvas>
</Canvas>

Changed mainpage.xaml as follows:

<Viewbox x:Name="myViewbox"
...
Height="32"
Child="{StaticResource WhiteImage}" />

Added a dependency property to the .cs: ...

public Canvas myImage
{
    get
    {
        return (Canvas)GetValue(myImageProperty);
    }
    set
    {
        SetValue(myImageProperty, value);
    }
}

Now I get a different error on this line in mainpage.xaml Child="{StaticResource WhiteImage}" /> - Provide value on System.Windows.StaticResourceExtension threw an exception - cannot find resource named WhiteImage. I get the same error when I change the xaml to myImage btw. Thanks for any help. On a related note, I find it hard to believe it is this complicated-all I am trying to do is this: if theme=black viewbox.child=whiteImage else viewbox.child=blackimage.

4

1 に答える 1

1

Canvas の子として文字列を使用しています。as child を次のように宣言する必要があります

<Canvas x:Key="myImage">
   <ContentControl Content="{StaticResource WhiteImage}" />
</Canvas>
于 2013-04-22T11:07:26.023 に答える