I've been working on a commandline application, and have recently decided to add a wpf window to the application. I added this as a UserControl, however I noticed I can't call this class using ShowDialog() from my main code;
I've tried changing the Base class from a UserControl to Window, however an error occurs;
public partial class UserControl1 : Window
{
public UserControl1()
{
InitializeComponent();
}
Error 1 Partial declarations of
'ExcelExample.UserControl1' must not
specify different base
classesExcelExample
I've added all the references found in my other WPF application to no avail. Help!
In order to change the base class it is not sufficient to change it in code only. You must also change the root tag and any nested elements in accompanying XAML file. For example, you have something like:
<UserControl x:Class="Your.Namespace.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
</UserControl.Resources>
</UserControl>
You must change it to something like:
<Window x:Class="Your.Namespace.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
</Window.Resources>
</Window>
Related
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.
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.
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.
I have a class called BIFUserControl which inherits from UserControl class. Now I am designing a new user control called BIFText which inherits from BIFUserControl class. So, I changed the XAML file called BIFText.xaml as follows :
<base:BIFUserControl
xmlns:base="clr-namespace:BaseInputFramework.BaseWidgets;assembly=BaseInputFramework"
x:Class="BIFWidgetLibrary.Text.BIFText"
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:mp="clr-namespace:Microsoft.Multipoint.Sdk.Controls;assembly=Microsoft.Multipoint.Sdk.Controls"
xmlns:utils="clr-namespace:BaseInputFramework.BaseWidgets.Utils;assembly=BaseInputFramework"
mc:Ignorable="d"
d:DesignHeight="150" d:DesignWidth="150">
<Grid>
</Grid> </base:BIFUserControl>
And then I changed my BIFText.xaml.cs file as follows:
namespace BIFWidgetLibrary.Text {
public partial class BIFText : BIFUserControl
{
public BIFText()
{
InitializeComponent();
}
} }
But now when I try to build the project, I get the following error message :
'BaseInputFramework.BaseWidgets.BIFUserControl' cannot be the root of a XAML file because it was defined using XAML. Line 2 Position 14.
Can someone please help me with this error. Thanks in advance.
Error says itself that BaseInputFramework.BaseWidgets.BIFUserControl cannot be root of a XAML file since it is defined using XAML. Only elements which are not defined using XAML file can only be root element. Refer to these links - Cannot define root element and Inheriting from UserControl
UserControls work by setting the Content to what you define in XAML, this might be the reason why you cannot inherit like that: The base class' content would be overwritten.
If you don't mind completely replacing the content you might as well use a custom control and define a Template instead.