This question already has answers here:
Inheriting from a UserControl in WPF
(6 answers)
Closed 2 years ago.
I code in wpf c#
partial class A : userControl
{
}
partial class B : userControl
{
}
But i have to make an inheritance between them
partial class A : userControl
{
}
partial class B : A
{
}
But when i do that i have an error saying :
Partial declarations of B must not specify different base classes
You cannot inherit from a user control that has a XAML file. You need to create a plain C# class as the base class:
// File: A.cs
public class A : UserControl
{
public A()
{
// create your controls programmatically
}
}
Then in your sub class's XAML file:
// File: B.xaml
<local:A x:Class="WpfApp1.B"
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:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
</Grid>
</local:A>
(replace WpfApp1 with your namespace, obviously)
Note that the root tag name is local:A instead of UserControl. This identifies your control's base class. The x:Class=... part specifies the class name of the inherited user control.
Related
I am having WPF form which is inherited with FormBase.cs which is of type Window. Now I change normal forms inheritance with this created FormBase.cs base class. But now problem is every time partial class which is inherited with FormBase.cs automatically change to default inheritance which is Windows in every change in form. And I found
Partial declarations of 'Wpf_Fomrs.MainWindow' must not specify different base
this error while compiling code.
Code:
[System.ComponentModel.DesignerCategory("")]
public class FormBase : Window, IFormBase // Base class
{
}
[System.ComponentModel.DesignerCategory("Form")]
public partial class MainWindow : FormBase // Created normal form
{
}
//Auto generated partial class which again inherited with `Window` class
public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector {
}
// I want this result every time.
public partial class MainWindow : System.Windows.Markup.IComponentConnector {
}
While using Win Forms I dont find such problem.
What is problem with code generation logic in WPF forms?
EDIT
XAML of MainWindow looks like
<Window x:Class="Wpf_Forms.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Form1" Height="350" Width="525" SizeToContent="WidthAndHeight" xmlns:my="clr-namespace:Foundation.ControlsWpf;assembly=Foundation" Loaded="Window_Loaded" Topmost="True" WindowState="Maximized">
I've been facing an error that tells me that the partial declarations must not specify a different base class.
public partial class MainWindow : Shape
{
public MainWindow()
{
InitializeComponent();
this.Stretch = System.Windows.Media.Stretch.Fill;
this.StrokeLineJoin = PenLineJoin.Round;
}
I get error from :
public partial class MainWindow : Shape
The 'MainWindow' gives me error about the specifying of a different base. How do i go about rectifying this error?
My XAML currently, is the default one:
<Window x:Class="Triangle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
I have yet to edot anything from the XAML as this codes are codes i found somewhere from online and is using it to try out whether or not it work.
MainWindow normally extends Window.
So in your code-behind you'll see public partial class MainWindow : Window, and in your associated XAML you'll see something like:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
... />
...
</Window>
To extend another class (not sure what Shape is, but I'm assuming it's appropriate in this case), you'll have to correct your XAML in addition to the code-behind... something like this:
<Shape x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
... />
...
</Shape>
MainWindow : Shape? I suppose it to be MainWindow : Window
do please verify the base class (root element) from the designer of the MainWindow.xaml and use the same base class here.
typically top level window classes like default MainWindow class derives from Window. whereas in your case I can see it is being derived from Shape
if you are trying to create a shape class then there is no InitializeComponent() in shape class and it does not need a designer hence a partial class is not required. last but not the least the class name MainWindow does not sounds a nice name for the same. you may perhaps revise it.
your main page should be in the format
public sealed partial class MainPage : Page
inheriting only Page Class.
Any way to create a global piece of code to run on the initialisation of all Windows like there you can create global properties for XAML through App.xaml?
I'm just curious as the piece of code I'm using relates specifically to interface but can't be set in xaml so must be in code so I have to write it into the constructor of each Window. Just wondering if there might be a work around.
you can solve this problem with the concept of inheritance create Base class that inherits Window do your common stuff in that class . All of Windows that want this common functionality will inherit the base class.
Base class
public class MyBaseWindow : Window
{
//do your common stuff in this base class for all windows
protected object MyProperty { get; set; }
}
.cs
public partial class MyWindow : MyBaseWindow
{
xaml
<local:MyBaseWindow x:Class="WpfApplication1.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MyWindow" Height="300" Width="300">
I hope this will give you an idea.
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.
I'm trying to create a custom base page for my WP app. I'm doing this by creating a new cs class file that inherits from PhoneApplicationPage like this:
public class BasePage: PhoneApplicationPage
{
//handle common non-visual stuff here
}
The problem is that I would like to add a control on every page that uses BasePage but BasePage has no LayoutRoot or any visual element that I can attach the control to. Is there a way I can add the same control to all the pages that use BasePage so that I don't have to copy/paste it every time?
Edit: Adding XAML based on TriggerPin's answer. I've created a new XAML file for my BasePage.cs but this is throwing errors in BasePage.g.cs:
"MyApp.MainPage already contains a definition for '_contentLoaded'" (and similar message for 'LayoutRoot' and 'InitializeComponent')
<local:BasePage
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyApp.Template"
x:Class="MyApp.MainPage"
mc:Ignorable="d">
Notice that by default, types derived from PhoneApplicationPage are partial
public partial class MainPage : PhoneApplicationPage
The partial definition allows you to split the class definition across multiple files. Most commonly with this type, the definition is split between a .cs and .xaml file.
<phone:PhoneApplicationPage x:Class="MyProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone">
<!-- Custom layout here -->
</phone:PhoneApplicationPage>
If you make your implementation a partial class and provide base xaml code, you will be able to do what you want to achieve.
You need a container which must be Panel type at least for adding your children controls. A Page is just a UserControl which couldn't add your UIElement into it directly.
Try this:
Panel container = // init it from your page, you must have a root element in page.
container.Children.Add(new TextBlock()); // your children control.
I did it in Win8.