【发布时间】:2017-04-22 00:02:45
【问题描述】:
我可以在 Windows 资源管理器中将图像上传到我的项目文件夹“images”,但不能上传到我的项目“images”文件夹。 (当运行 .exe 并且 Visual Studio 关闭时)
我想将图像复制到我的项目中,并将“构建操作”设置为资源,将“复制到输出”设置为始终复制。
为什么? 当我的程序 (.exe) 运行时(并且 Visual Studio 没有运行),我在主窗口中显示了图像。我有一个“上传图片”按钮。当用户上传图像时,它必须在主窗口中可见。图像显示有绑定。任何人都可以帮助我吗?
主窗口:(工作正常...“Afbeelding”是图像的名称,如“abc.jpg”)
<Button DataContext="{Binding Source={StaticResource mainViewModelLocator}, Path=MainWindowViewModel}" Command="{Binding AlgorithmActiefCommand}" CommandParameter="{Binding Content, ElementName=ollId}" Background="Transparent" BorderBrush="Transparent" Style="{StaticResource ButtonAsImage}">
<Image Source="{Binding Text, ElementName=image}" MaxHeight="65" MaxWidth="65" />
</Button>
ViewModel:当按下 uploadImage 按钮时,“LoadImage”功能启动:
public void LoadImage()
{
//upload image
OpenFileDialog _importAfbeelding = new OpenFileDialog();
_importAfbeelding.Title = "Selecteer een afbeelding";
if (_importAfbeelding.ShowDialog() == true)
{
var _afbeelding = new BitmapImage(new Uri(_importAfbeelding.FileName));
ImportPath = _importAfbeelding.FileName;
string[] _padGesplitst = _importPath.Split('\\');
ImportNaam = _padGesplitst[(_padGesplitst.Length - 1)];
string _destinationFullPath = System.AppDomain.CurrentDomain.BaseDirectory;
DestinationPath = _destinationFullPath.Remove(_destinationFullPath.Length -10) + "images\\OLL\\"+ ImportNaam;
}
try
{
File.Copy(ImportPath, this.DestinationPath, true);
MessageBox.Show("Your images is added with name: " + ImportNaam);
}
catch
{
MessageBox.Show("Unable to open file ");
}
}
现在我的“图像”地图中有 Windows 资源管理器中的图像...但我的项目中没有显示任何内容。并且该名称在数据库中保存为“Afbeelding”,用于 MainWindow 中的绑定。但什么都没有显示。
带有图像文件夹中的图像的主窗口。最后是没有图像。那是我用上传按钮添加的那个。
现在它在 Windows 资源管理器中,但在 MainWindow 中没有显示绑定
同样在重启 Visual Studio 后...它不会出现在项目中:
我在这里做错了什么?谢谢
【问题讨论】:
-
构建操作就是...构建操作。您不能在运行时设置任意文件的构建操作在应用程序被编译之后。
-
您想将图像文件添加到您在 Visual Studio 中的项目中吗? blogs.msdn.microsoft.com/benwilli/2015/05/21/…
-
Paul Abbott,是的,这是有道理的。 Kirodge,是的,这就是我尝试的方法,但当我在 Visual Studio 中时却不是!当 exe 运行时,没有打开 Visual Studio
-
请注意,将 Build Action 设置为 Resource 并将 Copy to Output Directory 设置为 Copy always i> 无论如何都没有意义。前者的目的是使镜像文件成为一个程序集资源(以便通过 Resource File Pack URI 访问它),而后者的目的只是将镜像文件复制到构建目标目录,即可执行文件被写入(从定义的相对文件路径加载它)。这是您在应用程序运行时可以轻松完成的事情。
-
但是,最好从文件系统中某处的专用图像文件夹(即
C:\ProgramData\<YourApp>\Images下)加载图像,并在那里复制新的图像文件。
标签: wpf mvvm image-uploading