Answered by:
How to make Win Store app page inherit from BasePrintPage from sample Print app

Question
-
Hello. The PrintSample app comes with a class called BasePrintPage. This class inherits from the Page class. I would like the main page of my app to inherit from BasePrintPage instead of Page, so that I can print the page. Does anyone know how I can do so? I tried to manually change the inheritance in the behind code for my main page, but the compiler complained that another partial definition of the MainPage class, did not inherit from the same class. I went into the MainPage.g.i.cs file and changed the MainPage class declaration there to inherit from BasePrintPage as well, but the compiler still complained that the MainPage class is not inheriting from the same class, in the various places the partial class is declared. Can someone indicate to me what I should do?
Initial class definitions .....
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
.
.
.
}(from MainPage.xaml.cs)
partial class MainPage : global::Windows.UI.Xaml.Controls.Page
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 4.0.0.0")]
private global::Windows.UI.Xaml.Controls.AppBar bottomAppBar;
.
.
.
}(from MainPage.g.i.cs)
Changed class definitions .....
public sealed partial class MainPage : PrintSample.BasePrintPage
{
public MainPage()
{
this.InitializeComponent();
.
.
.
}(from MainPage.xaml.cs)
partial class MainPage : global::PrintSample.BasePrintPage
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 4.0.0.0")]
private global::Windows.UI.Xaml.Controls.AppBar bottomAppBar;
.
.
.
}(from MainPage.xaml.cs)
Monday, September 30, 2013 6:36 PM
Answers
-
You need to set it in the Xaml as well as in the code behind. Something like:
<local:BasePrintPage x:Class="TestApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestApp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> </Grid> </local:BasePrintPage>
--Rob- Marked as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Tuesday, October 1, 2013 7:21 PM
Monday, September 30, 2013 6:48 PMModerator -
FYI: using this article as a guide, I managed to successfully get the Main Page of my Windows Store app, to derive from the BasePrintPage class, which I copied over from the PrintSample Win Store app. The code behind for the Main Page is the same as I had it before:
Changed class definitions .....
public sealed partial class MainPage : PrintSample.BasePrintPage
{
public MainPage()
{
this.InitializeComponent();
.
.
.
}(from MainPage.xaml.cs)
However, as you alluded to, I also changed the XAML for the Main Page from the following:
<Page
x:Class="Envelope_Printer.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Envelope_Printer"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
.
.
.
</Grid>
</Page>to the following:
<src:BasePrintPage
x:Class="Envelope_Printer.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Envelope_Printer"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:src="using:PrintSample"
mc:Ignorable="d">
.
.
.
</Grid>
</src:BasePrintPage>As you can see, I made the following changes:
Before Change After Change
<Page <src:BasePrintPage
</Page> </src:BasePrintPage>Also the following line was added to the namespace declarations at the top of the code:
xmlns:src="using:PrintSample"
Quite simply, the above adjustments show that BasePrintPage is the base class of MainPage class, and that the definition of BasePrintClass can be found in the PrintSample namespace, defined within my Windows Store app.
- Marked as answer by PDoug Tuesday, October 1, 2013 7:15 PM
Tuesday, October 1, 2013 7:15 PM
All replies
-
You need to set it in the Xaml as well as in the code behind. Something like:
<local:BasePrintPage x:Class="TestApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestApp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> </Grid> </local:BasePrintPage>
--Rob- Marked as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Tuesday, October 1, 2013 7:21 PM
Monday, September 30, 2013 6:48 PMModerator -
I'm not exactly sure what you mean. Are you saying I'm supposed to make some sort of reference in the XAML file to the base class for the MainPage class? Why would I need to do so, when I don't do so, when the MainPage class inherits from the Page class? Also, what would the syntax for the line look like? In addition, I'm not sure if the partial class declaration for MainPage in the MainPage.g.i.cs file I put down is correct, because the code editor doesn't give me that much feedback.Monday, September 30, 2013 7:35 PM
-
The Xaml tag for the page has to match the class name.
If you look at your MainPage.xaml file it defaults to <Page></Page> tags, and if you change MainPage to derive from something else you need to change those tags to match like I did in the bolded elements in the Xaml snippet I provided. This needs to match the class that the MainPage derives from so the generated file knows how to define the partial class.
You should never edit the generated MainPage.g.i.cs file. This will be generated (the ".g." stands for generated) for you from the Xaml. Your edits will be replaced by a newly generated version when you compile.
--Rob
Monday, September 30, 2013 10:45 PMModerator -
FYI: using this article as a guide, I managed to successfully get the Main Page of my Windows Store app, to derive from the BasePrintPage class, which I copied over from the PrintSample Win Store app. The code behind for the Main Page is the same as I had it before:
Changed class definitions .....
public sealed partial class MainPage : PrintSample.BasePrintPage
{
public MainPage()
{
this.InitializeComponent();
.
.
.
}(from MainPage.xaml.cs)
However, as you alluded to, I also changed the XAML for the Main Page from the following:
<Page
x:Class="Envelope_Printer.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Envelope_Printer"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
.
.
.
</Grid>
</Page>to the following:
<src:BasePrintPage
x:Class="Envelope_Printer.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Envelope_Printer"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:src="using:PrintSample"
mc:Ignorable="d">
.
.
.
</Grid>
</src:BasePrintPage>As you can see, I made the following changes:
Before Change After Change
<Page <src:BasePrintPage
</Page> </src:BasePrintPage>Also the following line was added to the namespace declarations at the top of the code:
xmlns:src="using:PrintSample"
Quite simply, the above adjustments show that BasePrintPage is the base class of MainPage class, and that the definition of BasePrintClass can be found in the PrintSample namespace, defined within my Windows Store app.
- Marked as answer by PDoug Tuesday, October 1, 2013 7:15 PM
Tuesday, October 1, 2013 7:15 PM