Skip to main content

Microsoft Silverlight

Unanswered Question Referenced namespaces missing from generated code behind for inherited user controlRSS Feed

(0)

jayme_edwards
jayme_ed...

Member

Member

0 points

5 Posts

Referenced namespaces missing from generated code behind for inherited user control

Hello,

In WPF this works:

  1. Create a class library "MyControls".
  2. Add a XAML user control called "MyControl"
  3. Add attribute to AssemblyInfo.cs of "MyControls" library to register the namespace containing "MyControl" with an XML namespace.
  4. Create a XAML application project "MyApp".
  5. Add a reference to the "MyControls" project to the "MyApp" project. 
  6. Add a XAML user control to the "MyApp" project.
  7. Modify the user control in the following ways:
    1. In the markup, declare the XML namespace from step 3.
    2. Change the root element of the user control to "MyControl" prefixed with the namespace prefix you used above.
    3. In the code behind, change the control to inherit from MyControl instead of UserControl.

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

Jayme Edwards
Senior Lead Consultant
Catapult Systems
http://www.catapultsystems.com

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Re: Referenced namespaces missing from generated code behind for inherited user control

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

 

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

jayme_edwards
jayme_ed...

Member

Member

0 points

5 Posts

Re: Referenced namespaces missing from generated code behind for inherited user control

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

Jayme Edwards
Senior Lead Consultant
Catapult Systems
http://www.catapultsystems.com

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: Referenced namespaces missing from generated code behind for inherited user control

I'm very interested to take a look this issue.  Can you send the sample file to mchlsync AT gmail DOT com?

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: Referenced namespaces missing from generated code behind for inherited user control

 Hello,

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.

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: Referenced namespaces missing from generated code behind for inherited user control

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:

  • Content property : The reason why I'm adding "Content" property in this class is for showing UI on the screen.
  • FindName : this.FindName() is used in the generated cs file. So, I need to implement this method to avoid this error.

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?

 

 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


jayme_edwards
jayme_ed...

Member

Member

0 points

5 Posts

Re: Referenced namespaces missing from generated code behind for inherited user control

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 :)

 -Jayme

Jayme Edwards
Senior Lead Consultant
Catapult Systems
http://www.catapultsystems.com

mchlSync
mchlSync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: Referenced namespaces missing from generated code behind for inherited user control

 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.

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


jayme_edwards
jayme_ed...

Member

Member

0 points

5 Posts

Re: Referenced namespaces missing from generated code behind for inherited user control

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.

Jayme Edwards
Senior Lead Consultant
Catapult Systems
http://www.catapultsystems.com

jayme_edwards
jayme_ed...

Member

Member

0 points

5 Posts

Re: Referenced namespaces missing from generated code behind for inherited user control

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

Jayme Edwards
Senior Lead Consultant
Catapult Systems
http://www.catapultsystems.com

aknuth
aknuth

Member

Member

8 points

11 Posts

Re: Re: Referenced namespaces missing from generated code behind for inherited user control

Hi there,

I got the same problem. Any progress on this topic yet?

Regards,
André

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities