Lucas
Lucas

Reputation: 3502

How to get WPF Application object from another class library?

I have a class library, let's call it Restarter. Now I want to call a method Restart() from an WPF Application.

Here some code placed in Restarter for better understanding

    public void Restart()
    {
        System.Windows.Application app = GetApplicationFromCaller();
        ...
    }

The Code from my WPF App

    public void Foo()
    {
        var restarter = new Restarter();
        restarter.Restart();
    }

Is it possible to get the Application from the calling method Foo?
I guess if at all, only through reflection.

Upvotes: 0

Views: 929

Answers (1)

YK1
YK1

Reputation: 7612

Application.Current will give you current application. However, ideally when you design a class library it must be agnostic of whether you call it from WPF app or any other type of application.

Upvotes: 2

Related Questions