49

I've done .Net development for awhile but I'm new to the WPF technology. What is the supposed purpose of App.xaml? Also, what type of xaml code do you usually put in it? It seems like for simple applications it could be ignored and left untouched. Is this true?

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101

8 Answers8

37

For simple applications, it is true, it can be ignored. The major purpose for App.xaml is for holding resources (style, pens, brushes, etc.) that would would like to be available through out all of the windows in your application.

Jason
  • 1,879
  • 16
  • 19
  • 5
    This answer is much better then the accepted one if you ask me. Some more examples, would be even better. +1 :) – Jo Smo Nov 29 '14 at 21:11
37

App.xaml is the declarative portion of your code (usually generated by Visual Studio) extending System.Windows.Application. For example, Expression Blend can use App.xaml to share a Resource Dictionary or a design-time data set with your entire application. And, because we are using Microsoft products, whatever Expression Blend can do auto-magically, we can do by hand in Visual Studio.

Now the tangent: To me, to ask about the purpose of App.xaml is to ask about the purpose for System.Windows.Application. Feel free to accuse me of changing the original question (let the digital brutality ensue).

You can’t just open a System.Windows.Controls.Window in any Assembly you like… Chris Sells is likely telling me this in his book. I began to understand the purpose of System.Windows.Application while using MEF and MVVM Light to display WPF windows in DLLs (not EXEs). I got errors like this:

The type 'System.Windows.Markup.IComponentConnector' is defined in an assembly that is not referenced.

or

The type 'System.Windows.Markup.IQueryAmbient' is defined in an assembly that is not referenced.

The above error is simply saying that I’m trying to open a WPF Window inside of a DLL and not an EXE. Then, there’s this error:

The component 'Songhay.Wpf.WordWalkingStick.Views.ClientView' does not have a resource identified by the URI '/Songhay.Wpf.WordWalkingStick;component/views/clientview.xaml'.

This boils down to the absence of a facility that associates WPF Window XAML with the WPF “code” (an instance). This facility is associated with WPF EXEs and not WPF DLLs. Visual Studio auto-generates a WPF EXE class called App.g.cs (in your \obj\Debug folder) with this call in it: System.Windows.Application.LoadComponent(this, resourceLocater) where resourceLocater is a badly named variable containing a System.Uri pointing to the XAML like ClientView.xaml mentioned above.

I’m sure Chris Sells has a whole chapter written on how WPF depends on System.Windows.Application for its very life. It is my loss (quite literally of time) for not having read about it.

I have shown myself a little something with this unit test:

[STAThread]
[TestMethod]
public void ShouldOpenWindow()
{
    Application app = new Application();
    app.Run(new Window());
}

Failing to wrap a new Window in the System.Windows.Application.Run() method will throw an error from the land of COM talking about, “Why did you pull the rug from underneath me?”

Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
rasx
  • 5,288
  • 2
  • 45
  • 60
  • 5
    That was a great explanation. – Pow-Ian Oct 19 '12 at 14:00
  • I don't know what you does in your MEF project but in my current Company i'm currently developing a Shell to do exactly THIS. Load modules from more or less "unknown" Assemblys and executing Windows and such on. If your Interested i can share my knowledge about MEF visual modules – Venson Aug 04 '13 at 09:58
  • I don't quite follow your tangent. The missing references are just that, missing references. And you can run a standalone app without an instance of Application. From a note on [MSDN](https://msdn.microsoft.com/en-us/library/system.windows.application(v=vs.110).aspx): >A standalone application does not require an Application object; – Onots Jan 15 '16 at 05:09
19

It is true. App.Xaml is some sort of central starting point. You CAN use it, or you CAN start your first window (it is defined in the app.xaml) manually. There are some lifetime events there centralls (like application start).

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • So... for what is it used then? An example would be nice. – Jo Smo Nov 29 '14 at 21:06
  • So, in ASP.NET terms, you're saying it's sort of like a MasterPage for your windows forms (which can also contain globally used functions)? – Jacob Mar 26 '15 at 14:07
11

Storing resources that are used across the whole application.

Application is the root of the logical tree.

Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53
4
App.xaml is a major part of wpf application.
It contains major four attributes
1.X:Class->used to connect you xaml and code-behind file(xaml.cs).
2.xmlns->To resolve wpf elements like canvas,stack panel(default one).
3.xmlns:x->To resolve XAML language definition.
4. StartupUri->To give start window when application is launching.
4

It is like Global.asax if you are coming from an ASP.NET background. You can also use it to share resources throughout your application. Comes in pretty handy for resource sharing.

Rahul Soni
  • 4,941
  • 3
  • 35
  • 58
0

++++++++

App.xaml is the declarative starting point of your application. Visual Studio will automatically create it for you when you start a new WPF application, including a Code-behind file called App.xaml.cs. They work much like for a Window, where the two files are partial classes, working together to allow you to work in both markup (XAML) and Code-behind.

App.xaml.cs extends the Application class, which is a central class in a WPF Windows application. .NET will go to this class for starting instructions and then start the desired Window or Page from there. This is also the place to subscribe to important application events, like application start, unhandled exceptions and so on.

One of the most commonly used features of the App.xaml file is to define global resources that may be used and accessed from all over an application, for instance global styles.

+++++++++ Source : http://www.wpf-tutorial.com/wpf-application/working-with-app-xaml/

Ron16
  • 445
  • 6
  • 11
0

Here is an updated answer in case people are still looking.

There is this excellent article on WPF, and the link specifically puts you at the App.Xaml point to begin teaching you the things you can do with it.

WPF is easy for the first very simple app or two. However, due to the increased flexibility of the framework, you need these types of tutorials to help you understand what can be done from where (in the various application files).

https://www.wpf-tutorial.com/wpf-application/working-with-app-xaml/

Good luck.

Major Tom
  • 31
  • 3