Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have several XAML "Page"s for which I would like to pull the facets that are in common together into a Page class extension I am calling ACPage. I must be missing some essential piece of knowledge, although I think I am pretty close. Can someone tell me what I am missing? I looked for an article on this (rather basic) topic, but didn't find one.

I get the following warning:

Warning 1 'Al_Cr.PageWelcome.InitializeComponent()' hides inherited member 'Al_Cr.ACPage.InitializeComponent()'. Use the new keyword if hiding was intended. D:\Projects\Al Cr\Al Cr\Al Cr\obj\x86\Release\PageWelcome.g.cs 50 21 Al Cr

and this error:

Error 2 'Al_Cr.ACPage' cannot be the root of a XAML file because it was defined using XAML. Line 1 Position 15. D:\Projects\Al Cr\Al Cr\Al Cr\PageWelcome.xaml 1 15 Al Cr

The warning points to the auto-generated PageWelcome.InitializeComponent() function definition. The error points to the end of the <big>local:ACPage</big>, below.

My ACPage.xaml & ACPage.xaml.cs are defined thusly:

<Page x:Class="Al_Cr.ACPage"
      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" 
      Style="{StaticResource StylePage}">
</Page>


public partial class ACPage : Page {
    public ACPage() {
    }
}


My PageWelcome.xaml & PageWelcome.xaml.cs which is a subclass of ACPage are defined thusly:

<<big>local:ACPage</big> x:Class="Al_Cr.PageWelcome"
      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:Al_Cr" >
    <Canvas>
         ...Welcome page stuff...
    </Canvas>
</local:ACPage>


public partial class PageWelcome : ACPage {
     public PageWelcome() {
         InitializeComponent();
     }
}


I am using MS Visual Studio C# 2010 Express (Version 10.0.30319.1) and .NET 4.0.30319.

Thank you in advance!
Posted

I can't help you with the XAML error, but the warning I can explain.

In a WPF page, InitializeComponent() is a designer-generated, non-virtual (is there a name for that?) method. In the .cs code-behind file, click the InitializeComponent() and hit F12 to view the designer generated code.

Since your derived page inherits another WPF page object, both objects have non-virtual designer-generated methods called InitializeComponent(). C# considers it bad form ( :rolleyes: ) for base and derived classes both to implement non-virtual/override methods of the same name and gives you a warning about it.

However, since the methods are getting called from their respective constructors, it's a warning that you can ignore because the right InitializeComponent() method will be called from the right constructor.

I'm interested to hear what people who know WPF have to say about the error - and if/how you can inherit one WPF page from another.

Chris
 
Share this answer
 
Hi!

You get that error, because: "Currently deriving a XAML generated class from another XAML generated class is not supported."
Solution is: "You need to define your base class all in code without using XAML."
:((

Source

Ike
 
Share this answer
 
Probably worth checking out the same topic on SO:

http://stackoverflow.com/questions/39843/how-do-i-create-a-base-page-in-wpf[^]

Matt Hamilton's response there suggesting the use of attached events is also worth considering.
 
Share this answer
 
v2
Gregory,
I don't know if you have received an answer for this yet, but I will attempt to here:

You will want to create a custom control and derive this control from the Page object as follows:
C#
namespace Lotamor.Intranet.Controls
{
    public class NavPage : Page
    {
        static NavPage()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NavPage),
             new FrameworkPropertyMetadata(typeof(NavPage)));
        }
    }
}

For this example, I will not get into much more for the control code.
The following is a complete XAML for this control.

XML
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Lotamor.Intranet.Controls">

    <Style TargetType="{x:Type local:NavPage}"
         BasedOn="{StaticResource {x:Type Page}}">
        <Setter Property="Template">
            <Setter.Value>
		<ControlTemplate TargetType="{x:Type local:NavPage}">
                    <Grid>
			<Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <!-- for the users (developer) content -->
                        <ContentPresenter Grid.Row="0" Grid.Column="0"/>
                        <!-- a button or link panel -->
			<ItemsControl Grid.Row="0" Grid.Column="1" Margin="10">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <VirtualizingStackPanel 
                                        Orientation="Vertical" 
                                        Margin="10"
                                        VerticalAlignment="Top"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <!-- some uninteresting content -->
                            <Button Margin="10" Width="120" Height="36"
                                Content="Right Button One"/>
                            <Button Margin="10" Width="120" Height="36"
                                Content="Right Button Two"/>
                            <Button Margin="10" Width="120" Height="36"
                                Content="Right Button Three"/>
                            <Button Margin="10" Width="120" Height="36"
                                Content="Right Button Four"/>
                        </ItemsControl>
                        <!-- the "footer" of the page. -->
                        <ItemsControl Grid.Row="1" Grid.Column="0"
                            Grid.ColumnSpan="2" Margin="10">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel Orientation="Horizontal"
                                        ItemWidth="220" ItemHeight="40"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <!-- more uninteresting content -->
                            <Button Margin="10,0" Width="200" Height="36"
                                Content="Bottom Button One"/>
                            <Button Margin="10,0" Width="200" Height="36"
                                Content="Bottom Button Two"/>
                            <Button Margin="10,0" Width="200" Height="36"
                                Content="Bottom Button Three"/>
                            <Button Margin="10,0" Width="200" Height="36"
                                Content="Bottom Button Four"/>
                            <Button Margin="10,0" Width="200" Height="36"
                                Content="Bottom Button Five"/>
                            <Button Margin="10,0" Width="200" Height="36"
                                Content="Bottom Button Six"/>
                        </ItemsControl>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

The grid divides up the page into 4 sections.
In the first row (0) and column (0) is where your content of you page will be.
XML
<Canvas>
     ...Welcome page stuff...
</Canvas>

See:
XML
<ContentPresenter/>

At row(0) and column(1) is a button button pannel.
At row(1) spanning both columns is a footer.

This should demonstrate the "Master" page for your WPF (browser) application.
I find it to work well.

Cheers,
Jim
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900