Boppity Bop
Boppity Bop

Reputation: 10463

UWP inherit from a class inherited from UserControl

I am porting a Silverlight application to UWP Windows 10 app.

Large part of it has controls, inherited from a class, which inherits from UserControl.

base:
public abstract class PartBase : UserControl, IPart, IDisposable

concrete:
public sealed partial class MyPart : PartBase 

its XAML:
<local:PartBase 

I get compilation error : The name "PartBase" does not exist in the namespace ..

Is inheritance permitted in UWP ?

Upvotes: 1

Views: 1360

Answers (1)

Bart
Bart

Reputation: 10015

Your code should work. I've created your abstract base class and a new control based on that class.

<local:PartBase
    x:Class="UWPTest.Controls.MyUserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWPTest.Controls"
    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>
        <Button>Test</Button>
    </Grid>
</local:PartBase>

Double check that the xmlns:local="using:UWPTest.Controls" is correct with the namespace PartBase is declared in. Then rebuild your solution and the error should go away (you will see the error if you don't rebuild).

On a page (e.g. MainPage) I can simply use the control:

<Page
    x:Class="UWPTest.MainPage"
    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"
    xmlns:controls="using:UWPTest.Controls"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <controls:MyUserControl1 />
    </Grid>
</Page>

Note the xmlns:controls pointing to the correct namespace. The designer will also give an error until you rebuild the app.

Everything builds here and the application runs, so if you still have the error after double checking all namepace declarations you'll have to put a repro online so we can check what else goes wrong.

Upvotes: 4

Related Questions