Link to home
Start Free TrialLog in
Avatar of Jaya1983
Jaya1983

asked on

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
Avatar of HVHSTech
HVHSTech
Flag of United States of America image

This article may be of some assistance

http://forums.silverlight.net/forums/t/12448.aspx
Avatar of Jaya1983
Jaya1983

ASKER

Hi I already checked that link.. I already searched Internet, find no solution and came here for the alternate solution from the experts.
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.
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.

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.
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.
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.
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>

ASKER CERTIFIED SOLUTION
Avatar of Rob Siklos
Rob Siklos
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial