Silverlight中的图像问题
问候
问题
当我尝试将图像添加到我的 silverlight 项目时,出现错误:“AG_E_NETWORK_ERROR”
。
重新创建
我创建了一个没有 Web 部件的新 silverlight 4 项目。所以我只得到 MyApplication。
在 MyApplication 中,我添加了一个名为“Images”的地图,并在该地图中添加了一些图像。鸡蛋.png,鸡肉.png。
现在我希望将这些图像加载到我的 silverlight 项目中。在 mainpage.xaml 中我放置: 但这不会加载。
所以我从后面的代码中尝试它:
var imag = new BitmapImage(new Uri(@"\Images\Egg.png", UriKind.Relative));
imag.ImageFailed += (s, ea) =>
{
throw new Exception(ea.ErrorException.Message);
};
然而,无论我尝试什么,这都会引发上述错误。
图像被设置为资源并且“请勿复制”。
- 我做错了什么?
- 我怎样才能使图像发挥作用?
[编辑]发表
评论后,我注意到图像正在原始项目中加载。
但我在将图像发送到的另一个项目中有一个用户控件。从那里加载仍然失败。
我该如何解决这个问题? 我错过了什么吗?
Greetings
Problem
When i'm trying to add images to my silverlight project I get the error: "AG_E_NETWORK_ERROR"
.
Recreate
I create a new silverlight 4 project without a web part. So I only get MyApplication.
In MyApplication I add a map called "Images" and in that map I add a few images. Egg.png, Chicken.png.
Now I wish to load these images to my silverlight project. In the mainpage.xaml I place:<Image Source="/Images/Egg.png" />
but this does't load.
So I try it from code behind:
var imag = new BitmapImage(new Uri(@"\Images\Egg.png", UriKind.Relative));
imag.ImageFailed += (s, ea) =>
{
throw new Exception(ea.ErrorException.Message);
};
This however keeps throwing the above error nomather what I try.
The images are set as a resource and "Do Not Copy".
- What am I doing wrong??
- How can I make the images work?
[Edit]
After comments I noticed the image is loading in the original project.
But I have a user control in a different project to which I send the image. From there the loading still fails.
How can I fix this?
Am I missing anything?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解析 XAML 时:
URI 将转换为适合定位图像的 URI。如果您在代码隐藏中设置源,则必须自己进行此转换。 URI 的格式如下:
请参阅此相关问题:
How do你在Silverlight中设置Image.Source(后面的代码)
When your XAML is parsed:
The URI is converted into a suitable URI for locating your image. If you set the source in code-behind you have to do this conversion yourself. The URI is of this format:
See this related question:
How do you set Image.Source in Silverlight(Code behind)
这是一个可怕的通用错误消息,但我相信问题可能只是找不到文件。当您将项目添加到 Silverlight 项目时,它们不会被复制到托管站点的
ClientBin
文件夹中(即使在构建时,无论复制到输出目录
设置如何,因为这仅决定了将其添加到当前项目的bin
文件夹中的内容) - 您需要手动复制这些内容(或最终使用构建后步骤),即:ClientBin
文件夹,位于.xap
文件旁边。更新:
那么,解决方案中没有网站部分,资源是否位于 Silverlight 项目本身的
bin
文件夹中?如果没有,请尝试以下操作:BuildAction
设置为Content
复制到输出目录
设置为始终复制
,或者如果较新则复制
This is a terrible generic error message but I believe the problem may simply be that the files can't be found. When you add items to your Silverlight project they don't get copied out to the
ClientBin
folder of the hosting site (even on build, regardless ofCopy to output directory
settings because this only dictates what makes it to the current project'sbin
folder) - you'll need to copy these manually (or eventually use a post-build step), that is:ClientBin
folder, alongside the.xap
file.Update:
So, there's no website part of the solution, are the resources making it to the
bin
folder of the Silverlight project itself? If not, try this:BuildAction
toContent
Copy to output directory
toCopy Always
, orCopy if newer