3

In WPF applications all the views are inherited from System.Windows.Window and have an associated xaml and codebehind file. That seems logical.

However I'm confused that why does the App file, inherited from System.Windows.Application, have a xaml file? Although it is an application and not a view (It is not visible)? I know that this file is usually used to define application resources, etc, and xaml provides an efficient way of defining them. But that can also be done programatically. Then what benefit did the designers of wpf achieve by having both the xaml and code behind files for "App"? Wouldn't one of them have been enough?

Hasan Fahim
  • 3,875
  • 1
  • 30
  • 51
  • Possible duplicate of [this question](http://stackoverflow.com/q/4146268/438180). – kyrylomyr Jun 10 '11 at 13:40
  • @archer. That question is different. In that question it is being asked that what kind of code needs to be in application file. Where as I am not asking that. You can re-read the question. I know what things are usually defined in it. But my point is that, if this is not a view (is not visible to user), then why does it have a xaml file? Wasn't it enough to just have a cs file? – Hasan Fahim Jun 10 '11 at 13:59

2 Answers2

5

However I'm confused that why does the App file, inherited from System.Windows.Application, have a xaml file? Although it is an application and not a view (It is not visible)?

Remember that XAML is not a UI language, but a general declarative language. While it's true that it's mostly used to represent UI for WPF or SilverLigth, it's also used to declare graph of objects in other non-UI technology.

The first example that comes into my mind is the Workflow (the XOML is a derivate of the XAML), SharePoint also use XAML in some hidden parts, and I've seen in a customer project with use XAML as a meta-language for generating web-apps (and yes, it actually outputs HTML).

Then, to answer to your question, the application have both files (and it is not actually a requirement) because you can :

  • declare some objects (in the xaml)
  • override the behavior of the application (by overriding appropriate methods)
Steve B
  • 36,818
  • 21
  • 101
  • 174
1

Designers can specify resources for entire application without entering any code and use it in any Window. Its something like a root for all windows. For example, if you use one style for every TextBox (or any other control) in every window, you can specify it in App.xaml and bind anywhere without duplicating.

kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
  • That I know. But my question was that, why are there two files, rather than just one. Probably it's for the convenience of developers, so that they can implement their logic in which ever language (xaml or cs) they are more comfortable in. – Hasan Fahim Jun 10 '11 at 14:05
  • XAML-file - for designers, who don't know anything about C#, CS-file - for coders. Whats wrong? How do you think they can be merged? – kyrylomyr Jun 10 '11 at 14:09