Changing the startupuri to a derived window class - c#

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

Related

How can I call one constructor of partial class in WPF?

I have multiple constructors and I would like to to call one constructor that have parameters , how can I do that?
My UserControl :
public UserControl1()
{
InitializeComponent();
}
public UserControl1(string name)
{
InitializeComponent();
Console.WriteLine(name);
textbox1.Text = name;
}
App.xaml.cs
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
UserControl1 us = new UserControl1("John");
}
}
I made a breakpoints on UserControl1 construct that contains parameters and I saw that the the App startup called the constructor with parameters and than return to the other default constructor.
What's the reason?
Firstly, did you check app.xaml file because there is probably xaml code which tries to open and show UserControl1 like:
StartupUri="UserControl1.xaml"
If that case, you need to delete it and show UserControl1 in OnStartup function.
Secondly, are you sure that there isn't any other UserControl1 in your project? If there is, have you ever tried to use namespace? Like:
SolutionName.ProjectName.UserControl1 us = new SolutionName.ProjectName.UserControl1("John");
Thirdly, does the program read textbox1.Text = name; line then goes to default constructor section to read? Could you please give more detail about it and your namespaces?

c# Adding new properties to all Windows in my App

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

A global function to be executed before opening a form

I have around 15 forms and every form includes some similar piece of codes.
What i want to know is that there is any way to automatically call a function containing that particular piece of code when a form is opening?
Like, lets say i want to show Hello World message every time when any form of a project is loaded.
So what i can do is i can create a module or class file and i can add a piece of code their and i can call it in every form.
But this i don't want, what i want is that, is there any way where i can add this piece of code and automatically it gets populated/triggered when a form gets loaded.
Maybe we can call it something like - auto calling function for forms
Like, whenever a form is opening automatically a class or function gets called without defining it in the particular form. Maybe a library kind of thing which will be called anyways when a form is loaded and i can add my piece of code there and it gets executed.
Create your own base class inheriting from form:
public abstract class FormBase : Form { /*...*/ }
Then every form you are using may inherit from this base class:
public class MyForm : FormBase { /*...*/ }
You can add an event handler to the form, and put whatever code needs to be run in there.
For Example:
this.Load += new System.EventHandler(this.FormName_Load);
public class frm_Base : Form
{
public void frm_Base()
{
this.Load += new System.EventHandler(this.frm_Base_Load);
}
public void frm_Base_Load(object sender, EventArgs e)
{
OnFormLoad();
}
public virtual OnFormLoad()
{
MessageBox.Show("Hello World");
}
}
public class frm_Derived : frm_Base
{
public override OnFormLoad()
{
base.OnFormLoad();
MessageBox.Show("Another Hello From Derived");
}
}
You can now inherit the functionality that happens on load in all of your other forms as well as do other things too by making your load method virtual.

Inherited Window can not have a name?

I'm having trouble with naming my Window which is inherited from its Base Window,
when I try to give a name to my Window I get following error.
The type BaseWindow cannot have a Name attribute. Values types and types without a default constructor can be used as items within ResourceDictionary.
XAML :
<log:BaseWindow
x:Class="EtraabMessenger.MainWindow"
x:Name="main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:log="clr-namespace:EtraabMessenger.MVVM.View.Controls"
xmlns:VMCore="clr-namespace:EtraabMessenger.MVVM.VMCore"
VMCore:WindowClosingBehavior.Closing="{Binding DoCloseMainWindowCommand}"
Height="464" Width="279">
</log:BaseWindow>
EDIT : Here is my BaseWindow class
public abstract class BaseWindow : Window, INotifyPropertyChanged
{
protected BaseWindow()
{
// Note (Important) : This message should register on all windows
// TODO : I'm planning to move this registeration to BaseWindow class
Messenger.Register<bool>(GeneralToken.ClientDisconnected, DisconnectFromServer);
}
protected abstract void DisconnectFromServer(bool isDisconnected);
protected abstract void RegisterTokens();
protected abstract void UnRegisterTokens();
....
....
....
}
Any advice will be helpful.
Your base window apparently, as the error states, needs a public default contructor (one without arguments), it also may not be abstract because an instance of it needs to be created.

Calling Constructors in Multi-Level Inheritance WPF C#

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

Categories