Gus
Gus

Reputation: 10659

WPF - How to access method declared in App.xaml.cs?

How can I access, using C#, a public instance method declared in App.xaml.cs?

Upvotes: 29

Views: 22507

Answers (3)

Robert
Robert

Reputation: 2523

You can also add a static property in your App class with new modifier like so:

public static new App Current => Application.Current as App;

This way you can access your method like so:

App.Current.YourMethod()

Check this answer about new modifier on SO.

Upvotes: 0

thomasmartinsen
thomasmartinsen

Reputation: 1993

Have you considered to create a separate class to hold your application wide methods (eg. AppState.cs)?

Upvotes: 4

Akash Kava
Akash Kava

Reputation: 39916

((App)Application.Current).YourMethod ....

Upvotes: 75

Related Questions