JasonDavis
JasonDavis

Reputation: 48983

Do WPF application require the use of XAML?

I am wanting to learn C# and it seems everyone is switching from using WinForms to using WPF. WPF applications seems so much more complicated to me because of the use of the .XAML files that are used to building the Forms.

I am just asking before I get really involved, is the XAML files the only way to build WPF applications? Is there an easier method? I know I could just learn to use the WinForms which seems a lot easier since you basically have a Form object that you work with code but like I mentioned I think it would be best to build WPF apps

Upvotes: 5

Views: 3383

Answers (5)

brunnerh
brunnerh

Reputation: 185578

XAML does make things a lot easier if you know how to use it because it is more readable and declarative, but you can do (pretty much) anything in C# code as well if you like.

e.g.

<Border BorderBrush="Red">
    <TextBlock Text="Lorem Ipsum"/>
</Border>

vs.

var border = new Border();
border.BorderBrush = Brushes.Red;
var textBlock = new TextBlock();
textBlock.Text = "Lorem Ipsum";
// The following step is implicit in XAML via the structure
border.Child = textBlock;

Though this can be written more concisely and hierarchically:

new Border
{
    BorderBrush = Brushes.Red,
    Child = new TextBlock
    {
        Text = "Lorem Ipsum"
    }
};

Generally i would always recommend using XAML, reasons include:

  • The parser optimizes the tree construction according to WPF's layout system.
  • Creating DataTemplates in code is not supported. The construction using FrameworkElementFactories has been deprecated in favour of using the XamlParser (and i definitely do not recommend juggling XAML strings in code).
  • Separation between UI and code.

Upvotes: 12

Andy Dent
Andy Dent

Reputation: 17981

Yes, you can code pure WPF but I don't recommend it.

Petzold's book Application = Code + Markup walks you through using pure WPF from code before getting into using XAML. It's readily available second-hand and you can see the sample code there on the website. It's also available online on the Safari ebook site, so you can read for free with a trial account for a couple of weeks. His book is a very serious attempt to teach you WPF without XAML getting in the way as contrasted with the XAML-heavy approach of many other books.

The visual editor in VS2010 was much better than the previous one so you don't necessarily have to understand much XAML to create your interfaces. I also suggest trying the Expression Blend tool to see if you can get use to its "designer approach" to creating the interface.

WPF has a ton of flexibility in where you choose to do things. I've taken a more code-centric approach and use XAML for the layout and styling but do my binding of data and commands in code. That gives a cleaner style of XAML and avoids you having to learn some nuances of how to specify bindings in XAML. For easing the learning curve, I recommend starting with that approach.

I also suggest you start out using the MVVM pattern with a framework such as MVVMLite which will provide a lot of infrastructure for you and help with the separation of GUI and logic.

Upvotes: 3

aqwert
aqwert

Reputation: 10789

No you do not have to but at @H.B. states it makes it easier.

If you use and IDE like Visual Studio, just like WinForms you have a designer plus Expression Blend. If you find XAML overwhelming at first perhaps play with that rather than going straight into XAML.

Upvotes: 2

Muad&#39;Dib
Muad&#39;Dib

Reputation: 29286

While you CAN build everything in codebehind, XAML IS the easy way to build WPF apps. Doing things this way also helps with seperating your view from your program logic. Ideally, a WPF development team would consist of at least one programmer/designer who is in charge, so to speak, of the XAML (or visual side) of things. they would be making animations, and other visual elements. and of at least one "normal" developer who is in charge of the programming logic and data model type things. Of course, this is not an ideal world.

Upvotes: 2

JaredPar
JaredPar

Reputation: 755587

XAML is certainly not the only way to build WPF applications. After being run through a couple of tools XAML itself is translated into C# / IL which is used to actually build the UI. There is nothing stopping you from writing the exact same code and building a WPF form by hand.

I would caution you though to consider not taking this approach. XAML is certainly the tool of choice for building WPF applications. It will be in the vast majority of web samples. There are far fewer samples of hand coded WPF applications.

Upvotes: 5

Related Questions