Accessing mainwindow.xaml.cs methods from another class - c#

hi i am relatively new at Wpf application
i want to know that can i create a object of mainwindow.xaml.cs
in some other class
And then using that object excess the user defined methods in mainwindow.xaml.cs
i am trying to something like this
it creates the object of mainwindow but can't access the method
the method which i am trying to access is public defined
MainWindow window = new Mainwindow();
window.point_to_screen();
it gives this error
Error 2 The type name 'point_to_screen' does not exist in the type 'System.Windows.Window'

You should cast the current mainwindow to a MainWindow object.
If you create a new window you won't be able to acces your current open window
MainWindow wnd = (MainWindow)Application.Current.MainWindow;
wnd.point_to_screen();

Make sure your method be in public protection level, and check if you use an instance method (not static) :
public class MainWindow
{
...
public void point_to_screen()
{
...
}
}
and use it:
MainWindow window = new Mainwindow();
window.point_to_screen();
if you use a static method your code should be like this:
public class MainWindow
{
...
public static void point_to_screen()
{
...
}
}
and use it:
MainWindow.point_to_screen();

Related

Secondwindow can't acces Textboxes of Mainwindow

So i have two Windows.
The first window stores all the data with textboxes, comboboxes etc.
In the second window i want to the user to enter some Information and based on the Information i want to Change something in the mainwindow.
I changed
public partial class Window2 : Window
to
public partial class Window2 : MainWindow
but it still does not work.
public partial class MainWindow : Window
{
int Languagetoken = 1;
public MainWindow()
{
InitializeComponent();
DateTextBox.Text = DateTime.Now.ToShortDateString();
}
...
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
{
Do not know how to do it. The code above is not in the same xaml they just Show the initialization of both.
There are several ways to solve this but the simplest one in your case is probably to inject Window2 with a reference to the MainWindow when you create it:
Window2 win = new Window2(this);
Window2:
public partial class Window2 : Window
{
private readonly MainWindow _mainWindow;
public Window2(MainWindow mainWindow)
{
_mainWindow = mainWindow;
InitializeComponent();
}
...
}
You could then access any internal or public member of the MainWindow in Window2 using the _mainWindow reference, e.g.:
_mainWindow.textBlock1.Text = "...";
By default text boxes and all other controls are represented by private member variables int he class representing your form so you won't be able to reach into it.
In order access it from outside, you can add a property inside the first window...
public string DataText
{
get
{
return DataTextBox.Text;
}
set
{
DataTextBox.Text = value;
}
}
Or you can change the access level of the controls to internal or public so that you can access it by using a reference to the window.

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

Using another class in WPF C# Project

How to make an object of another class in MainClass.
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
myClass obj = new myClass();
//obj.Show(); //not possible!!
}
public partial class myClass
{
void Show()
{
}
}
}
Now In this project, I can't access Show() method using "obj" object. How do I access method of another Class in this project??
You must declare Show as a public void to have access to the method.
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
obj.Show(); //and must be inside of a method, function or constructor.
}
myClass obj = new myClass();
//obj.Show(); //not possible beacause is not a public method.!!
}
public partial class myClass
{
//public method.
public void Show()
{
}
}
}
After reading your comments I understood that you are new WPF, and confusing some concepts with Console Applications.
In Console Applications, the Main method acts as an entry point, and everything in the method is executed in an orderly fashion from top to bottom, unless some function calls are made. Consider the following example.
Static void Main(string[] args)
{
myClass obj = new myClass();
obj.Show();
}
This code is valid because when the Console Application starts it executes from top to bottom. I mean it creates a myClass object and calls Show method, but in WPF it's different. The only method that executes immediately like Main is MainWindow Constructor. The code after the constructor is not auto executed, unless they are properties, just like a Console Application. I mean the following code will not work properly in a console application.
Static void Main(string[] args)
{
myClass obj = new myClass();
}
obj.Show();
It's because Show method is called out side of the Main method and the program doesn't know what to do with it. Similarly in WPF, you have to call the Show method in the constructor.
public MainWindow()
{
InitializeComponent();
obj.Show();
}
There are many ways to call Show method in WPF, and the above mentioned way is only one of them. The logic Console Applications and Logic of WPF Applications are similar and different at the same time. I suggest you read a few articles or books on WPF to clear things up.

Stack Overflow when trying to access form controls from class

I have a problem, after adding this code so I can access my MainWindow controls in Downloader class:
public partial class MainWindow : Form
{
private Downloader fileDownloader;
public MainWindow()
{
InitializeComponent();
fileDownloader = new Downloader(this);
}
//smth
}
and
class Downloader : MainWindow
{
private MainWindow _controlsRef;
public Downloader(MainWindow _controlsRef)
{
this._controlsRef = _controlsRef;
}
// smth
}
it now gives me "An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll" on line
this.mainControlPanel.ResumeLayout(false);
in MainWindow.Designer.cs. If i comment out the code above, it works fine. Any ideas please?
PS. Also, when I'm in Downloader class, should i access the controls like
textbox.Text
or
_controlsRef.textbox.Text
Both seem to give no compile errors, is there any difference between the two?
Your Downloader class inherits MainWindow. When you instansiate it, according to the C# specification, the base class is initialized first. When MainWindow initializes, it creates a new instance of Downloader, which causes it eventually to stackoverflow, because you're in an endless cyclic dependency.
Since Downloader inherits MainWindow, there is no point in getting an instance of it via your constructor. You can simply access it's protected and public members from your derived.
For example:
public partial class MainWindow : Form
{
protected string Bar { get; set; }
public MainWindow()
{
Bar = "bar";
InitializeComponent();
}
}
public class Downloader : MainWindow
{
public void Foo()
{
// Access bar:
Console.WriteLine(Bar);
}
}
It's problem because here you are having cyclic dependancy i.e. both the method waiting for each other to complete.
check
public MainWindow()
{
InitializeComponent();
fileDownloader = new Downloader(this);//this wait to complete downloader intialization
}
public Downloader(MainWindow _controlsRef)//this wait to complete mainwindow first
{
this._controlsRef = _controlsRef;
}
my mean to say as you are inheriting Downloader from MainWindow , which in constructor crating downloader instace...and downloader calls Mainwindow constructor first as its parent crates problem for you here
Solution
if want reference you can do like this
public partial class MainWindow : Form
{
protected MainWindow mainWindow;
public MainWindow()
{
InitializeComponent();
mainWindow = this;
}
//smth
}
class Downloader : MainWindow
{
public Downloader()
{
//this.mainWindow //will give you reference to main winsow
}
// smth
}

How can I call a methode from the MainWindow without making an instance of it?

I have a method in my Mainwindow, i want to call this method in an other usercontrol.
I dont use a static method because my MainWindow is not Static, and I can't make it static.
So I figured out to use this, but I dont know what comes behind the AS and I dont know if I can put a method is VAR?
I also can't make another MainWindow instance because that gives me a Stackoverflow exception.
How can I solve this?
var myMethode= mainWindow.FindName("MyMethode") as (should be a methode);
if (myMethode!= null)
{
//My code
}
You can define a static method on a class that is not static.
For example:
static void Main()
{
Foo foo = new Foo();
Foo.DoSomething();
foo.DoSomethingElse();
}
public class Foo
{
public static void DoSomething()
{
Console.WriteLine("DoSomething");
}
public void DoSomethingElse()
{
Console.WriteLine("DoSomethingElse");
}
}
But wouldn't it be a better solution to pass the MainWindow as a parameter into the User Control? So the user controls knows to which window it belongs and can access a function on it? (even better to declare an interface for this and pas the interface around).
This would look like:
public interface IWindow
{
string SomeWindowActivity();
}
public class MyUserControl
{
public IWindow Window { get; set; }
public void SomeActionOnUserControl()
{
string data = Window.SomeWindowActivity();
}
}
public class MainWindow : IWindow
{
MyUserControl MyUserControl { get; set; }
public MainWindow()
{
// Link the UserControl to the Window it's one. This can be done trough the
// constructor or a property
MyUserControl.Window = this;
}
public string SomeWindowActivity()
{
// Some code...
return "result";
}
}
Try this
((MyMainWindow)Application.Current.MainWindow).Method()
You don't need to make MainWindow singleton in your case, you have access to it from Application.Current singleton
Application.Current.MainWindow
Hope this helps
Short unswer: you can't. You want to call a instance method, you need to have an instance.
The fact that MainWindow is not static does not prevent you from defining static methods in it, as long as those methods do not use other instance members, so if it a helper method, you can define it static and call from other place, it might a good idea to refactor it out of MainWindow class then.
If it's a nonstatic method, you claim you don't want to create second instance of MainWindow, why not call it on first instance then, by passing it to your control?
Also, if creating another instance of MainWindow gives you stackoverflow, maybe it's because you just did some recurrent call with this method, and it can be fixed?

Categories