Issue with x:Name with XAML used between WPF and Silverlight
I'm having a strange namespace resolution issue in my user control XAML files.
If I create a new WPF librar开发者_运维百科y, it adds the following:
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</UserControl>
This evaluates to a class name of MyLibraryNameSpace.UserControl1.
However, if I add a Silverlight class library and add a UserControl to that application, I get the following XAML
<UserControl x:Class="MyLibraryNameSpace.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</UserControl>
This evaluates to the same thing, but the XAML is different, so I can't have Silverlight and WPF projects referring to the same XAML since the x:Class statement would be correct for one and incorrect for the other. If I specify x:Class="MyLibraryNameSpace.UserControl1" in WPF it will be interpreted as MyLibraryNameSpace.MyLibraryNameSpace.UserControl1 under my current project.
How do I get my WPF library to use fully qualified class names or otherwise resolve this scenario so I can use a single namespace for shared WPF / Silverlight code?
The more I think about this, the more I think that this is just a design decision they made differently between the WPF and Silverlight teams and didn't adjust things for fear of breaking backward compatibility.
What I've wound up doing is create two subfolders: one for WPF and one for Silverlight. The .xaml.cs files were kept in the controls folder, but the XAML was copied to the Silverlight and WPF folders and slightly modified. That way the code behind at least is shared. Going forward, I'll use controls instead of user controls and convert existing user controls to controls when I can (that should let me share the XAML).
Moral of the story: Use Controls for WPF / Silverlight multi-targeting, not User Controls.
It's perfectly possible to share a UserControl between WPF and Silverlight (whether it's a good idea I'll leave to others, for me it depends how much you can share). Sharing XAML is certainly harder than sharing code as you can't rely on being able to #if your way out of incompatibilities. I'd expect your Silverlight XAML above to work in WPF as-is.
My usual approach would be
- Separate Silverlight and WPF solutions, with a corresponding project in each (with the same name/namespace)
- Create a UserControl in the Silverlight project
- Add the UserControl to the WPF project as a link
I don't really see what x:Name has to do with your issue, WPF and Silverlight both have this.
精彩评论