How can I access, using C#, a public instance method declared in App.xaml.cs?
Asked
Active
Viewed 2.2k times
3 Answers
75
((App)Application.Current).YourMethod ....

Akash Kava
- 39,066
- 20
- 121
- 167
-
It works, but I don't understand. If Application.Current == App, and they are the same type, how is this type-cast working? Thanks – Gus Cavalcanti Oct 22 '09 at 18:58
-
2Application is System.Windows.Application where else App is inside your project derived from Application, Application.Current is of type Application, not App. – Akash Kava Oct 22 '09 at 19:08
-
1Thanks. Is it possible to do this right from a xaml-view ? (Like _Click="App.YourMethod"_) – mYnDstrEAm Oct 10 '14 at 12:26
4
Have you considered to create a separate class to hold your application wide methods (eg. AppState.cs)?

thomasmartinsen
- 1,983
- 14
- 21
-
This is a better approach, but I wanted to know how to see the stuff declared in App.xaml.cs. Thanks. – Gus Cavalcanti Oct 22 '09 at 18:56
-
7I'm curious what you had in mind with this suggestion. You still need a place to store it. Where better to store an application-wide method than in your application class? How else would you be able to access it from any window? – Jonathan Wood Jun 22 '16 at 17:00