views:

3190

answers:

17

I don't like to use XAML. I prefer to code everything in C#, but I think that I am doing things wrong.

In which cases it is better to use XAML and when do you use C#? What is your experience?

+1  A: 

I originally came from the Web side of development and am learning WPF/Silverlight at the moment. For me, the XAML model makes much more sense to me than WinForms ever did. I treat the XAML as if it were HTML and the .cs files just like a code-behind.

Rob Allen
+36  A: 

Creating an entire window in C# can be a mess of code. The best thing about WPF is that XAML allows you to separate your design from your logic, making for much easier-to-read code.

I'll use C# when I need to create dynamic controls, but I tend to keep my general design, static storyboards, styles, datatemplates, etc. in XAML.

Will Eddins
Agreed. The beauty of XAML is that it can live outside of compile-time. In other words, it is more like HTML. It becomes very easy to read your XAML from a file in a "Themes" folder, and tweak the UI like a config file. There are some amazing use cases for this. You could never do that with C# (not to mention the security risk it would create)
Brian Genisio
The down side is that you have to learn essentially two different languages for describing the same thing. I think it's unfortunate that the WPF team decided to use XML for the gui description language. They could have gone with a syntax a lot closer to C#, and thereby made it easier to learn, easier to type, and easier on the eyes. I'm thinking something like JSON, only it would be C#ON. There really isn't a hard line between code and data. Lisp guys understood this in the 70's. Unfortunately XML proponents still don't seem to get it today.
Baxissimo
@Baxissimo: Wasn't the point of using XML to enable non-programmers to design interfaces?
Phil
@Phil - Why would non-programmers be more familiar with XML? There's a lot more angle brackets than JSON has :-)
Robert Fraser
@Baxissimo: Because it's easier for humans to read and write than a programming language? Although I think the idea was for them to use blend and then port the interface over to a developer with XML.
Phil
@Phil, you've got to be kidding? XML is harder to read and write than 99% of programming languages. Don't agree, try giving a 2 page C program and a 2 page XML file to a non-programmer and ask them which is simpler.
Ash
@Ash: I think rather than asking which is 'simpler' a better test would be to ask them to interpret each. How many non programmers do you think will correctly interpret a 2 page C program compared to a 2 page XML file? It takes training and practise to understand a programming language but XML is just labelled data.
Phil
@Phil, when it comes to interpretation, comparing your average declarative XML file with imperative C file is not a valid comparison. A more valid comparison would be between an XSLT file and a C file, and that is even more in favour of the C file. In general, my opinion is that XML is highly overrated and has commonly been applied where it there are far better options (Ant, WPF etc).
Ash
@Ash: Is it still invalid with regards to XAML, which was the original point of this conversation?
Phil
There is nothing preventing you from separating design and logic in C#. Why does design have to use an entirely foreign syntax from logic?
Luke
@Luke: It doesn't necessarily, but it's helpful that it's in a markup language rather than requiring you to learn programming. A design team I worked with had no idea how to use C#, or how to do even simple programming for that matter, but were very good at designing WPF windows and controls through XAML and Expression Blend.
Will Eddins
+10  A: 

My experience is that some things are far quicker to do in C#, while most are quicker to do in XAML. When it takes 5 lines of C# code to do what a single line of XAML code can do, it's pretty easy for me to choose which is better.

John Fisher
Can you provide an example?
Luke
There are many. Try writing a simple app with some actual behavior, without XAML. You'll see rather quickly where XAML would have been much faster to churn out.
John Fisher
A: 

Some things are easier to maintain or debug in code.

eschneider
I think I need an example. What is easier to debug in code that can be done in XAML?
Brian Genisio
+22  A: 

Check out this video on MVVM in WPF. If you want wrap your head around how to organize a WPF application vis-a-vis what goes in XAML, code behind and other abstractions, this is a great place to start.

JP Alioto
+1 on MVVM. Seriously. It changes the way you write WPF/Silverlight code, and makes your code really testable.
Brian Genisio
I was also going to suggest this video.
emddudley
+1 for MVVM as well - testability is amazing.
Andrew Dunkman
+5  A: 

The urge to want to write your UIs in C# instead of XAML is really just a manifestation of how comfortable you are in XAML.

For me, is is a personal goal to write as little code-behind as possible. Quite simply put, code behind is hard to unit-test, but can (and usually does) include logic that doesn't get tested. XAML is declarative (like HTML) and doesn't include any logic, so there is nothing to unit-test. I keep my view code in XAML and I keep my view logic in my ViewModel (MVVM) which is VERY easy to test.

Once you become more comfortable with XAML, the more you will realize its benefits over view construction in procedural code... Using a pattern like MVVM, you take it a step further, and realize that code-behind is only useful in rare cases.

Brian Genisio
my vote is for you Brian such stuff is really based on personal interest.
Usman Masood
+1  A: 

It's not just about you, it's also about your team, some of whom may be designers.

HTH, Kent

Kent Boogaart
A: 

Not to Mention that you will be able to do more in Xaml2009 that has to be done in code behind now.

Unfortantly BAML won't fully support xaml 2009 in vs 2010 time frame so you can't compile xaml in 2010 time frame. And will have to wait for a later version of blend to do the full dev design loop. (later than 3)

Douglas

DouglasH
+3  A: 

The most important thing to bear in mind is that XAML is for presentation. All your presentation should be in the XAML. If you have logic, you keep that out of your XAML - in your C#.

Imagine swapping out your XAML file with one that LOOKS completely different - but still uses the same data - that's where the division should be.

Sohnee
+6  A: 

You can certainly go too far with XAML. Those who want their entire user interface (including logic, event handling relationships, etc) defined in XAML are probably missing the point.

The aim of XAML is to provide a common format for determining how things should look. It should just be a description of how to lay things out, how to color and style them visually.

There is really very little point in trying to use it as a replacement for other aspects of C#, because C# has a permanent head-start in terms of programming features - reuse (defining types and functions), referring to variables, procedural programming, and even declarative or functional styles.

Personally I really like throwing together a UI with a Linq expression!

The ultimate absurdity was reached by a sample I saw where they used workflow actions as the children of a button to supply the Click handler, so the whole program was in XAML. It sounds "cool", but the problem was that it was significantly more ugly and unreadable than the equivalent C# or VB.NET program, and so everything that is ready to use in C# has to be replaced by a more verbose, flaky equivalent. Nothing has actually been gained by this translation to an uglier syntax - it's the same program only more hideous. XML is a poor basis for the syntax of a general programming language. Start with the fact that the greater-than symbol has to be written as >!

In a parallel universe, Microsoft released C# 3.0 before they finished XAML. The XAML team adopted C# 3.0 object/list initializer syntax instead of XML as their syntax. And this whole debate never happened.

Daniel Earwicker
I disagree.XAML is a great way to define tree hierarchies as they exist extensively in UIs. Doing so with C# would just get really really messy in terms of indentation. XAML by definition is only a way to serialize .NET Objects to XML and backwards, so anything you do in XAML can be done with c# and object initializers. It's just not pretty and a lot of work.
Tigraine
You've grasped that the two are equivalently powerful, so you don't entirely disagree. What I think you're missing is that there is no need to have a second syntax, so why have one? To understand this, consider how JSON is often preferred over XML these days. C# initialiser syntax is like JSON in C#. Exactly the same tree can be represented in either, with exactly the same amount of indentation. There are things in WPF (e.g. setting up Grid) where this can't be done in a single expression tree, but that could have been done differently (and can be worked around with simple wrapper functions).
Daniel Earwicker
@Tigraine - You seem to imply that XAML isn't really really messy. I disagree. I find XAML to be a mess to look at and work with. At least with code I can step through the debugger, and don't have to wade through a swamp of angle brackets.
Baxissimo
+3  A: 
Danny Varod
+1  A: 

I love clean code, the less code I have, the cleaner it is. Code can be written in millions of ways, XAML is more restrictive. XAML is good at cutting down code, when appropriate of course. If I can put something in XAML, I'll do it. Tools can read XAML and do stuff with it. Much less with code. Tools and frameworks handling XAML will evolve and get better, doing more and better work with existing XAML. Can't say that about code. The more XAML evolves, the more we'll be able to declaratively define our applications.

For me, using XAML is like using LINQ. If I can write a one line statement that is easy to read and let a framework decide the best way to implement what I want, I feel good. I definitely try, as much as I can, to use declarations of what I want instead of hardcoding how I want it to be done. F# is another good example of this paradigm.

devMomentum
+4  A: 

One of the nice things about XAML is the separation of presentation an logic. This separation is not just theoretical but also practical. I my case, most of the my UIs are now being handled by a designer. This designer uses blend and does not know C#, has no interest of learnning c#, and frankly should not need to. This designer is a true designer, an artist, that knows how to use those tools to make things look really really nice. Basically my phylosipy is this, the more I use XAML, the less work I have to do on the UI because he can do it. This has worked well for us. I usually design my control to be lookless, give them a basic no frill look, use the DataContext to bind my object(btw DataTriggers are a great way to cut down on code). As a result, I will often checkin my code, come back the next day, synch it, and the UI will look completely different, but everything still work!!!

Of course, it took at least 1/2 year to get there, but now this model seem to work and our UI look Kick A##, our application earns high praise, and I do little work on the UI itself and get to work on cooler things. To make a long story short, I think the code behind might be a bit more developer centric and forgets a whole other group that can benefit, trive, and make a living using WPF, namely the designers.

Of course, there are still times when it takes a devloper to make XAML/WPF sing and dance, and sometime we need to educate the designers about the right way to do things, but I think its worth the investement pays off many times over in large scale projects (maybe not so in short one0

jm Ducasse
What if Blend spat out C# rather than XAML? Would it really matter to your designer? Is there some reason Blend couldn't do this?
Luke
@Myself I suppose it would be non-optimal to have designers spit out every different .Net language.
Luke
A: 

What are you expecting with this bounty? In my humble opinion, here are the best answer you can get for that question.

If you want more opinions...

XAML is clean, quick and more flexible. If you want to do stuff like bindings, styles, triggers, commands, etc etc, is more easy and fast in XAML. But yes, the more important thing is to split the view from the rest of the app.

Jesus Rodriguez
A: 

XAML can be seen as being similar to a combination of XHTML and CSS or XML and XSL which are used for structure and design. Any form of logic should be in C#. This way structure and design are seperated from logic. Your code should be cleaner too by using this approach. Another positive thing is that tasks can be easier to separate between designers and programmers.

One more thing... this is the definition of XAML from MSDN:

Extensible Application Markup Language (XAML) is a markup language for declarative application programming. Windows Presentation Foundation (WPF) implements a Extensible Application Markup Language (XAML) loader and provides Extensible Application Markup Language (XAML) language support for Windows Presentation Foundation (WPF) types such that you can create the majority of your application UI in Extensible Application Markup Language (XAML) markup. In addition, the SDK includes a Extensible Application Markup Language (XAML) editing tool called XAMLPad. You can use this tool to experiment with Extensible Application Markup Language (XAML) in real time.

Link to quote.

Partial
A: 

XAML and C# is a really good way to separate the logic from design as already described.

For a typical programmer the way of programming is really changed by using WPF, assuming that the programmer comes from a C++, VB, WinForms, ATL and MFC background, a background from were the UI was not as natural separated from the logic as with XAML and C#.

To get used to this way of programming it takes some time, but getting more and more experiance it gets really effective.

Before starting it is really good to learn the MVVM pattern and to run tutorials to understand the strength of the pattern, and also to get to understand its benefits.

WPF and C# applications based on MVVM pattern benefits:

1. User Experiance and Usability Separating the logic from UI, makes it more natural to have a dedicated designer working on the UI design and animations. In that way the programmer can focus on the logic behind and the technical solution while the UI is designed by someone that knows design. That has been a problem for many software companies, atleast in the industry that the programmers actualy are the once that are designing the UI as well and that has resulted in a lot of support, maintance and in effecive applications.

There is a higher probability that it ends up with more user friendly applications by have a person with Usability background focusing on the usage instead of the technical solution. There is really interesting book abbout such examples, User Interface Design for Programmers, by Joel Spolsky.

By using the MVVM pattern for XAML application there is a good chance we will see more user firendly applications.

2. Maintanence Maintenance, which is cost a lot within software development. It can feel that the MVVM pattern is a big overhead in the begining but while functions are added and the more complex and advanced the application is getting the more benefitial it is. You will see that it is really easy to maintaine such an application. For an overview you can look into this video:

3. Mix of competences Dedicated designer and a dedicated programmer working in a team to achieve a better result is a very good way of mixing competences. Instead of only hiring programmers organizations need to combine the competences to provide the best results.

4. Opportunity for design interested programmers Finally it is possible to implement fancy applications in the Windows environment. If you are a programmer that are interested in design the Microsoft Expression Blend really opens up possiblities to learn and achive fancy, useful applications with a nice design.

There can though be risk using with XAML and C#, MVVM or not, the great possibilities and flexibility it provides can also be a drawback. Letting the programmer loose with this new easy UI environment, the applications may end up with a wide spectra of animations, colors, everything this new environment provides. Remebering how you added UI controls in the C++ and ATL envrionment.

Still the benefits are more and I hope you get some inspiration to use XAML instead of C# for the UI, when getting used to it I am convinced you will like it.

A link to a good tuturial: Tutorial MVVM XAML C#

Sun
A: 

XAML , MXML all these stuff works as long as you are developing a simple UI with some average complexity . Once your UI gets complex and richer , the automatic data bindings will cause more trouble than their benefits . The purpose of XAML is not make programming easy , it is to seperate UI from logic so that tooling of UI can be easy ..

No Data bindings , No Event handlers .. assume that you will never see your XAML again and write code ..

XAML is presentation . Data binding is not presentation . its presentation logic. put all presentation logic in code behind (data binding and handlers). Developers own code behind , Designers own XAML . if you are a developer and if you are touching XAML , then move that part in to code behind .

we can write WPF apps without XAML too ..

Thanks Vinoth Kumar R (Had gone thru enough using Flex MXML data binding)

vinoth