• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 865
  • Last Modified:

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
0
Jaya1983
Asked:
Jaya1983
  • 4
  • 4
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Get your problem seen by more experts

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

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now