c# - WPF 按钮中的图像在运行时不可见

标签 c# wpf image button

所有,我有一个小应用程序的以下开始,它检查 .resx 文件的嵌入式括号的一致性(这样就不会发生不匹配的“...{0}”字符串的运行时错误)。我有以下用于 MainWindow.xaml 的 XAML,我的特殊问题与要在按钮上显示的图像有关

<Window x:Class="ResxChecker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="174.383" Width="495.869">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="350*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="30*"/>
        </Grid.RowDefinitions>
        <Label Content="Select .resx file:" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Height="24" Width="Auto" Grid.ColumnSpan="1"/>
        <TextBox Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Margin="10,0,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
        <Button Grid.Column="2" HorizontalAlignment="Right" Margin="5,0,10,0" Grid.Row="1">
            <Image VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="16 " Width="16" Source="pack://siteoforigin:,,,/Resources/UserCost2013Open16.png"/>
        </Button>
    </Grid>
</Window>

图像具有“构建操作 = 资源”、“复制到输出目录 = 不复制”- 图像在设计器中显示但在运行时不显示。我看到了以下问题并阅读了相关答案,但都没有解决问题:

  1. WPF control images not displayed when consumed by an application

  2. image problem in wpf (image does not show up)

  3. Background Image of Button not showing in WPF

如何让按钮图像在运行时显示?

最佳答案

将构建操作更改为“资源”。 你的包网址也是错误的。要么使用:

Source="pack://application:,,,/Resource/UserCost2013Open16.png"

或者只是

Source="/Resource/UserCost2013Open16.png"

关于c# - WPF 按钮中的图像在运行时不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15636047/

相关文章:

image - React Native Image 元素因 URI 位置而崩溃

c# - 根据文本长度调整表单大小

c# - 将 C# 6.0 默认属性值转换为 C# 5.0

c# - 绑定(bind)关键字指的是什么?

php - 在 PHP 中合并多个相邻的图像

html - IE 中的错误图像定位

c# - CoreCLR 中的 Type.InvokeMember(..)

c# - 如何更快地将元素/对象添加到列表中?

c# - 如何为 FlowDocumentScrollViewer 的自动滚动创建附加行为

c# - 自动生成 WPF/Silverlight View (脚手架)