【问题标题】:WPF button with image doesn't appear没有出现带有图像的 WPF 按钮
【发布时间】:2012-06-10 01:52:48
【问题描述】:

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

            <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 明明添加到资源中了,还是不知道是什么问题,谢谢!

【问题讨论】:

    标签: c# wpf image button


    【解决方案1】:
    URI 中的

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

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

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

    【讨论】:

    【解决方案2】:

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

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

    应该也是有效的

    【讨论】:

    • 由于某种原因,我尝试这样做时没有出现图像(我可以看到按钮,但上面没有图像)
    猜你喜欢
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多