All,
I have a simple class.
public class Container : UserControl
{
public bool IsClickable { get; set; }
}
I have a class that extends this class.
public class ScrollingContainer : Container
{
public void Draw()
{
}
public void Update()
{
}
}
I have a custom class, that then extends ScrollingContainer.
public partial class MaskContainer : ScrollingContainer
{
public MaskContainer()
{
InitializeComponent();
}
}
XAML
<local:ScrollingContainer x:Class="Test.Types.MaskContainer"
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:local="clr-namespace:GameObjects;assembly=GameObjects"
mc:Ignorable="d"
>
</local:ScrollingContainer>
In my mainpage.xaml, I have the following.
<types:MaskContainer x:Name="maskContainer" Canvas.ZIndex="1" Width="Auto" Height="Auto">
<Canvas x:Name="maskCanvas">
<Button x:Name="button1" Content="test button"/>
</Canvas>
</types:MaskContainer>
Why, at runtime, are both maskCanvas and button1 null? maskContainer is not null.
The inheritance should be straightforward here. Container inherits usercontrol. Scrollable container inherits container. Mask Container inherits scrollable container. Why am I losing the functionality of the original base class at this level? Is it incorrect to add the element (button1) to the maskcontainer inside of the main.xaml?
My end goal is to create a container that is reusable, but inherits all properties/methods that I’ve specified throughout the chain.
Any help is appreciated.
The problem is,
MaskContainerhas an attached XAML file, which makes the WPF codegen generate a class that implementsSystem.Windows.Markup.IComponentConnector. And when a control implements that interface, it receives a whole new quality: it becomes a naming scope root. Which means that inside such a control, there is a whole separate naming space. Control names outside do not conflict with inside ones.In this particular case, that means that both
maskCanvasandbutton1are not visible from withinmainpageby name, but are visible from withinMaskContainerinstead.In fact, I’m very surprised that you’ve even managed to get this compiled somehow. From my experience, if you try this trick with Visual Studio, it would complain about
maskCanvasbeing “under scope of element MaskContainer“. Do you use command-line compilation by any chance?Ok, so enough blah-blah-blah already.
What’s the solution? Simply remove XAML file from
MaskContainer. That will get rid of autogeneratedIComponentConnectorimplementation, and everything will work as you expect it to.