C# WPF-使用.exe文件中的新图像

C# WPF-使用.exe文件中的新图像,c#,wpf,image,C#,Wpf,Image,在我的WPF应用程序中,我在VisualStudio中向我的项目添加了一些图像,并更改了它们的构建操作。他们工作。 问题是当我想向images文件夹添加新图像时。之后,我启动我的.exe文件和应用程序无法看到这些新添加的图像。 如何解决这个问题 private void ShowImage(Button button) { string buttonName = button.Name; string path = $@"Image

在我的WPF应用程序中,我在VisualStudio中向我的项目添加了一些图像,并更改了它们的构建操作。他们工作。 问题是当我想向images文件夹添加新图像时。之后,我启动我的.exe文件和应用程序无法看到这些新添加的图像。 如何解决这个问题

private void ShowImage(Button button)
        {
            string buttonName = button.Name;
            string path = $@"Images/{buttonName}.png";

            Image image = new Image();
            image.Source = new BitmapImage(new Uri(path, UriKind.Relative));
            button.Content = image;
        }

如果我理解你的问题,我认为你想要的答案是一个组件参考,看起来是这样的:

private void ShowImage(Button button)
    {
        string buttonName = button.Name;
        string path = "/MyProject;component/Images/{buttonName}.png";

        Image image = new Image();
        image.Source = new BitmapImage(new Uri(path, UriKind.Relative));
        button.Content = image;
    }

您正在设置的生成操作是什么以及如何加载文件您可以共享一些代码片段。生成操作:内容复制到输出目录:始终复制我添加的将图像设置为按钮内容的方法。当我在编译之前将图像添加到项目中时,一切正常。但我想“手动”将图像添加到文件夹中,而无需再次编译。是否将新图像文件复制到实际输出目录,例如
bin/Debug/images/
bin/Release/images/
?如果图像文件名与某个按钮名匹配,则应该可以。是。它可以看到新文件(我得到了所有文件的名称,并且包含在内),但无法显示它。请注意,在类似的代码后面,需要使用适当的前缀限定它,或者
pack://application:,,,
pack://siteoforigin:,,,
。它也不适用于
UriKind.Relative