I have added two windows to a Silverlight 5 application project: FooWindowBase
and SomeFooWindow
. The latter is a sub-class of the former. Both contain a default constructor calling InitializeComponent
, and apparently Visual Studio or the compiler autogenerates that method. This leads to the following compiler warning in the derived class SomeFooWindow
that…
"sub
InitializeComponent
shadows an overloadable member declared in the base classFooWindowBase
. If you want to overload the base method, this method must be declaredOverloads
."
Since InitializeComponent
was autogenerated in both cases, there seems to be no way for me to add a Shadows
over Overloads
specifier.
Is there any way to prevent, or get rid of, this compiler warning without editing the autogenerated code?
FooWindowBase
:
XAML:
<c:ChildWindow
x:Class="FooNamespace.FooWindowBase"
xmlns:c="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
…>
…
</c:ChildWindow>
Code:
Imports System.Windows.Controls
Partial MustInherit Class FooWindowBase : Inherits ChildWindow
Public Sub New()
InitializeComponent()
End Sub
…
End Class
SomeFooWindow:
XAML:
<l:FooWindowBase
x:Class="FooNamespace.SomeFooWindow"
xmlns:l="clr-namespace:FooNamespace;assembly=Foo"
…>
…
</l:FooWindowBase>
Code:
Partial Class SomeFooWindow : Inherits FooWindowBase
Public Sub New()
InitializeComponent()
End Sub
…
End Class
I'll post the workaround I have ended up using for others who are facing the same problem.
One posted answer to this question asked on Code Project mentions that:
"Currently deriving a XAML generated class from another XAML generated class is not supported."
and the solution would be to:
"[…] define your base class all in code without using XAML."
The exact same words are mentioned on this Microsoft Knowledge Base article, btw. So basically, the compiler warning cannot be gotten rid of because that inheritance scenario I've posted simply isn't supported.
I ended up doing the following:
SomeFooWindow
into a UserControl
called SomeFooControl
;FooWindowBase
to FooWindow
and removed the MustInherit
modifier;ContentPresenter
control to FooWindow
;instead of instantiating the previous SomeFooWindow
, instantiated a FooWindow
and set the ContentPresenter
's content to an instance of a SomeFooControl
:
Dim someFooWindow = New FooWindow(…)
someFooWindow.Content = New SomeFooControl(…)