XAML or C# code-behind

后端 未结 20 1905
盖世英雄少女心
盖世英雄少女心 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: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)

    
        

    "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.

提交回复
热议问题