在Silverlight中引用全局样式的图像

时间:2011-02-14 10:31:26

标签: silverlight image resources coding-style themes

我有点问题,我只是想知道解决问题的最佳方法是:

我在app.xaml中有一个全局样式,它指定了许多控件的样式。它还指定了一些其他控件中使用的图像。

app.xaml文件中感兴趣的区域是:

<!-- Button Icons -->
<BitmapImage x:Key="BackIcon" UriSource="../Images/BackIcon.png" />
<BitmapImage x:Key="PrintIcon" UriSource="../Images/PrintIcon.png" />
<BitmapImage x:Key="ClearIcon" UriSource="../Images/ClearIcon.png" />

<!-- Default Button Style -->
<Style TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>

                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ColorAnimation Duration="00:00:00.00"  To="#999999" Storyboard.TargetName="BackgroundColor" Storyboard.TargetProperty="Color"  />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ColorAnimation Duration="00:00:00.00"  To="#bbbbbb" Storyboard.TargetName="BackgroundColor" Storyboard.TargetProperty="Color"  />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseOver" />
                            <VisualState x:Name="Normal"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>

                    <Border Name="BackgroundBorder" BorderThickness="0" CornerRadius="8"> 

                        <Border.Background>
                            <SolidColorBrush x:Name="BackgroundColor" Color="#dddddd" />
                        </Border.Background>

                        <ContentPresenter
                            x:Name="content"
                            Content="{TemplateBinding Content}"
                            VerticalAlignment="Center" HorizontalAlignment="Center" />

                    </Border>
                </Grid>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

项目结构如下:

root\
|-- App.xaml
|-- Mainpage.xaml(.cs)
|-- Images\
|   |-- BackIcon.png
|   |-- ClearIcon.png
|   |-- PrintIcon.png
|-- Controls\
    |-- ControlOne.xaml(.cs)
    |-- ControlTwo.xaml(.cs)
    |-- ...

所以我在ControlOne中有一个按钮,其内容包含一个Image,其来源是{StaticResource ClearIcon}。这工作正常,但如果我在MainPage.xaml上放置一个具有相同图像和静态资源的按钮,这将失败,因为图像的相对位置不正确。它现在应该是Images / ClearIcon.png,而不是../ Images / ClearIcon.png。

那么在样式/主题中指定图像资源的最佳方法是什么?无论资源在何处使用,它们都是正确的?

此外,我不确定我是否有点奇怪的方式,但我不想将图像直接放在按钮模板中,因为我必须创建三种不同的按钮样式对于每个按钮,当真正的风格保持不变时,它只是变化的图像!

提前多多感谢!

安迪

1 个答案:

答案 0 :(得分:2)

确定,

似乎我忽略了URI在silverlight中的工作原理。阅读这篇文章:http://weblogs.asp.net/jgalloway/archive/2008/09/11/silverlight-and-relative-uri-s-for-image-and-video-sources.aspx确实有助于澄清它。

所以我似乎正在将我的图像编译到程序集中并引用它们,但是当你这样做时,相对路径总是来自引用图像的xaml文件的位置(因此我的问题)。

我需要做的是将图像设置为'内容'而不是'复制到输出'。这意味着图像不再编译到程序集中,而是复制到相同文件夹结构下的xap中。现在我可以使用前面的正斜杠(即'/Images/BackIcon.png')引用我的图像,并且无论xaml文件的位置如何,相对路径将始终来自xap的根。

无论如何似乎工作得很好! :)