c# WPF URI 语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35214453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# WPF URI syntax
提问by Nick S
I am just learning c# and have been struggling to work with URIs in WPF. I've googled around a fair bit but not having much luck.
我只是在学习 c# 并且一直在努力在 WPF 中使用 URI。我在谷歌上搜索了很多,但运气不佳。
Essentially I'm trying to have a BitmapImage object stored as a property in a Car object. I then want to display the BitmapImage in an Image control on a WPF form.
本质上,我试图将 BitmapImage 对象作为属性存储在 Car 对象中。然后我想在 WPF 表单上的图像控件中显示 BitmapImage。
The app is a simple app (it's for a Uni assignment), so no database, etc.
该应用程序是一个简单的应用程序(用于 Uni 作业),因此没有数据库等。
I have two methods of doing this. The first is that I'm preloading Car data from a text file, including the filename of the JPG I want to load. I have included the JPG in a directory called Files which is off the main directory where my source code and class files are. I have set the JPG file to 'Content' and 'Always copy'. When I run a Debug, it copies the Files directory and the JPG to the debug\bin directory.
我有两种方法可以做到这一点。第一个是我从文本文件中预加载 Car 数据,包括我要加载的 JPG 的文件名。我已将 JPG 包含在名为 Files 的目录中,该目录位于我的源代码和类文件所在的主目录之外。我已将 JPG 文件设置为“内容”和“始终复制”。当我运行 Debug 时,它会将 Files 目录和 JPG 复制到 debug\bin 目录。
My code creates a BitmapImage by referring to the JPG using a URI as follows;
我的代码通过使用 URI 引用 JPG 来创建一个 BitmapImage,如下所示;
BitmapImage myImage = new BitmapImage (new Uri("Files/" + Car.Imagefilename, UriKind.Relative);
Car.Image = myImage;
ImageControl.Source = myImage;
If I step through this code in the debugger, it sometimes works and displays the image, but most of the time it doesn't.
如果我在调试器中逐步执行此代码,它有时会工作并显示图像,但大多数时候它不会。
My second method is when a user creates a new Car. This method always works. In this one, I use a file dialog box (dlg) to select the image and use an absolute path.
我的第二种方法是当用户创建一辆新车时。这种方法总是有效。在这个中,我使用文件对话框 (dlg) 来选择图像并使用绝对路径。
BitmapImage myImage = new BitmapImage (new Uri(dlg.Filename, UriKind.Absolute);
Car.Image = myImage;
ImageControl.Source = myImage;
So....I can't work out why the first method doesn't work. I think it's got something to do with the relative reference, but I can't work out how to syntax that properly to work. I've tried using "pack:,,,", I've tried adding "component", I've tried an '@' before the "pack". I can't seem to find something that explains this simply.
所以......我无法弄清楚为什么第一种方法不起作用。我认为这与相对引用有关,但我无法弄清楚如何正确使用语法来工作。我试过使用“pack:,,,”,我试过添加“component”,我试过在“pack”之前加一个“@”。我似乎找不到可以简单解释这一点的东西。
Apologies if this is straight forward but it's doing my head in! Appreciate any pointers.
抱歉,如果这是直截了当的,但它让我头疼!感谢任何指针。
采纳答案by Chintana Meegamarachchi
First try to load the image file using its absolute path. For example if the images are stored in c:\projects\yourproject\files, then try using something like
首先尝试使用其绝对路径加载图像文件。例如,如果图像存储在 c:\projects\yourproject\files,然后尝试使用类似
BitmapImage myImage = new BitmapImage (new Uri("c:/projects/yourproject/files/carname.jpg", UriKind.Absolute);
If it works, what you are facing is an path calculation issue.
如果它有效,您面临的是路径计算问题。
At this point you may either calculate the Absolute with reference to your executable using AppDomain.CurrentDomain.BaseDirectoryat runtime or use App.Configto store the path and reference it from there.
此时,您可以AppDomain.CurrentDomain.BaseDirectory在运行时参考可执行文件计算绝对值,也可以App.Config用于存储路径并从那里引用它。
Cheers
干杯
回答by Clemens
If the image files are located in a "Files" folder of your Visual Studio project, you should set their Build Actionto Resource(and Copy to Output Directoryto Do not copy), and load them by a Resource File Pack URI:
如果图像文件位于您的Visual Studio项目的“文件”文件夹,你应该设置自己Build Action来Resource(和Copy to Output Directory对Do not copy),并通过加载它们的资源文件包URI:
var image = new BitmapImage(new Uri("pack://application:,,,/Files/" + Car.Imagefilename));
Car.Image = image;
ImageControl.Source = image;
There is no need to copy the files anywhere. Images are loaded directly from the assembly.
无需将文件复制到任何地方。图像直接从程序集加载。

