I'm trying to add a few extra base properties (somewhere) in my project so that every window I create will automatically inherit these properties.
E.g. each window will have a "block_count" based on the area of the window
For context, I'm using a WPF project.
Currently trying: I've created an abstract window class with my desired properties and then defined my MainWindow as one of these types to inherit the property and method...
But when coding in my MainWindow class I still can't access the property or method even though it should have inherited them? Where am I going wrong or what is the best way to add properties to all windows in my project?
My Abstract Window class
public abstract partial class AbWinDefiner : Window
{
private int _block_count;
private void get_block_count()
{
_block_count = Convert.ToInt32(Math.Floor(Width * Height / 32));
}
public AbWinDefiner()
{
InitializeComponent();
}
}
My Main Window class
public partial class MainWindow : AbWinDefiner
{
public MainWindow()
{
InitializeComponent();
}
}
Make it protected, not private
Related
Let's say in a WPF project I've built an abstract window class so I can add some base properties to every window I create derived from that:
// Abstract window, based on normal window
public abstract partial class abs_window : Window
{
// example of added property to my abstract class
protected int xxx = 25;
public abs_window()
{
InitializeComponent();
}
}
// Another class dervied from my abstract window
public partial class derivedWindow : abs_window
{
void aa()
{
// Random method aa() to show access to the protected xxx int
MessageBox.Show(Convert.ToString(xxx));
}
}
So, this compiles fine, but my StartUpUri is pointing to the default MainWindow that I don't really want at all... I want it to point straight to a new instance of my doubly derived derivedWindow class? Is that possible?
I tried both
StartupUri="abs_window.xaml" and StartupUri="derivedWindow.xaml"> but the first couldn't work since it's abstract and the 2nd doesn't work because there isn't an existing .xaml file
If I add a new "window" .xaml file it'll just be a normal window and not my derived type!?
Answering on behalf of Hans' comment unless he chooses to answer also:
First created a startup method in the default App class (App.xaml.cs) like Hans' link here
public partial class App : Application
{
void App_Startup(object sender, StartupEventArgs e)
{
derivedWindow window = new derivedWindow();
window.Show();
}
}
But what that link didn't mention was to change the App.xaml file by replacing a StartupUri which seems to be always referencing an .xaml with a Startup method StartupUri="MainWindow" with Startup="App_Startup" which I found here
In my application, I have a BaseForm which has a generic member in it:
public partial class BaseForm<T> : Form where T : Presenter
{
protected T Presenter;
public BaseForm()
{
InitializeComponent();
}
}
Now what i need is to have a form which is inherited from my BaseForm
public partial class SampleForm : BaseForm<SamplePresenter>
{
public SampleForm()
{
InitializeComponent();
Presenter = new SamplePresenter();
}
}
The problem is that the Visual Studio designer does not show my SampleForm derived from BaseForm<T>.
It gives this warning:
Warning 1 The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file:
SampleForm --- The base class 'Invoice.BaseForm' could not be loaded. Ensure the assembly has been referenced and that all projects have been built. 0 0
How can I overcome this?
P.S. I looked at this post but didn't really get the whole idea of how to solve this.
The designer doesn't support this, as described in that post.
You need this base class:
public partial class SampleFormIntermediate : BaseForm<SamplePresenter>
{
public SampleFormIntermediate()
{
InitializeComponent();
Presenter = new SamplePresenter();
}
}
And you need to use this class for the Visual Studio designer:
public partial class SampleForm : SampleFormIntermediate
{
}
In that way, Visual Studio 'understands' what to open in the designer and how to open it.
I have the following setup and I'm wondering if what I'm doing is the best approach:
Project1 //reusable code and controls
mainclass.cs
usercontrol1.xaml
usercontrol2.xaml
Project2 //referencing Project1
mainwindow.xaml //containing usercontrol1.xaml and usercontrol2.xaml
Project1 contains a class and user controls used in other projects, for example Project2.
As the mainwindow in Project2 is initialized I create a new instance of mainclass and pass it as variable to the usercontrols, I also pass the usercontrols as variables to the mainclass.
In this way from the mainclass I can have access to all public methods and variables in the usercontrols and vice versa. Is this a good approach?
public partial class MainWindow : Window
{
public MainClass MC = new MainClass();
public MainWindow()
{
InitializeComponent();
MC.usercontrol1 = this.usercontrol1;
MC.usercontrol2 = this.usercontrol2;
usercontrol1.mainClass = MC;
usercontrol2.mainClass = MC;
}
...
I know that part of this would be more appropriate if done via Events, but the number of methods and variables I need to cross-access between the class and the usercontrols is very high, and doing this way seems to work just fine...
Thanks!
Why not have a constructor in the MainClass since all those classes are in the same project:
public MainClass(usercontrol1 uc1, usercontrol2 uc2)
{
uc1.mainClass = this;
uc2.mainClass = this;
this.usercontrol1 = uc1;
this.usercontrol2 = uc2;
}
then,
public partial class MainWindow : Window
{
public MainClass MC = new MainClass(this.usercontrol1, this.usercontrol2);
public MainWindow()
{
InitializeComponent();
}
...
Otherwise I am not sure what you are trying to do exactly.
Circular dependencies are bad and tightly bound classes are also bad, since they break the open closed principle...
I am not sure what you are trying to accomplish, but I would suggest another class to compose all three classes or else use events if you can.
I am creating an application with a lot of forms and the visual style required takes a lot time to be applied from the designer, so I created a class called Layout to apply these
property changes to every form on its Load() method.
class Layout : Form
{
public void ApplicarLayout(Form frm)
{
frm.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
foreach (Control c in frm.Controls)
{
if (c is TextBox)
{
//Apply textBox Formatting
}
//Iterate through the controls in the form and add respective format
}
}
}
So far, so good. My plan was to then inherit this class from every form and just call the base.AplicarLayout() method.However, I get the error:
Inconsistent accessibility: base class 'EntityClub_.Layout' is less accessible than class 'EntityClub_.MainAdminWindow'
Here you can see how I do it.
public partial class MainAdminWindow : Layout
{
public MainAdminWindow()
{
InitializeComponent();
}
public void MainAdminWindow_Load(object sender, EventArgs e)
{
base.ApplicarLayout(this);//ERROR HERE
}
}
Do you know how can I do this using inheritance? I don't want to instance the class and I don't want to pollute each window´s code with the layout method.
Classes without an explicit access modifier (and aren't nested) are implied to be internal.
Therefore:
class Layout : Form
..is internal, whereas:
public partial class MainAdminWindow : Layout
..is public (because you've explicitly said so). Changing Layout's declaration to this solves the issue:
public class Layout : Form
I am having trouble calling base constructors in wpf windows:
public class TemplateWindow : Window //Template window class
{
public TemplateWindow (int no)
{
}
}
public partial class MainView : TemplateWindow
{
public MainView() : base(1) //error here
{
InitializeComponent();
}
}
It gives me an error at the indicated location as it apparently is trying to call the Window constructor with base instead. The MainView class is the code behind of a xaml window.
However, when I tested the problem like below, it works perfectly fine.
class A //Base Class
{
public A() { }
}
class B : A
{
public B(int no) { }
}
partial class C : B
{
public C() : base(1) { }
}
What am i doing wrong?
You have your MainView class defined in XAML, don't you? It probably goes something like this:
<Window x:Class="MyNamespace.MainView" ... >
...
</Window>
Note the big Window word right at the beginning. It tells the compiler that you want this XAML to generate a class named MyNamespace.MainView, and you want it to inherit from Window. So that's what the compiler does: it happily generates your class and makes it inherit from Window. Right-click the InitializeComponent word and choose "Go to Definition". This will take you to the autogenerated file, and you'll be able to see the class.
Now, if you want MainView to inherit from TemplateWindow, you just have to say so in your XAML:
<my:TemplateWindow
xmlns:my="MyNamespace"
x:Class="MyNamespace.MainView" ... >
...
</my:TemplateWindow>
But that will give you another problem: now, all of a sudden, you can't use the visual designer.
That would be because the designer cannot create an instance of your TemplateWindow class for editing. Why? Well, because TemplateWindow doesn't have a default constructor, of course!
So for this kind of thing to work, you'll just have to define two constructors in TemplateWindow - one default, and one accepting an int.
Good luck.
Here is your answer
http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx