【问题标题】:How do you set Image.Source in Silverlight(Code behind)如何在 Silverlight 中设置 Image.Source(后面的代码)
【发布时间】:2009-02-23 12:38:44
【问题描述】:

我通过 Silverlight 中的代码隐藏动态生成图像,显然图像源不接受字符串或 Uri 作为路径。

如何设置来源?

【问题讨论】:

  • 我也花了一点时间才弄明白。 Guantam 的答案看起来像我使用的。
  • 我不得不稍微改变一下,它可以在路径中不包含命名空间的情况下工作

标签: silverlight


【解决方案1】:

你的意思是它不会接受字符串作为源?

你不能这样做吗?

或者您是说您的图像在内存中并且您不知道如何引用它?

this.MyImage.Source = new BitmapImage(new Uri("/MyNameSpace;images/someimage.png", UriKind.Relative));

【讨论】:

  • 不接受我的意思的字符串,例如:MyImage.Source = "/MyNameSpace;images/someimage.png" 就像在 asp.net 中一样
  • 我的项目需要附加“组件”:this.MyImage.Source = new BitmapImage(new Uri("/MyNameSpace;components/images/someimage.png", UriKind.Relative));跨度>
【解决方案2】:
// 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));  

【讨论】:

  • 无需使用 HTTP 协议。只使用本地资源就可以了。
【解决方案3】:

我需要替换以下内容才能解决问题:

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"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多