简体   繁体   English

尝试在背后的代码中修改Image.Source时发生未知错误

[英]Unknown error when trying to modify Image.Source in code behind

I'm actually developing a Windows 8 C#/XAML app for the Windows Store. 我实际上是在为Windows应用商店开发Windows 8 C#/ XAML应用。 In this app, I want to show images depending on what the user selected just before. 在这个应用程序中,我想根据用户之前选择的内容显示图像。 So, in the code behind, I try to modify the myImage.Source parameter. 因此,在后面的代码中,我尝试修改myImage.Source参数。 I do it like this : 我这样做是这样的:

myImage.Source = new BitmapImage(new Uri("/folder/imagename.png", UriKind.Relative));

This used to work in WPF applications for Windows 7, but there, in Windows 8, I got an error: 这曾经可以在Windows 7的WPF应用程序中工作,但是在那里,在Windows 8中,我得到了一个错误:

System.Uri cannot be converted to Windows.Foundation.Uri. 无法将System.Uri转换为Windows.Foundation.Uri。 Consult http://go.microsoft.com/fwlink/?LinkID=215849 to obtain details. 请访问http://go.microsoft.com/fwlink/?LinkID=215849以获取详细信息。

Of course, the link is useless, and I found nothing by searching the Internet. 当然,该链接是无用的,并且通过搜索Internet我一无所获。 I'm kinda lost with this. 我有点迷失了。 So i'm here to ask for help. 所以我在这里寻求帮助。

Thanks in advance! 提前致谢!

The Image.Source property is of type ImageSource . Image.Source属性的类型为ImageSource Therefore, try the following example: 因此,请尝试以下示例:

myImage.Source = "/YourApplicationName;component/folder/imagename.png";

Or this one: 或者这个:

BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(
    "pack://application:,,,/YourApplicationName;component/folder/imagename.png");
image.EndInit();
myImage.Source = image;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM