Search code examples
c#wpf

How to avoid CS0108 InitializeComponent hides inherited member warning in inherited WPF control


I have an inherited ContentControl in a NET 7 WPF app (Visual Studio 17.7.2). The compiler creates this warning:

Warning CS0108 'ChartWithSkippedDataPoints.InitializeComponent()' hides inherited member 'BaseChartControl.InitializeComponent()'. Use the new keyword if hiding was intended. Controlboard.WpfClient C:\source\Controlboard.WpfClient\obj\Debug\net7.0-windows10.0.19041\Presentation\Controls\Charts\ChartWithSkippedDataPoints.g.cs 53 Active

The code in the auto generated file causes this warning. I don't know how to avoid that.

Base content control BaseChartControl.cs:

public partial class BaseChartControl : ContentControl
{
    public BaseChartControl()
    {
        InitializeComponent();
        // do something 
    }
    
    protected void InitializeComponent()
    {
        // create some content
    }
    
}

Inherited control C# ChartWithSkippedDataPointsControl.cs:

public partial class ChartWithSkippedDataPointsControl : BaseChartControl
{
    public ChartWithSkippedDataPointsControl()
    {
        InitializeComponent();  // could also be removed
    }
    
    // do something 

}

Inherited control XAML ChartWithSkippedDataPointsControl.xaml:

<local:BaseChartControl x:Class="Controlboard.WpfClient.Presentation.Controls.Charts.ChartWithSkippedDataPointsControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Controlboard.WpfClient.Presentation.Controls.Charts"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
            
    </Grid>
</local:BaseChartControl>

Auto generated file ChartWithSkippedDataPointsControl.g.cs

#pragma checksum "..\..\..\..\..\..\Presentation\Controls\Charts\ChartWithSkippedDataPointsControl.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "67E69F63AFFD58706220572EAD3E41CFEC0A5F12"
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
    
namespace Controlboard.WpfClient.Presentation.Controls.Charts {    
    
    /// <summary>
    /// ChartWithSkippedDataPointsControl
    /// </summary>
    public partial class ChartWithSkippedDataPointsControl : Controlboard.WpfClient.Presentation.Controls.Charts.BaseChartControl, System.Windows.Markup.IComponentConnector {
        
        private bool _contentLoaded;
        
        /// <summary>
        /// InitializeComponent
        /// </summary>
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "7.0.10.0")]
        public void InitializeComponent() {
            if (_contentLoaded) {
                return;
            }
            _contentLoaded = true;
            System.Uri resourceLocater = new System.Uri("/Controlboard;component/presentation/controls/charts/chartwithskippeddatapoin" +
                    "tscontrol.xaml", System.UriKind.Relative);
            
            #line 1 "..\..\..\..\..\..\Presentation\Controls\Charts\ChartWithSkippedDataPointsControl.xaml"
            System.Windows.Application.LoadComponent(this, resourceLocater);
            
            #line default
            #line hidden
        }
        
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "7.0.10.0")]
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
            this._contentLoaded = true;
        }
    }
}

I tried to remove the InitializeComponent in the constructor of the inherited control. It works, so it isn't necessary here, but the warning remains. Also calling the base constructor or completely removing the constructor of the inherited control didn't help.

I tried do make the InitializeComponent method virtual in the base control and override it:

    protected override void InitializeComponent()
    { 
        base.InitializeComponent();
    }

But that only results in an error:

ERROR CS0111 Type 'ChartWithSkippedDataPointsControl' already defines a member called 'InitializeComponent' with the same parameter types

Controlboard.WpfClient C:\source\Controlboard.WpfClient\obj\Debug\net7.0-windows10.0.19041\Presentation\Controls\Charts\ChartWithSkippedDataPointsControl.g.cs 53 N/A

The only way how I can avoid it, is to create the inherited control also only from C# code without XAML. But I want to extend it using XAML, not only by code (if possible).


Solution

  • You should remove or rename the protected void InitializeComponent() method from the BaseChartControl.

    The XAML compiler generates a method with the same name for you behind the scenes. The (auto-generated) implementation of it locates a URI to the XAML markup to be processed as explained here.