views:

1400

answers:

1

I'm getting the following error every time my Silverlight application load and cannot figure out how to get around it. This error occurs right when the UserControl is loaded (but before the Source is bound). I've tried handling this error during the ImageFailed event, but it is not fired when this exception is thrown. What can I do?

<Image
    x:Name="EmployeeImage"
    Source="{Binding Path=ImageUrl}"
    Stretch="UniformToFill">
</Image>

Edit (clarification): I have an Employee object which contains an ImageUrl field. My UserControl's DataContext is bound to a ViewModel object which contains a SelectedEmployee property. So my Image element binds to the SelectedEmployee's ImageUrl property. This works great, EXCEPT when the Image is first loaded (and before the DataContext is bound). If I instantiate the SelectedEmployee in the ViewModel's constructor then the error goes away. If I don't, I get the error. This is strange to me, because instantiating a new Employee object results in the ImageUrl being null. Either way, the Image successfully binds when the DataContext is set (and the Employee object is populated with data). I'd just like to figure out why I am getting the error.

Thanks!

A: 

Most likely you are trying to access a network resources that is not in the same domain as the Silverlight page. You are not allowed cross domain calls in Silverlight for security reasons.

Phil Wright
Well, this error occurs BEFORE the DataContext is bound to the Image.Source property, so there is no Url for it to try and access. After binding, the image I want to retrieve is displayed just fine.
Kevin Babcock
On a side note, the images reside on another domain that does not have a policy file. And since the images still display just fine (except for the initial error when the control is first loaded), is it safe to assume cross-domain requests are permitted for an Images?
Kevin Babcock
Perhaps the error is because you don't have the source bound before the image tries to load. Try assigning a default placeholder image (blank.png or something) and see if the exception occurs. The image soruce should still switch ok when you set the DataContext.
sipwiz
I don't know if that is the case or not. Read my edit for further explanation as to what I am trying to do (and what I have done to try to fix the problem).I really appreciate all your comments! Thanks!
Kevin Babcock