2

我正在使用几个小时前在一个问题上找到的以下代码来使按钮具有图像:

            <Button Name="bPlay" Height="70" Width="70" Margin="359,480,349,11">
            <Button.Template>
                <ControlTemplate>
                    <Border HorizontalAlignment="Center" VerticalAlignment="Center">
                        <Image Source="pack://siteoforigin:,,,/Resources/play.bmp" Width="70" Height="70" />
                    </Border>
                </ControlTemplate>
            </Button.Template>
        </Button>

问题是由于某种原因它在 Visual Studio 上看起来不错,但是当我运行程序时,这些按钮不会出现。我找不到问题,我有点卡住了。图片play.bmp显然添加到资源中,但我仍然不知道问题是什么,谢谢!

4

2 回答 2

6

URI 中的“siteoforigin”表示文件必须位于相对于执行程序集目录的给定路径中。您的可执行文件很可能位于 bin/Debug 文件夹中。它正在查看可能不存在的可执行文件的子目录(“bin/Debug/Resources/play.bmp”)。

如果要以这种方式链接到文件,则必须告诉 Visual Studio 将其复制到输出文件夹(从“属性”窗格)。或者自己复制。

或者更好的是,您应该将其作为资源链接,它将嵌入到应用程序中。在属性窗格中将文件的构建类型设置为 Resource,并使用项目文件夹中的相对路径链接到它。在这种情况下,在 XAML 中直接写“Resources/play.bmp”。

于 2012-06-06T03:04:06.457 回答
0

尝试简化您的代码,也许只是将图像拉出并查看是否显示。

<Button Name="bPlay" Height="70" Width="70" Margin="359,480,349,11">
    <Image Source="pack://siteoforigin:,,,/Resources/play.bmp" Width="70" Height="70" />
</Button>

也应该有效

于 2012-06-06T03:02:02.827 回答