XAML or C# code-behind

后端 未结 20 1843
盖世英雄少女心
盖世英雄少女心 2020-12-13 02:03

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

相关标签:
20条回答
  • 2020-12-13 02:47

    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.

    0 讨论(0)
  • 2020-12-13 02:48

    All logic should be in code regardless what you programming language that you use in computer programming. XAML should never replace that even in ControlTemplate although it is nice but it is far much easier to unit test and debug code.

    0 讨论(0)
  • 2020-12-13 02:49

    It seems no answers mentioned an important point yet: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh465340.aspx

    XAML is just procedural code (only easier)

    <Grid x:Name="ContentPanel" Margin="12,0,12,0">
        <Button Height="72" Width="160" Content="Click Me" />
    </Grid>
    

    "The following shows how this XAML could be partially replaced by code written in C# or Visual Basic."

    // Initialize the button
    Button myButton = new Button();
    // Set its properties
    myButton.Width = 160;
    myButton.Height = 72;
    myButton.Content = "Click Me";
    // Attach it to the visual tree, specifically as a child of
    // a Grid object (named 'ContentPanel') that already exists. In other words, position
    // the button in the UI.
    ContentPanel.Children.Add(myButton);
    

    If you worked with windows forms for example, you probably remember the windows forms designer generates a .designer.cs file containing code similar to the example from the link above. That kind of declarative code is much better represented in XAML than C#.

    For any non-toy application you should ALWAYS prefer XAML to define UI and connect it to logic in a MVVM manner.

    0 讨论(0)
  • 2020-12-13 02:52

    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)

    0 讨论(0)
  • 2020-12-13 02:54

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

    0 讨论(0)
  • 2020-12-13 02:55

    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 &gt;!

    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.

    0 讨论(0)
提交回复
热议问题