9

First off, please note that this question is not about WPF vs. WinForms.

What are the highest-ranking benefits that led Microsoft to invent XAML in favour of the “old” approach of generating compilable C# code?

My impression is that other developers seem to find a lot of downsides to it, but Microsoft went with it regardless, which must mean that there is a benefit that I am not seeing. I would like you to explain to me what it is that I am missing.

6
  • 4
    Isn't this a duplicate?: programmers.stackexchange.com/questions/139382/…
    – user16764
    Commented Mar 12, 2012 at 20:23
  • No, because that one was closed for reasons outlined here, while this one is explicitly written to be in line with the FAQ and guidelines.
    – Timwi
    Commented Mar 12, 2012 at 20:26
  • @RobertHarvey I take it back, it’s actually different; I wanted to ask how XML-based syntax is better than a special-purpose language, rather than "normal source code" like C#. Commented Mar 12, 2012 at 20:59
  • the benefits for you or for the language/tool implementers?
    – jk.
    Commented Mar 12, 2012 at 22:19
  • 2
    @Timwi I don't know about that. I know several people who seem compelled to buy Microsoft products regardless of how much it will benefit them. Commented Mar 13, 2012 at 14:07

7 Answers 7

7

The biggest benefit of XAML is that it allows you to describe your UI in an declarative way. That is, you don't so much say "create a box, now resize it", you say "create a box and btw, it's this size"

The second place where XAML shines is that it really takes advantage of bindings. For instance:

<Button IsEnabled="{Binding IsBusy}">

In your view model you can go about your business and set IsBusy to true or false, and as it changes, the Button's enabled state automatically switches back and forth.

So as a more complex example:

<ListBox ItemsSource={Binding Options}>
 <ListBox.ItemsTemplate>
  <CheckBox Checked="{Binding IsChecked}" Contents="{Binding Name}"/>
 </ListBox.ItemsTemplate>
</ListBox>

In this example we're binding to a list (Options) and creating an item in a list box for every item. The cool thing here is that as we add items to the list, or remove them the UI is automatically synced to the state of our list. As we click the checkbox on each item, the underlying objects are automatically updated to match the UI. With powerful tools like this I've hacked out UIs in a fraction of the time it would take me to wire up all the events in a WinForms app.

In general this all means that you can write your UI in a declarative format and this is a massive productivity gain. Although, I must mention that this does at a cost. You will probably get more performance out of a WinForms app as the code will be less generalized. But unless you're doing something like showing 40,000 rows in a data grid, you probably won't notice a big slowdown using XAML.

It should also be mentioned that other UI libs accomplish this same goal without XML. For instance, QT uses Json (http://labs.qt.nokia.com/2009/05/13/qt-declarative-ui/)

3
  • 4
    All the benefits you mention are equally applicable to a carefully engineered domain-specific language that isn’t derived from XML. Commented Mar 12, 2012 at 20:52
  • 1
    And hence the last line of my answer and the example pointing to Qt. Commented Mar 12, 2012 at 20:56
  • There is indeed a long history of UI meta-languages. n.b. the Motif UIL and also David Flannigan's pre-Java work on his Xmt Library.
    – sdg
    Commented Mar 13, 2012 at 14:40
6

The 8 Benefits of XAML for UI and Beyond

  • XAML Describes Data in a Concise, but Human and Machine Comprehensible, Way
  • XAML is Useful for Many Types of Data
  • XAML uses Typed Object Models for Better Programming and Validation
  • XAML is Extensible and Version Tolerant
  • XAML is Toolable
  • XAML is XML (it benefits from the availability of off-the-shelf tools, it's human-readable, etc.)
  • XAML Enables Event Driven Programming Models
  • XAML can be Compiled or Interpreted

I view XAML as an "intermediate language" of UI; you can use it with any programming language, and on different platforms. It enables language, platform and device-independent description of the user interface.

8
  • 1
    In your opinion, are those 8 benefits valid and/or accurate? Commented Mar 12, 2012 at 20:49
  • 5
    Benefit #9 - XAML Enhances Your Resume
    – jfrankcarr
    Commented Mar 12, 2012 at 20:49
  • 3
    @SnOrfus: More or less. Concise is arguable, for example. :P Commented Mar 12, 2012 at 20:51
  • 1
    "XAML is XML" is also arguable. XML places unusually strict limitations on comments, for example, which are a useful tool for temporarily removing some code. Commented Mar 12, 2012 at 21:01
  • 3
    A lot of these fall under "XAML is XML". I would answer @SnOrfus "yes, in a salesman's power-point presentation sort of way".
    – psr
    Commented Mar 12, 2012 at 21:01
3

Using XML gives you meta-data in a format that a huge number of parsers and parsing tools support. At least in theory this allows anyone to meta-program against it using any language.

More practically, XML lets you seem like a more open standard. You can argue it's just markup like a browser, but easier to parse.

These are real benefits, but in practice there is too much going on outside the XAML for the benefits to be terribly important.

2
  • 1
    Why does this argument apply to XAML, but not to a programming language? "<While Condition="a == true"><Print>Hi!</Print></While>" anyone? That is obviously atrocious; why do you reckon UI representation is different? Commented Mar 12, 2012 at 21:03
  • @romkyns - I wasn't arguing for XAML, the question asked whether there were any benefits (my interpretation). As for why I think UI is different - probably UI layout is simpler to represent than arbitrary code so the XML is less atrocious than it would be for, say, XC#(the XML syntax for C# that we must pray never exists). In fact I'm rather dubious about real world benefits of XAML over either .NET code with reasonable API for meta coding or a custom language with published syntax. (Neither of which lets MS put an "XML-driven" sticker on the box.)
    – psr
    Commented Mar 12, 2012 at 21:27
1

XAML was designed to simply represent a hierarchy of objects and not only for GUI such as XPS and WF. Using XAML to build applications would help you deliver Rich Internet Applications (RIA) as well as rich desktop applications.

The use of XAML as a text based declarative representation of GUI brings the MS development tools a step closer to other technologies considered as standard in the industry such as HTML.

Some of the main features of XAML (over Windows Forms) are:

  1. XAML allows for drawing Scalable Vector Graphics (SVG) Images and transformations. One of the benefits of vector graphics is that they can scale to any size without losing quality so you don't worry much about the resolution of the user's computer.

  2. Windows forms, in general, suffered from limited support for automatic placement of controls as a result of windows re-size and changes in the GUI surface (as when new controls are added or removed). Controls are positioned by explicit specification of row and column values for each control through the GUI or program code. While XAML allows for this behavior, it introduces a more easy way to solve this problem by providing he Stack Control control and the Wrap Panel controls.

  3. In XAML you can construct new controls from existing ones directly in the XAML using nesting. You can nest an image control within the button control together with a Stack Panel to create a new type of button, that is, a button with a picture. Since this control is created in XAML, you don't need to create a separate dll to use it like as the case is in Windows forms. Example:

    <Button Width="90" Height="40">
        <StackPanel Orientation="Horizontal">
          <Image Source="Yes.png" Width="14" Height="14" />
          <TextBlock Margin="5,1,0,0" Text="Yes" />
        </StackPanel>
    

  4. Styling and templates allows you to customize the looks of controls on several levels similar to the capabilities of CSS and HTML. No procedural programming required. One change can be reflected on the entire GUI.

  5. XAML allows advanced data binding techniques not available in Windows forms. You can even bind one control to anther's data value(s).

  6. XAML provides a built in Navigation framework that you can use or replace with other frameworks. In fact WPF, Silverlight and XAML are designed to take advantage of MVVM pattern that you can't exactly achieve with Windows Forms.

  7. With the introduction of XAML, the code behind files could be isolated to a greater degeree from the interface design files. Of course the dependency of some names still exist. However, the fact that XAML is text based declarative language similar to XML and HTML that does not require a compiler, allows interface designers to create separate XAML files to be later integrated with the code behind files. This is unlike the case with windows forms where the GUI is tightly coupled as procedural code in the project. Designers using XAML could even leave Visual Studio and use the MS Expression Blend tool to create their GUI. MS Expression Blend is created for designers rather than programmers.

  8. XAML provides advanced native techniques for animation and video display without the need for 3rd party tools.

  9. XAML was intended to make the development of Windows-Based software and Web Pages more close than it was using ASP.NET nad Windows Forms. Silverlight and WPF both use XAML (although there are some differences, but large part of the experience is transferable).

  10. An SDK for XAML includes interesting controls and it allows you, among other features, to support wizards/storyboard style presentation without using a 3rd party tool.

4
  • You’ve obviously put quite a lot of effort into this, but you’ve misunderstood one important point: you’re discussing the advantages of WPF, not XAML. XAML is but a single representation of WPF UIs, one that happens to be XML-based. Commented Mar 12, 2012 at 23:06
  • @romkyns, thanks for your comment. The question title is: "explain the benefit(s) of using an XML-based syntax (e.g. XAML) instead of normal source code (e.g. WinForms)?" - No mention of WPF even in the body.
    – NoChance
    Commented Mar 13, 2012 at 8:20
  • That’s right, and yet your answer still discusses WPF (but calls it XAML, incorrectly). Do you see what I mean now? XAML does not provide a built-in navigation framework; WPF does. Styling is a feature of WPF, not XAML. Etc. Commented Mar 13, 2012 at 10:37
  • @romkyns, thanks for the clarification, I see your point. Your suggestion that XAML itself does is totally independent from this features is new to me and I kind of doubt it. I may get a chance to research it more. I appreciate pointing this out.
    – NoChance
    Commented Mar 13, 2012 at 11:13
1

XAML Describes Data in a Concise, but Human and Machine Comprehensible, Way

In theory every new language is something you need to learn. Quite many people would prefer to speak many / multiple languages - as it's considered as language richness. This exists and it's related to natural languages, but not necessary to programming languages.

I by myself have programming in quite many languages including batch files, perl, lua, python, c++ and c#. Do I remember all of them ? No, I can switch using another language, but it takes time. Just writing now in C#, then switch to C++, you can easily mix up is it NULL, null, nil or nullptr depending on programming language and it's "dialect". (c++ normal, c++ managed)

xaml is xml "dialect". Is it easy to learn ? based on quite many question on other forums I would say no.

Does it replace C# ? No.

Answer is quite obvious - I would prefer to have one programming language, which is well designed (C# - e.g. data type reflection), has a good peformance (like C++) and scriptable (C#-script, C++/cling or even better)

Currently C++ and C# don't fully satisfy my needs, but xaml is some odd data format, which I would not consider as worth of learning (only wrapping around, and hiding it from developer's eyes)

At the end I would like to recombine natural language (spoken) with programming language (artificially constructed language) into same one language - so it can be used for communication and by programmers - this is later on into future, when programming language "architecture" or "design patterns" will start to be documented.

  1. XAML allows for drawing Scalable Vector Graphics (SVG) Images and transformations.
  2. ...

XAML indeed introduces some new improved features over winforms, so it's worth of considering to use it. But eventually if you want you user interface to become fully dynamic - xaml needs to be replaced by similar run-time code.

Eventually user interface should be simple to use - and that means minimum set of controls, controls looks similar ( cancel, ok , shortcuts, same width / height and so on ) which means that UI can be more or less auto-generated.

Btw - is it possible to embedd into xaml C# code directly ? Meta data, string, comments ?

It's sometimes neat to misuse or abuse language in way noone imagined it to be used. Perl does this a lot, but I prefer somehow more clean syntax rather than cryptic one. But C# versus xaml - I would chose C#.

0

A benefit is that the XAML can be used in other applications. A good example is that the XAML can be developed in Blend by a designer/artist while the internals (code behind) get coded in visual studio.

I beleive it was Microsoft's attempt at separate UI markup versus code behind and to make it possible for the UI to be coded by non-programmers using a tool like Blend.

But, many shops don't have Blend so for many of us it looks like a re-invention of the wheel.

Here's are 8 points bought out by someone closer to the windows client:

8 XAML Benefits

5
  • 1
    The author of your link compares XAML with a pretty verbose general purpose programming language. Maybe XAML is more compact than C# or Java. That's a point against C# and Java, not really a point in favor of XAML. The equivalent Ruby code would be just as compact, with the full power of Ruby available if needed. Commented Mar 12, 2012 at 21:45
  • That's a benefit of the Blend tool, not XAML? Blend could have existed as a designer-oriented GUI that created code behind the scenes?
    – MarkJ
    Commented Mar 12, 2012 at 21:51
  • 1
    It really does look like a reinvention of the wheel to those of us using Delphi. It's had a simple, code-independent DSL for form layout since the very beginning (1995), and without all the ugly, bulky baggage that XML carries with it. Commented Mar 13, 2012 at 0:01
  • @MarkJ - Blend is specifically geared toward designers, prior to that it was just drag/drop in Visual Studio. Prior to WPF, windows forms had limited built in functionality for animation and GUI bling. Blend could have existed prior, but the designer would need to be familar with C# or .net (.cs) code files. XAML you can markup which is something a designer is already familiar with (HTML, XML, etc.)
    – Jon Raynor
    Commented Mar 13, 2012 at 12:58
  • I'm suggesting Blend could have been a designer-oriented GUI that created C# code files. The designer need not have been familiar with the structure of a C# code file, just like they don't have to understand the internal binary structure of a JPG or PNG in order to create images in Photoshop.
    – MarkJ
    Commented Mar 13, 2012 at 15:07
-1

One point I dont see in alot of answers is that xaml encurages the core of wpf desgin principals. Seperate the visual representation and visual logic (in pratice this is limited to triggers and such but assuming no custom controls this is often all you need) from the business logic. This also couples well as others have noted with a expresive syntax for binding, attached propertys and other meta data which can be more verbose in normal C# code.

On a more personal opinion note, one thing I find nice about xaml is it beter represents the visual hierarchy of controls than just generating code. At a glance its easy to see how one control is related to the others.

Not the answer you're looking for? Browse other questions tagged or ask your own question.