How to restrict auto generated inheritance in System.Windows.Window in WPF - c#

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">

Related

Inheritance of a userControl from another one in wpf [duplicate]

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.

"Kproj.Forms.frmSwitch" cannot be the root of a XAML file because it was defined using XAML

I am aware that there are other questions posted that appear to be the same issue but none of them fix my issue.
I'm new to WPF I'm trying to convert a program from WinForms to WPF. I have a main window, "Kproj.Forms.frmLogin", that inherits a base class, "Kproj.Forms.frmSwitch", that inherits the System.Windows.Window class. WhenI got the above issue, my initial XAML code was:
<Control:frmSwitch x:Class="Kproj.Forms.frmLogin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Control="clr-namespace:Kproj.Forms"
mc:Ignorable="d"
Title="LOG IN" Height="309" Width="678">
<Grid Height="271" Width="662">
... Content
</Grid>
</Control:frmSwitch>
with these in the code-behinds:
namespace Kproj.Forms
{
public partial class frmLogin : frmSwitch
{
}
}
namespace Kproj.Forms
{
public partial class frmSwitch : Window
{
}
}
Upon further research, I found out that I needed to make frmSwitch into a base class with no XAML. Thus, I created frmSwitch2 in just general Class form that looks like this:
namespace Kproj.Forms
{
public class frmSwitch2 : Window
{
}
}
I then adjusted the main window XAML accordingly:
<Control:frmSwitch2 x:Class="Kproj.Forms.frmLogin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Control="clr-namespace:Kproj.Forms"
mc:Ignorable="d"
Title="LOG IN" Height="309" Width="678">
<Grid Height="271" Width="662">
... Content
</Grid>
</Control:frmSwitch2>
and the main window code-behind to:
namespace Kproj.Forms
{
public partial class frmLogin : frmSwitch
{
}
}
Now, I lost the original error message, but I gained a message that states
"The name 'frmSwitch2' does not exist in the namespace 'clr-namespace:FITS.Forms'."
even though it suggests "frmSwitch2" when I type "Control:" in the main window XAML, so it knows it does exist in the namespace. Any suggestions?
Disclaimer: I tried researching it as best as possible but all the posts I found on StackOverflow were this issue but all were fixed by converting from XAML\cs partial classes to solo code-behind full XAML-less class.
After looking into what the inheritance actually wanted, I learned that the only purpose of the inheritance was for variables so I was able to make it work but just converting them to static global variables and accessing them directly. I ended up not needing the inheritance after all.
If anyone else, that ends up knowing more MVVM, does come across a fix for this issue, it would be nice to know it. Even though I no longer need it, it would be good learning.

Partial declarations of 'Triangle.MainWindow' must not specify different base classes

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.

Global Code on Window Initialisation

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.

c# UserControl base class inheritance

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.

Categories

Resources