Silverlight Base Class Inheritance Error

I have added a Usercontrol into my Silverlight project which by default inherited from UserControl, Later I have modified the cs files so that all the UserControls will inherit from my custom BaseUserControl. I have manually changed the inheritance in the *.g.cs file which are created to load the XAML at runtime. But I am geeting error as the name 'InitializeComponent' does not exist in the current context. and      Partial declarations of 'SLTemplate.MainPage' must not specify different base classes      . How can I resolve this
Jaya1983Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HVHSTechCommented:
This article may be of some assistance

http://forums.silverlight.net/forums/t/12448.aspx
0
Jaya1983Author Commented:
Hi I already checked that link.. I already searched Internet, find no solution and came here for the alternate solution from the experts.
0
Rob SiklosCommented:
Make sure your base class is not abstract.

Make sure your derived class is partial.

In your XAML, make sure the root element is <BaseUserControl>, not <UserControl>

Do not edit the generated .g.cs files - this will only cause you trouble.

If none of the above work please post some code which shows exactly what you are doing.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Jaya1983Author Commented:
I am implementing like this

BaseControl.xaml
----------------------------
namespace SilverlightApplication1
{
    public partial class BaseControl : UserControl
    {
        public BaseControl()
        {
            InitializeComponent();
        }
    }
}

MainPage.xaml
-----------------------
namespace SilverlightApplication1
{
    public partial class MainPage : BaseControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

 
         
    }
}

BaseControl Xaml
------------------------
<UserControl x:Class="SilverlightApplication1.BaseControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">

    </Grid>
</UserControl>


MainPage Xaml
-----------------------
<BaseControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/client/2007"
    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:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot1">

  </Grid>
</BaseControl>


AssemblyInfo.cs
------------------------
added in

[assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "SilverlightApplication1")]


Error in MainPage.Xaml.cs
----------------------------------
Xaml parse exception for InitializeComponent();
Unknown element: BaseControl.

0
Rob SiklosCommented:
I would take out that attribute in AssemblyInfo, and instead do this in MainPage.xaml:

<local:BaseControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/client/2007"
    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:DesignWidth="640" d:DesignHeight="480"
    xmlns:local="clr-namespace:SilverlightApplication1"
>
  <Grid x:Name="LayoutRoot1">

  </Grid>
</local:BaseControl>

Also, BaseControl should just be a regular class which inherits from UserControl - it shouldn't have any XAML, and its constructor should not call InitializeComponent.
0
Jaya1983Author Commented:
Thanks a Lot, It worked if we take Ordinary Class as Base and it inherits UserControl as its Base, but  do we inherit the same things if we inherit the Common Base Class using UserControl as its Base Class as when if tried to Inherit Silverlight User Control as Base Class if taken User Control as Base Class?

I removed entry in Assembly Info and I have done as you said above.
0
Rob SiklosCommented:
I don't really understand your question, but if you inherit from UserControl, you get everything that UserControl has.

Don't forget to mark my comment above as the solution.
0
Jaya1983Author Commented:
I mean what if I have inherited the base silverlight user control to another silverlight user control.

SilverlightControl1.xaml   (Here UserControl is Inheriting for Silverlight UserControl )
--------------------------------
namespace SilverlightApplication1
{
    public partial class SilverlightControl1 : UserControl
    {
        public SilverlightControl1()
        {
            InitializeComponent();
        }
    }
}

MainPage.xaml  ( Now I am trying to Inheriting SilverlightControl1for MainPage UserControl class and I am getting errors as Partial class is already defined and Initilize error etc, if I do so like that)
---------------------
namespace SilverlightApplication1
{
    public partial class MainPage : SilverlightControl1
    {
        public MainPage()
        {
            InitializeComponent();
        }
         
    }
}


If I do as above I will get error, if I do with user control class and by doing as you have said it working fine, but if I do as above I will get error. Hope you understand my question.

No Error for the following:-

BaseControl.cs (UserControl is Inheriting here)
--------------------
namespace SilverlightApplication1
{
    public class BaseControl: UserControl
    {

    }
}

MainPage.xaml  ( BaseControl is Inheriting here)
----------------------
namespace SilverlightApplication1  
{
    public partial class MainPage : BaseControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
 
    }
}

MainPage.xaml
-------------------

<local:BaseControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/client/2007"
    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:DesignWidth="640" d:DesignHeight="480"
    xmlns:local="clr-namespace:SilverlightApplication4"
>
    <Grid x:Name="LayoutRoot1">
 
    </Grid>
</local:BaseControl>

0
Rob SiklosCommented:
No - I don't think that will work.

The rule of thumb is basically this:

If you have a class with some XAML, and that XAML contains "x:Class" in the root element (i.e. it has a code-behind file), you can't inherit from that class.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.