Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug Referenced namespaces missing from generated code behind for inherited user control
10 replies. Latest Post by aknuth on January 13, 2009.
(0)
jayme_ed...
Member
0 points
5 Posts
08-20-2008 3:37 PM |
Hello,
In WPF this works:
In Silverlight, doing this works except that the partial class generated by compiling the "MyApp" project's user control doesn't have a using statement for the C# namespace(s) from step 3.
This is important for allowing people to create libraries of controls that can be inherited from. I've used it in many situations in WPF to great effect!!
Thanks
Yi-Lun L...
All-Star
25052 points
2,747 Posts
08-22-2008 1:14 AM |
Hello, can you share a sample project? I can't reproduce this problem... The generated code in Page.g.cs looks like this:
public partial class Page : SilverlightClassLibrary1.Control1
08-22-2008 9:34 AM |
How do I get the sample project to you? Do you want me to email it or is there a way to attach messages in this forum? I have a sample project created that demonstrates this.
-Jayme
mchlsync
Star
14606 points
2,730 Posts
08-23-2008 7:04 AM |
I'm very interested to take a look this issue. Can you send the sample file to mchlsync AT gmail DOT com?
08-23-2008 1:00 PM |
I recieved your sample project.I think it's related to the nested namespace problem. Please read last two or three posts in this link http://silverlight.net/forums/p/10986/83178.aspx I will find out about that and will let you know.
08-23-2008 4:34 PM |
I got it.
As your sample is not availble here, I will show from scretch.
1. Create Silverlight Application with Website attached.
2. Add a Silverlight Class Library project to Solution and named it SilverlightBaseLib
2. Add a class called BaseControl.cs
3. BaseControl should be inherited from UserControl.
using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace SilverlightBaseLib { public class BaseControl : UserControl{ public new object FindName(string name) { return base.FindName(name); } public new UIElement Content { get { return base.Content; } set { base.Content = value; } } }}
Note:
4. Add that Silverlight class library to Silverlight Application project.
5. Add one Silverlight User Control to Silverlight Application and named it InheritedControl.xaml
6. Copy and paste the following code.
<base:BaseControl x:Class="SilverlightApplication1.InheritedControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:base="clr-namespace:SilverlightBaseLib;assembly=SilverlightBaseLib" Width="400" Height="300"> <Grid x:Name="LayoutRoot" Background="Red"> </Grid></base:BaseControl>
7. Go to InheritedControl.xaml.cs and change UserControl to SilverlightBaseLib.BaseControl.
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace SilverlightApplication1 { public partial class InheritedControl : SilverlightBaseLib.BaseControl { public InheritedControl() { InitializeComponent(); } }}
8. then, use it in Page.xaml as below.
<UserControl xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" x:Class="SilverlightApplication1.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ctl="clr-namespace:SilverlightApplication1" > <Grid> <ctl:InheritedControl /> </Grid></UserControl>
That's all. Hope it helps.
Note: Yu-lin said that xmlns mapping should be used in Assembly. Ref: http://silverlight.net/forums/p/10986/34909.aspx but it doesn't work well with nested control. Hello Yu-lin, Could you please explain me why it's not working with nested control for mapping?
08-23-2008 5:04 PM |
Thanks very much for the post, you get the point across well.
I really want to be able to do what you show in step 6 with Silverlight though. It works in WPF. The nice thing is then you can have several base classes, like one for an edit page, one for a list page as user controls and then in your markup say <EditPage> or <ListPage> as the root element of the XAML documents that inherit from the bases. It works nicely in WPF and gets you closer to a domain specific language by removing UserControl from the language on the page.
Just my two cents, hope you guys implement it :)
mchlSync
08-24-2008 2:16 AM |
The steps that I mentioned above are working fine in Silverlight.
But yes. You can't use like <BaseControl> or <EditPage> directly. You will have to go with <alias:BaseControl> or <alias:EditPage> or etc. I'm not sure why the mapping is not working. If the mapping is working fine, you maybe able to go without having alias.
08-24-2008 11:15 AM |
I don't mind having to put a namespace prefix before my control. What I don't like is having to have my namespace declaration use the "clr-namespace" directive and not an XML namespace declared in my AssemblyInfo.cs. Using the "clr-namespace" prefix within an xml namespace declaration constrains my namespace prefix to only refer to one namespace of C# code per XML namespace.
In WPF you can map multiple C# namespaces in another assembly to a SINGLE XML namespace, declare this XML namespace in your inherited subclass in a silverlight application assembly, and have access to all of your types under a single namespace. This allows third party control vendors or creators of frameworks on WPF to enable consumers of their controls to declare a single XML namespace on top of their XAML and have access to their entire API of user controls - a larger framework of controls and markup WILL be broken up into more than one C# namespace, and with the way things are now consumers of my API now need to declare multiple namespaces, remember the assembly they reside in, and then use multiple namespace prefixes instead of one XML namespace mapped to all of them. I don't see any reason other than time constraints as to why this isn't in Silverlight. These are two Microsoft owned technologies, and the feature I describe is a very powerful one that by not enabling in Silverlight, makes its implementation of XAML look like a shoddy, half-implemented effort. Having some controls missing from Silverlight 2 is one thing - having the architecture laden with caveats that differ from WPF because time to market and a desire for more controls took precedence over the infrastructure is another.
I would like to say that Silverlight is a solid implementation of WPF for the web, but to do that fairly it needs to function consistently enough with respect to things that are not constrained simply by being in a browser (like the way assemblies are dynamically loaded) for me to safely recommend that this technology is "ready for primetime" to others for projects. My ability to cut the time needed to deliver applications by packaging reusable controls and have folks familiar with WPF come on board quickly with Silverlight makes or breaks the feasibility of the technology. Having to explain that Silverlight does things differently in areas where there are no technical constraints as to why costs time in communicating architecture, and leveraging existing personnel.
09-10-2008 3:14 PM |
To add a simple code example, if I have the following classes:
namespace A { public class MyControl1 : UserControl { ... } }namespace B { public class MyControl2 : UserControl { ... } }namespace C { public class MyControl3 : UserControl { ... } }
In WPF I can add this to my AssemblyInfo.cs:
[assembly: XmlnsDefinition("http://MyControls", "A")][assembly: XmlnsDefinition("http://MyControls", "B")][assembly: XmlnsDefinition("http://MyControls", "C")]
And then use them on a page with only one namespace declaration (assuming my page has a reference to the assembly into which the previous controls are compiled):
<MyControl1 x:Class="MyOtherProject.MyControl1Subclass" xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://MyControls"> <MyControl2> <MyControl3 /> </MyControl2></MyControl1>
In Silverlight my AssemblyInfo.cs entries look like this to do the same thing:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "A")][assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "B")][assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "C")]
And to use them I need to declare three CLR namespaces, AND they can't be the default (no prefix) namespace or the compiler gives warnings:
<a:MyControl1 x:Class="MyOtherProject.MyControl1Subclass" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:a="clr-namespace:A;assembly=MyControlsAssembly" xmlns:b="clr-namespace:B;assembly=MyControlsAssembly" xmlns:c="clr-namespace:C;assembly=MyControlsAssembly"> <b:MyControl2> <c:MyControl3 /> </b:MyControl2> </a:MyControl1>
I would really like to see this work consistently with WPF
aknuth
8 points
11 Posts
01-13-2009 10:33 AM |
Hi there,
I got the same problem. Any progress on this topic yet?
Regards,André