简体   繁体   English

带有图像的WPF按钮未出现

[英]WPF button with image doesn't appear

I'm using the following code I found on a question a few hours ago to make a button have an image: 我使用几个小时前在一个问题上发现的以下代码,使按钮具有图像:

            <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>

The problem is that for some reason it looks fine on Visual Studio, but when I run the program those buttons won't appear. 问题是由于某种原因在Visual Studio上看起来不错,但是当我运行程序时,这些按钮将不会出现。 I can't find the problem and I'm a little bit stuck. 我找不到问题,我有点卡住了。 The image play.bmp is added to resources obviously, but still I don't know what the problem is, thanks! 显然将图像play.bmp添加到资源中,但是仍然不知道是什么问题,谢谢!

"siteoforigin" in the URI indicates the file must be in the given path relative to the directory of the executing assembly. URI中的“ siteoforigin”表示文件必须位于相对于执行程序集的目录的给定路径中。 Your executable file is most likely in a bin/Debug folder. 您的可执行文件很可能位于bin / Debug文件夹中。 It's looking in the subdirectory of the executable, which probably doesn't exist ("bin/Debug/Resources/play.bmp"). 它在可执行文件的子目录中查找,该子目录可能不存在(“ bin / Debug / Resources / play.bmp”)。

If you want to link to the file that way then you have to tell Visual Studio to copy it to the output folder (from the Properties pane). 如果要以这种方式链接到文件,则必须告诉Visual Studio将其复制到输出文件夹(从“属性”窗格)。 Or copy it yourself. 或自己复制。

Or better yet you should link to it as a resource and it will get embedded in the application. 或者更好的是,您应该将其链接为资源,并将其嵌入到应用程序中。 Set the file's build type to Resource in the properties pane, and link to it using the relative path in your project folder. 在属性窗格中将文件的构建类型设置为Resource,然后使用项目文件夹中的相对路径链接到该文件。 In this case, literally write "Resources/play.bmp" in the XAML. 在这种情况下,请在XAML中按原样编写“ Resources / play.bmp”。

Try simplifying your code, perhaps just pull the image out and see if that displays. 尝试简化您的代码,也许只是拉出图像,看看是否显示。

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

should be valid as well 也应该是有效的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM