Adding F # Image

I have a stack panel to which I want to add some icons dynamically.

If I add a TextBlock to the stack panel, it works fine:

// assuming stackPanel is my stack panel
let text = new TextBlock()
text.Text <- "Test"
stackPanel.Children.add(text)

However, my goal is to add an image, but it looks like it cannot resolve the image

let getImageSource(imagePath) = 
    let uri = new Uri(imagePath, UriKind.Relative)
    new BitmapImage(uri); 

let icon = new Image()
icon.Source <- getImageSource("images/fileIcon/icon.gif")

stackPanel.Children.Add(icon) // this doesnt work

now when i do:

let icon = new Image()
icon.Source <- getImageSource("images/fileIcon/icon.gif")

stackPanel.Children.Add(icon) 

let text = new TextBlock()
text.Text <- "Test"
stackPanel.Children.add(text)

I see there is an empty space between the texts, as if there was an empty image. Therefore, I assume that something is wrong with the way I resolve the path to the image, but I'm not sure why.

Thoughts?

Thank!

+3
source share
2 answers

In case your gif Build Action is a resource, then the correct way to handle it is /SilverlightApplication1;component/path/to/file.gif. Here SilverlightApplication1 is the name of your silverlight application

, Build Action , /path/to/file.gif, BitmapImage.

Silverlight 2: URI .

, BitmapImage.ImageFailed , .

: AFAIK Silverlight GIF. PNG.

+5

Uri, WPF.

let uri = Uri("pack://application:,,,/asm_name;component/images/fileIcon/icon.gif")

asm_name .

Silverlight, uri . , icon.gif .

let uri = Uri("../images/fileIcon/icon.gif", UriKind.Relative)

, .

0

All Articles