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

Moo*_*ght 50 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>
Run Code Online (Sandbox Code Playgroud)

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

  1. 应用程序使用时不显示WPF控件图像

  2. wpf中的图像问题(图像没有显示)

  3. 没有显示在WPF的按钮的背景图象

如何让按钮图像在运行时出现?

Phi*_*hil 68

将构建操作更改为"资源".你的包网址也错了.使用:

Source="pack://application:,,,/Resource/UserCost2013Open16.png"
Run Code Online (Sandbox Code Playgroud)

或者干脆

Source="/Resource/UserCost2013Open16.png"
Run Code Online (Sandbox Code Playgroud)

  • 我刚刚使用设计器属性面板重新添加了图像,我首先做了它,它提供了一个与我所拥有的不同的URI,就像你显示的那个 - 这不起作用.我重新添加了图像作为资源 - 再次没有运气.我_then_重新添加了图像_again_ - 这给了我一个像底部一个节目的URI ... Taadaa!它起作用了,为什么会发生这种情况我不知道,但将来很高兴知道.谢谢你的时间. (4认同)

Bas*_*sti 28

有2个解决方案:

1:更改图像的设置:

Build Action = Content
Copy to output directory = Copy if newer
Source="pack://siteoforigin:,,,/Resources/UserCost2013Open16.png"
Run Code Online (Sandbox Code Playgroud)


2:在源路径中使用应用程序而不是siteoforigin时,您必须采用以下方法:

a)图像将位于名为"Resources"的SubFolder中,而.exe文件将很小

Source="pack://application:,,,/Resources/UserCost2013Open16.png"
Build Action = Content
Copy to output directory = Copy if newer
Run Code Online (Sandbox Code Playgroud)

b)图像将包含在.exe中,并且不存在带有imagefile的子文件夹

Source="pack://application:,,,/Resources/UserCost2013Open16.png"
Build Action = Resource
Copy to output directory = Copy if newer
Run Code Online (Sandbox Code Playgroud)


Dar*_*nas 7

在我的例子中,我将图像放在一个名为的单独项目中Common,图像Resources位于此项目中命名的文件夹下.在我的另一个项目中,我添加了一个引用Common并设置了这样的图像源:

<Image Source="/Common;component/Resources/anImage.png"/>
Run Code Online (Sandbox Code Playgroud)

该图像具有Build Action设置为ResourceCopy to Output DirectoryDo not copy.但是,由于一些奇怪的原因,直到我删除了我的解决方案中的每个汇编文件并创建了一个Clean SolutionBuild Solution.不知道为什么,但是一旦我重建了一切,它都开始在运行时工作.我仍然无法弄清楚为什么它在Design Time工作.

  • 将 `component` 字符串添加到源路径中对于让我的图像在运行时显示至关重要。 (2认同)

Fel*_* D. 7

假设你有

  • 设置你Build ActionResource

  • 正确使用设置该路径 URI-PACK-FORMAT

在我的情况下它仍然没有显示.

Clean & Rebuild 不只是Build为我修好了!


Ωme*_*Man 7

在 Visual Studio 中执行以下步骤

  1. 将图像添加到您想要命名的文件夹中,在本例中我创建了一个Assets命名文件夹。
  2. 然后将图像的属性设置ResourceBuild Action.
  3. 然后选择文件夹中的图像并将其拖放到其中<MenuItem.Icon>,它应该正确路径。

将图像拖放到项目中,设置为资源,然后拖放到页面上

  • 请注意,您可能需要重建项目,编译器有时不会将新资源识别为需要重建。


Dee*_*pak 5

转到资源文件夹中的图像,右键单击图像,转到属性,单击构建操作属性,然后将其从无更改为资源。那行得通。