# Saturday, December 01, 2007

Silverlight Bug: MediaElements will restart if their containment lineage changes

I have not yet found a workaround for this problem.

The setup is simple: Create two Canvas elements. In one canvas, place a MediaElement. Point the media element to a valid video source. It doesn't matter if it is set to AutoPlay or not (but if using manual play, then you will need to initiate playback in some manner after loading).

Now once that video playback has begun (it doesn't matter how), if you remove the MediaElement from the original canvas using Children.Remove() and then add it to the other canvas using Children.Add(), then the playback position will reset and will start playing from the beginning.

I would possibly expect this behavior if the MediaElement has the AutoPlay property enabled, but this also happens when AutoPlay is false. Basically, as long as the media is active, it is restarted if you relocate the element to another parent control. I am guessing this is a bug in the MediaElement's OnLoad implementation that is not respecting the AutoPlay setting if the media is currently being played, and is internally calling Stop() and Start().

Saturday, December 01, 2007 12:24:02 AM (Eastern Standard Time, UTC-05:00) #  Disclaimer | Comments [0] | 

Silverlight Bug: Using a packaged image source for multiple Image elements

I ran into this nasty little bug today... it is not easy to reproduce, but when it happens it is very annoying to debug.

First, this bug only occurs when you are using a ZIP file to package your external resources and using a Silverlight Downloader object to bring the package to the browser, and subsequently using Image.SetSource() to provide the image files to the visual elements.

Second, it's not really a bug in functionality exactly, but more of a nuisance to the viewer. You see, the images get displayed correctly, but Silverlight throws extra error messages.

The problem occurs when you reuse a single image resource from a downloaded zip archive for multiple Image elements. And it only happens when the two images are assigned to the same resource consecutively. Example:

function downloadComplete(sender, eventArgs)
{
  // this one is OK
  sender.GetHost().FindName(“Image1”).SetSource(sender, “picture.png”);
  // this one fails
  sender.GetHost().FindName(“Image2”).SetSource(sender, “picture.png”);
}

This is using Silverlight 1.0, but I assume the same issue also happens in 1.1.

The problem appears to be related to referencing the same resource twice consecutively. If you reference another resource from the Downloader between the two calls to SetSource(), then the problem goes away. Also, if you simply don't reuse the same resource like this, then it can be avoided.

UPDATE January 2008: My previous "fix" of referencing another resource in between uses of the same resource does not seem to always work. In addition, some Silverlight installations (not all!) are also raising spurious ImageError 4001 messages. The only complete fix I have found is to implement a custom error handler, and ignore those ImageErrors:

function handleError(sender, errorArgs)
{
  if (errorArgs.errorType == "ImageError" && errorArgs.errorCode == 3002)
    {
      // This error is raised sporadically by SetSource
      return;
    }
    if (errorArgs.errorType == "ImageError" && errorArgs.errorCode == 4001)
    {
      // This error is raised sporadically by SetSource
      return;
    }
  Silverlight.default_error_handler(sender, errorArgs);
}

Saturday, December 01, 2007 12:15:55 AM (Eastern Standard Time, UTC-05:00) #  Disclaimer | Comments [0] | 
View Keith Rome's profile on LinkedIn

On this page....

Archives

Navigation

Categories

About

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Sign In

Certification Logo Certification Logo Certification Logo Certification Logo Certification Logo

Powered by: newtelligence dasBlog 2.3.9074.18820