1

I have question about using XAML across the WPF and Silverlight platforms.

Background:

I have a silverlight app that needs to pass the Xaml to WPF and do some calculation to update the XAML. When I run the changes in WPF, I parse the XAML and convert it to the Canvas object which does the job perfectly fine. The problem is WPF strip out the silverlight namespace and even delete the name of some elements.

The code that I use to convert XAML into Canvas

Canvas canvas = XamlReader.Parse(xaml) as Canvas

Here is the original Xaml from Silverlight:

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Holder" Width="58" Height="23" >

.......

After I convert it to a Canvas object, the Xaml of the Canvas become this:

<Canvas Name="Holder" Width="58" Height="23" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Canvas.Clip><RectangleGeometry RadiusX="0" RadiusY="0" Rect="0,0,58,23"/>
</Canvas.Clip><Rectangle Name="HolderBackground" Canvas.Top="0.4" Canvas.Left="0.4" Width="57.2" Height="22.2" RadiusX="0" RadiusY="0" Fill="#FFFFFF" />
<Canvas Name="Image1" Canvas.Top="0.8" Canvas.Left="0.8" Width="24.37999153137207" Height="21.4"> .......

You should notice the xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" is missing. Also, all the x:Name now becomes Name. If you look closely, you will notice this element's name has been removed

Does everyone have any idea or simple solution that I can convert Silverlight XAML -> WPF XAML ->(back to) Silverlight XAML?

Cheers

4
  • Could you please add some more detail about what your problem is? Using "Name" instead of "x:Name" on a Canvas is perfectly valid in both Silverlight and WPF, so why is this a problem for you? Dec 10, 2009 at 18:39
  • I honestly didn't know if using "Name" instead of "x:Name" is completely valid in silverlight. The biggest problem I am facing is that the we have some code written in Silverlight1 which I am not sure if it will still works if we use "Name" instead of "x:Name". Also, that still doesn't explain about some of the names disappeared.
    – junk
    Dec 10, 2009 at 21:56
  • One thing worth mentioning is when I get the Xaml out from the Canvas, I used XamlWriter.Save(). I am not sure if that's a problem
    – junk
    Dec 10, 2009 at 22:10
  • When used on UIElement, "x:Name" and "Name" are equivalent. If you want to know the different, essentially x:Name="foo" says to the Xaml parser "find some way to name this particular element as "foo"), but Name="foo" means "set the property "Name" to the value "foo". As it happens, for UIElements, both of these instructions are equivalent due to UIElement defining the RuntimeNamePropertyAttribute attribute pointing to the Name property. Relevent documentation here: tinyurl.com/y8bwc8q and tinyurl.com/ycb2y6y Dec 10, 2009 at 23:50

1 Answer 1

0

It looks like I have to self-answer this, but it's not a defn answer

someone wrote a custom XamlWriter class that resolves part of the problem, but the missing name of RectangleGeometry problem would still exist http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.