简体   繁体   中英

c# UserControl base class inheritance

I have a base class call PopupWindow which inherits from UserControl. PopupWindow has no associated xaml page, its just a regular class which inherits from UserControl

I then have another UserControl which inherits from PopupWindow. Now this is a proper user control in that is has an associated xaml page.

I have changed the root xaml tag to be PopupWindow like this

<local:PopupWindow
  .....
</local:PopupWindow>

where local is the namespace in this PopupWindow exists.

But I keep getting an error that PopupWindow does not exist in the given namespace.

What am I doing wrong?

Thanks

I've managed to get this working as follows:

Parent control:

public class PopupWindow : UserControl
{
}

Inheriting control:

Code behind:

public partial class PopupWindowChild
{
    public PopupWindowChild()
    {
        InitializeComponent();
    }
}

XAML:

<Controls:PopupWindow x:Class="MyNamespace.Controls.PopupWindowChild"
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:Controls="clr-namespace:MyNamespace.Controls">
    <TextBlock Text="Blah" />
</Controls:PopupWindow>

Main view:

<Window x:Class="MyNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Controls="clr-namespace:MyNamespace.Controls">
    <Controls:PopupWindow />
</Window>

Your XAML won't recognise your control until the code is built. Once built and run, I'm seeing "Blah" in my main view as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM