locked
Creating a Base Class from UserControl for a Windows Store App RRS feed

  • Question

  • I want to create a base class that can be used the same way that UserControl is used. I have tried creating a class that inherits from UserControl, but I continue to receive an error that says that the base classes cannot be different, even though I changed UserControl to my base class in both the codebehind and XAML. What do I need to do to create a custom base control class? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, December 17, 2013 9:49 PM

All replies

  • This shouldn't be a problem. It would help us diagnose what you are doing if you would include more information to show what your Xaml and class structure look like.

    As a guess, make sure your inherited class references the user control in its base. For example, if I have a user control MyUserControl1 and a second control which inherits it, the Xaml for each would be:

    <UserControl
        x:Class="App25.MyUserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App25"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
        
        <Grid>
           <TextBlock Text="MyUserControl1" />
        </Grid>
    </UserControl>
    

    <local:MyUserControl1
        x:Class="App25.MyUserControl2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App25"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
        
        <Grid>
            <TextBlock Text="MyUserControl2" />
        </Grid>
    </local:MyUserControl1>
    

    Wednesday, December 18, 2013 1:14 AM
    Moderator
  • My base class that inherits from UserControl is named KnotControl and has the following codebehind:
    Public MustInherit Class KnotControl : Inherits UserControl
    	Public mscount As Double = 0	'Milliseconds since timer started
    	Protected timer As DispatcherTimer
    
    	Public Sub New()
    		Me.timer = New DispatcherTimer()
    		Me.timer.Interval = TimeSpan.FromMilliseconds(1)
    		AddHandler Me.timer.Tick, AddressOf Me.PathAnimation_Tick
    	End Sub
    
    	Public MustOverride Sub PathAnimation_Tick(sender As Object, e As Object)
    
    	Public Shared Function GetAngle(currms As Double, startms As Double, endms As Double, startangle As Double, endangle As Double) As Double
    		'Returns the current angle for a segment that starts at startms time, ends at endms time, starts at startangle, and ends at endangle
    		Return (((currms - startms) * (endangle - startangle)) / (endms - startms) + startangle).ToRadians()
    	End Function
    	Public Shared Function GetPoint(currms As Double, startms As Double, endms As Double, startpoint As Double, endpoint As Double) As Double
    		'Returns the x or y value for a segment that starts at startms time, ends at endms time, starts at x or y startpoint, and ends at x or y endpoint
    		Return ((currms - startms) * (endpoint - startpoint)) / (endms - startms) + startpoint
    	End Function
    End Class
    An example of a control that I made from KnotControl (in this case Bowline) has the following codebehind and XAML:
    Public NotInheritable Class Bowline : Inherits KnotControl
    	Public Overrides Sub PathAnimation_Tick(sender As Object, e As Object)
    	End Sub
    End Class

    <KnotControl
    	x:Class="KnotWhatYouWantTest.Bowline"
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	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">
    	<Canvas Style="{StaticResource KnotCanvasStyle}">
    		<Path Name="Path7a" Style="{StaticResource UnderPathStyle}" Data="M 200 35 L 180 35"/>
    		<Path Name="Path7b" Style="{StaticResource OverPathStyle}" StrokeDashArray="1 1" Data="M 200 35 L 180 35" Visibility="{Binding Visibility,ElementName=Path7a}" StrokeDashOffset="1.7"/>
    		<Path Name="Path7c" Style="{StaticResource DashedPathStyle}" StrokeDashArray="0 2" Data="M 200 35 L 180 35" Visibility="{Binding Visibility,ElementName=Path7a}" StrokeDashOffset="1.7"/>
    		<Path Name="Path8a" Style="{StaticResource UnderPathStyle}" Data="M 180 35 A 15 15 0 0 0 165 50"/>
    		<Path Name="Path8b" Style="{StaticResource OverPathStyle}" StrokeDashArray="1 1" Data="M 180 35 A 15 15 0 0 0 165 50" Visibility="{Binding Visibility,ElementName=Path8a}"/>
    		<Path Name="Path8c" Style="{StaticResource DashedPathStyle}" StrokeDashArray="0 2" Data="M 180 35 A 15 15 0 0 0 165 50" Visibility="{Binding Visibility,ElementName=Path8a}"/>
    		<Path Name="Path3a" Style="{StaticResource UnderPathStyle}" Data="M 169 104 A 100 100 0 1 0 240 75"/>
    		<Path Name="Path3b" Style="{StaticResource OverPathStyle}" StrokeDashArray="1 1" Data="M 169 104 A 100 100 0 1 0 240 75"/>
    		<Path Name="Path3c" Style="{StaticResource DashedPathStyle}" StrokeDashArray="0 2" Data="M 169 104 A 100 100 0 1 0 240 75"/>
    		<Path Name="Path4a" Style="{StaticResource UnderPathStyle}" Data="M 240 75 A 25 25 0 0 1 222 68"/>
    		<Path Name="Path4b" Style="{StaticResource OverPathStyle}" StrokeDashArray="1 1" Data="M 240 75 A 25 25 0 0 1 222 68" Visibility="{Binding Visibility,ElementName=Path4a}"/>
    		<Path Name="Path4c" Style="{StaticResource DashedPathStyle}" StrokeDashArray="0 2" Data="M 240 75 A 25 25 0 0 1 222 68" Visibility="{Binding Visibility,ElementName=Path4a}"/>
    		<Path Name="Path12a" Style="{StaticResource UnderPathStyle}" Data="M 205 80 L 205 115"/>
    		<Path Name="Path12b" Style="{StaticResource OverPathStyle}" StrokeDashArray="1 1" Data="M 205 80 L 205 115" Visibility="{Binding Visibility,ElementName=Path12a}" StrokeDashOffset="1.75"/>
    		<Path Name="Path12c" Style="{StaticResource DashedPathStyle}" StrokeDashArray="0 2" Data="M 205 80 L 205 115" Visibility="{Binding Visibility,ElementName=Path12a}" StrokeDashOffset="1.75"/>
    		<Path Style="{StaticResource UnderPathStyle}" Data="M 180 0 L 180 70 A 30 25 0 0 0 240 70 A 15 15 0 0 0 214 59"/>
    		<Path Style="{StaticResource OverPathStyle}" StrokeDashArray="1 1" Data="M 180 0 L 180 70 A 30 25 0 0 0 240 70 A 15 15 0 0 0 214 59"/>
    		<Path Style="{StaticResource DashedPathStyle}" StrokeDashArray="0 2" Data="M 180 0 L 180 70 A 30 25 0 0 0 240 70 A 15 15 0 0 0 214 59"/>
    		<Path Style="{StaticResource UnderPathStyle}" Data="M 214 59 L 169 104" StrokeEndLineCap="Flat"/>
    		<Path Style="{StaticResource OverPathStyle}" StrokeDashArray="1 1" Data="M 214 59 L 169 104"/>
    		<Path Style="{StaticResource DashedPathStyle}" StrokeDashArray="0 2" Data="M 214 59 L 169 104"/>
    		<Path Name="Path5a" Style="{StaticResource UnderPathStyle}" Data="M 222 68 A 25 25 0 0 1 215 50"/>
    		<Path Name="Path5b" Style="{StaticResource OverPathStyle}" StrokeDashArray="1 1" Data="M 222 68 A 25 25 0 0 1 215 50" Visibility="{Binding Visibility,ElementName=Path5a}" StrokeDashOffset="0.5"/>
    		<Path Name="Path5c" Style="{StaticResource DashedPathStyle}" StrokeDashArray="0 2" Data="M 222 68 A 25 25 0 0 1 215 50" Visibility="{Binding Visibility,ElementName=Path5a}" StrokeDashOffset="0.5"/>
    		<Path Name="Path6a" Style="{StaticResource UnderPathStyle}" Data="M 215 50 A 15 15 0 0 0 200 35" StrokeEndLineCap="Flat"/>
    		<Path Name="Path6b" Style="{StaticResource OverPathStyle}" StrokeDashArray="1 1" Data="M 215 50 A 15 15 0 0 0 200 35" Visibility="{Binding Visibility,ElementName=Path6a}" StrokeDashOffset="1" StrokeEndLineCap="Flat"/>
    		<Path Name="Path6c" Style="{StaticResource DashedPathStyle}" StrokeDashArray="0 2" Data="M 215 50 A 15 15 0 0 0 200 35" Visibility="{Binding Visibility,ElementName=Path6a}" StrokeDashOffset="1" StrokeEndLineCap="Flat"/>
    		<Path Name="Path9a" Style="{StaticResource UnderPathStyle}" Data="M 165 50 A 15 15 0 0 0 180 65"/>
    		<Path Name="Path9b" Style="{StaticResource OverPathStyle}" StrokeDashArray="1 1" Data="M 165 50 A 15 15 0 0 0 180 65" Visibility="{Binding Visibility,ElementName=Path9a}" StrokeDashOffset="1"/>
    		<Path Name="Path9c" Style="{StaticResource DashedPathStyle}" StrokeDashArray="0 2" Data="M 165 50 A 15 15 0 0 0 180 65" Visibility="{Binding Visibility,ElementName=Path9a}" StrokeDashOffset="1"/>
    		<Path Name="Path10a" Style="{StaticResource UnderPathStyle}" Data="M 180 65 L 190 65"/>
    		<Path Name="Path10b" Style="{StaticResource OverPathStyle}" StrokeDashArray="1 1" Data="M 180 65 L 190 65" Visibility="{Binding Visibility,ElementName=Path10a}" StrokeDashOffset="1.75"/>
    		<Path Name="Path10c" Style="{StaticResource DashedPathStyle}" StrokeDashArray="0 2" Data="M 180 65 L 190 65" Visibility="{Binding Visibility,ElementName=Path10a}" StrokeDashOffset="1.75"/>
    		<Path Name="Path11a" Style="{StaticResource UnderPathStyle}" Data="M 190 65 A 15 15 0 0 1 205 80" StrokeEndLineCap="Flat"/>
    		<Path Name="Path11b" Style="{StaticResource OverPathStyle}" StrokeDashArray="1 1" Data="M 190 65 A 15 15 0 0 1 205 80" StrokeEndLineCap="Flat" Visibility="{Binding Visibility,ElementName=Path11a}" StrokeDashOffset="1"/>
    		<Path Name="Path11c" Style="{StaticResource DashedPathStyle}" StrokeDashArray="0 2" Data="M 190 65 A 15 15 0 0 1 205 80" StrokeEndLineCap="Flat" Visibility="{Binding Visibility,ElementName=Path11a}" StrokeDashOffset="1"/>
    	</Canvas>
    </KnotControl>

    I do not have a *.xaml file for the KnotControl because I did not know what was supposed to go in it, since it inherits from UserControl. Should any of these files look different? I can't quite figure out what I'm doing wrong here. Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, December 18, 2013 2:01 AM