I study WPF and encountered the following:

1) You can write program code directly in XAML.

Is this approach justified in some cases? It is clear that writing all the logic in XAML is not good.

2) As I understood through the XamlReader, you can dynamically create forms based on Xaml files.

Does this method have any limitations? For example, is it possible to change the current form during work by loading another markup and so on?

3) I understand correctly that the x:Name attribute is optional? Ie if I want to work with an element in the code, then I give it a name, and if I don’t, I ignore it?

    1 answer 1

    1) It depends on what you mean by the code. In XAML, it makes sense to write only markup, that is, a declarative description. From the "logic" is that triggers in styles or there are bindings. The code with logic is written C # in the code-behind.

    If we are talking about the x:Code construct, I will quote MSDN :

    Avoid or limit the use of embedded code. In terms of coding architecture and philosophy, providing a separation between markup and dedicated code better separates the roles of the designer and the developer. At a more technical level, code written for inline code can be inconvenient in terms of writing, as the developer always writes XAML in the newly created shared class and can only use the default XML namespace mappings. [...]

    2) Yes, but. At the same time you lose all the code-behind, so you are limited to standard controls. Newer versions of XAML seem to support code inserts, but they are not supported by the WPF framework.

    Well, to change the current form there are other, standard expressive means, without manual reading XAML. For example, podgruzka necessary UI depending on DataContext 'and through DataTemplate .

    3) Yes, x:Name optional. And in most cases, instead of x:Name you can simply write Name .

    • And the binding of event handlers is also done not in the markup, but in the code? - iluxa1810
    • @ iluxa1810: Usually in markup. <Button Click="OnCloseClick">Close</Button> . - VladD
    • Hmm ... even if there are several handlers? Porridge will not occur? - iluxa1810
    • @ iluxa1810: It seems to have not occurred. In any case, quite often many properties will be set in XAML, so one or the other handler is not a big problem. - VladD