Tuesday, August 6, 2013

Make the Silverlight XAML files play nice with your Visual Studio web app

Today I was building a web application with an embedded gallery when I hit a snag that wasted an hour of my time. The fix turned out to be simple so I decided to write it up to save you the same grief.

The issue only affects developers adding a gallery to an existing website using Visual Studio. To make deployment easier, I had grabbed the compiled version of GSP and added all the files in the gs directory to a gs directory in my web application. When I compiled the project, I was met with this error:

“Project file must include the .NET Framework assembly 'PresentationCore, PresentationFramework' in the reference list.”

I dutifully added project references to PresentationCore.dll and PresentationFramework.dll and tried again. This time I got 84 errors, all of which had one of these two messages:

“The tag 'Canvas' does not exist in XML namespace 'http://schemas.microsoft.com/client/2007'.”

“Each dictionary entry must have an associated key.”

The errors were generated by the XAML files included in Gallery Server Pro. These are the Silverlight media players for audio and video files and are used in some browsers for MP3, M4A, WMA and WMV files. Only AudioGray.xaml and Professional.xaml are actually used in a default installation, but you can modify the media templates to use any of the other players if you prefer their style.

xaml1

Anyway, so I tried to make the error go away by changing the namespace to a value that included Canvas. That is, I opened one of the files (AudioGray.xaml) and changed the first line from:

<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="404" Height="40">

to this:

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="404" Height="40">

That got rid of the first error. To get rid of the second error I had to add x:Key to each element that required it. For example, I changed this:

<Storyboard x:Name="PlayPauseButton_MouseEnter">

to this:

<Storyboard x:Key=”A1” x:Name="PlayPauseButton_MouseEnter">

After tediously applying unique keys to dozens of elements, those errors disappeared. Unfortunately, two new ones appeared:

“VideoBrush is not supported in a Windows Presentation Foundation (WPF) project.”

“The tag 'VideoBrush' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.”

After a bit of research I discovered that VideoBrush exists only in the Silverlight version of System.Windows.dll, which-as far as I can tell-cannot be referenced from a WebForms ASP.NET app. I seemed dead in the water.

The 10-second solution

Then it dawned on me to look at the build action property for the XAML files. They were set to Page:

xaml2

Aha! All I want Visual Studio to do with these files is copy them to the web server during deployment. They are not Silverlight pages and should not be compiled or parsed. To tell VS to treat them as content, change the build action to Content. Once I did that, the project compiled.

That’s the long story. The short story is this: When you include the XAML files from Gallery Server Pro in your web application, change the build action to Content. That’s it. You don’t need the references to PresentationCore.dll or PresentationFramework.dll or any of the other monkeying around I did.

Really, I should have thought of that first, but you know how it goes sometimes. Maybe this post will save you some debugging time.

No comments: