简体   繁体   中英

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM