我通过Silverlight中的代码隐藏动态生成图像,显然图像源不接受字符串或Uri作为路径。
如何设置来源?
答案 0 :(得分:54)
你是怎么说它不接受字符串作为源?
你不能这样做吗?
或者你是说你的形象在记忆中并且你不知道如何引用它?
this.MyImage.Source = new BitmapImage(new Uri("/MyNameSpace;images/someimage.png", UriKind.Relative));
答案 1 :(得分:6)
// create a new image
Image image = new Image();
// better to keep this in a global config singleton
string hostName = Application.Current.Host.Source.Host;
if (Application.Current.Host.Source.Port != 80)
hostName += ":" + Application.Current.Host.Source.Port;
// set the image source
image.Source = new BitmapImage(new Uri("http://" + hostName + "/image111.jpg", UriKind.Absolute));
答案 2 :(得分:1)
我需要更换以下内容才能使解决方案正常工作:
this.MyImage.Source = new BitmapImage(new Uri("/MyNameSpace;components/images/someimage.png", UriKind.Relative));
MyNameSpace对我不起作用,但是ExecutingAssemblyName确实如此:
Dim tmp As String() = Assembly.GetExecutingAssembly.FullName.Split(","c)
Dim path As String = "/" & tmp(0) & ";component/images/"
MyImage.Source = new BitmapImage(new Uri(path & "someImage.png"))