Updated 6/22/2007 - You can now download a sample project here.
I ran into a problem the other day when I tried to create a base class that would derive from System.Windows.Controls.UserControl in the WPF stack... I was trying to implement common functionality that would be utilized across a number of of controls.
You would think that this would be pretty straight forward... my base class looked like the following:
1: public abstract class UIPart : System.Windows.Controls.UserControl , IPart
2: {
3: //code removed
4: }
1: public partial class MyUserControl : UIPart
3: public MyUserControl()
4: {
5: InitializeComponent();
6: }
7:
8: }
1: <UserControl x:Class="Shell.Runtime.Parts.MyUserControl"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
4: <Grid>
5:
6: </Grid>
7: </UserControl>
When I compiled this I kept getting the following compile error:
Partial declarations of MyUserControl must not specify different base classes.
If I browsed to the MyUserControl.g.cs it showed that MyUserControl still dervied from System.Windows.Controls.UserControl. If you are wondering what the MyUserControl.g.cs is... it is a file generated by the compiler, you can get to it by right clicking on InitializeComponent() in the MyUserControl constructor and selecting "Go to Declaration".
1: /// <summary>
2: /// MyUserControl
3: /// </summary>
4: public partial class MyUserControl : System.Windows.Controls.UserControl, System.Windows.Markup.IComponentConnector
5: {
6: //code removed
7: }
Its important to understand that when compiling the MyUserControl.xaml is used to create the MyUserControl.g.cs file, which will contain the field references for any named controls and also use the root tag as the base class. This of course of the problem... my root tag was UserControl (as shown in the above xaml), which maps to System.Windows.Controls.UserControl.
1: <UIPart x:Class="Shell.Runtime.Parts.MyUserControl"
7: </UIPart>
I modified the xaml so that the root tag was UIPart, which would then map to my UIPart base class I had defined above and derived from System.Windows.Controls.UserControl. If you compile at this point, you would get the following error:
The tag 'UIPart' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
This results from the fact that the UIPart base class cannot be resolved, you need to add a reference to enable the compiler to resolve it. That is accomplished by doing the following:
1: <runtime:UIPart x:Class="Shell.Runtime.Parts.MyUserControl"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:runtime="clr-namespace:Shell.Runtime"
5: >
6: <Grid>
8: </Grid>
9: </runtime:UIPart>
Notice that we added the runtime namespace prefix to the UIPart tag and we also import another namespace (Shell.Runtime) using a special syntax... clr-namespace followed by the actual namespace that UIPart is found in. If UIPart was found in another assembly you could use the following syntax to import the namespace from another assembly:
4: xmlns:runtime="clr-namespace:Shell.Runtime;assembly=Shell"
The above namespace import indicates that the Shell.Runtime namespace is found in the Shell.dll assembly.
Posted in |Comments [1]
Sysknowlogy