Is it possible to program using the WinRT API without using XAML?

12

For some time now, declarative languages have taken over the development of software for creating graphical user interfaces. The most obvious examples are the framework WPF that uses XAML (eXtensible Application Markup Language) as a declarative language and the Qt framework with its Qt Markup Language (QML) .

Unlike Qt that does not publicly expose the code (classes) responsible for the effective execution of the interface, forcing the use of QML in the new GUI style (QtWidgets is the opposite) and does not allow an imperative programming mode instead of declarative (at least it does not allow doing 100% in code), WPF allows two programming modes: 1) declaratively through XAML, which is the most used mode and 2) imperative / object-oriented programming with code across the exposed classes, after all XAML is all built on top of existing classes, even counting with auxiliary classes to convert values from XAML to code. Roughly there is a 1: 1 relationship between classes and their members and the XAML code tags and their attributes.

Although ostensibly demonstrated that XAML should be used primarily - this has several advantages - and even some people believe that it is the only form, documentation, WPF books, and some specific up-to-date articles on the subject (en) (also here and SO original ) clearly indicate the possibility of using only non-declarative programming.

I have software that generates all the on-the-fly screens through code. Adapting this software to Windows 8, it would even be absurd at runtime to generate the XAML for later the framework "convert to code" to run.

I do not know the technology well and I do not know if it would be worth learning for much later to find out if it will not meet my need.

I have looked extensively on the internet in general and on specific websites if this is also possible and found nothing. I could not find anything to indicate that it is possible or even that it is not. I was not able to find an example even showing only code. Surely I found a lot of code that supports WinRT's XAML but nothing that indicates that 100% of the interface can be done without XAML.

My question is whether there is a possibility of using WinRT for GUI building without using any XAML, only with code, and of course, where can I find information / documentation about it, if possible with examples. In other words, is 100% of the code responsible for building and interacting the GUI exposed to the programmer? Some examples here would be very welcome.

The usefulness of XAML is not under discussion. For this specific case would be an exaggeration.

    
asked by anonymous 23.12.2013 / 13:12

2 answers

6

WPF has nothing to do with HTML!

You can use WPF without XAML (XAML actually always translates to C #).

It's as simple as Windows Forms:

Ex: Let's suppose I want to make a page (not to be confused with webpage) for a Windows 8 app with a label and a button, with binding :

using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace App1
{
public class TestPage : Windows.UI.Xaml.Controls.Page
{

    public TestPage()
    {
        var stackPanel = new StackPanel();
        var label = new TextBlock { Text = "Hello World" };
        var button = new Button();
        var buttonCaption = new TextBlock();

        buttonCaption.SetBinding(TextBlock.TextProperty, new Binding { Path = new Windows.UI.Xaml.PropertyPath("Text"), Source = label });
        button.Content = buttonCaption;

        stackPanel.Children.Add(label);
        stackPanel.Children.Add(button);

        var layoutRoot = new Grid
        {
            HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
            VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center,
        };

        layoutRoot.Children.Add(stackPanel);
        Content = layoutRoot;
        }

        }

}

Responding to the questions in the comment:

This is WinRT (that is, Silverlight for Windows Store Apps).

I have no references other than the XAML documentation. Everything XAML does, there is a corresponding class. If you have Grid , StackPanel , Button , etc. in XAML, it will fatally have them in the WPF namespace (whatever version it is). This includes features such as binding and static resources .

There is no "place with authority that affirms ...", but this is intrinsic. C # does not compile or run XAML, it at most interprets and mounts the screen with the objects contained in namespace Windows.UI.Xaml . There is nothing that exists in XAML that does not exist in this namespace (after all, XAML does not exist at all).

You can accomplish any task in this way, although this is very bad. XAML makes writing easy and makes some shortcuts that you have to use to do it manually. An example of this is putting text inside a button. In XAML, it's simple. In code you must explicitly specify that the contents of the button is TextBlock (something like Label ).

You're confusing things a little. XAML is just the name of markup (it never actually runs, is always converted or interpreted). The fact that is called XAML is because MS intended WPF to be used with a markup language aid and there is absolutely no reason not to use it , at least in part. WPF binding is powerful enough to avoid creating components on hand. But of course, .net is a framework that gives you the freedom to do whatever you want.

    
03.02.2014 / 16:50
2

According to the Marcos Zolnowski :

For WinRT it is possible to use HTML instead of XAML. If you only create objects via javascript, I believe this answers your question (without XAML and only via code). But I believe this is not the answer you are looking for, if it is, I will transfer it to the answers section.

I would add that you can dynamically create elements a>:

        // Popula contatos do XElement com informações de Contacts.  
        XElement contacts =
            new XElement("Contacts",
                new XElement("Contact1",
                    new XElement("Name", "Patrick Hines"),
                    new XElement("Phone", "206-555-0144"),
                    new XElement("Address",
                        new XElement("Street1", "123 Main St"),
                        new XElement("City", "Mercer Island"),
                        new XElement("State", "WA"),
                        new XElement("Postal", "68042")
                    )
                ),
                new XElement("Contact2",
                    new XElement("Name", "Yoshi Latime"),
                    new XElement("Phone", "503-555-6874"),
                    new XElement("Address",
                        new XElement("Street1", "City Center Plaza 516 Main St."),
                        new XElement("City", "Elgin"),
                        new XElement("State", "OR"),
                        new XElement("Postal", "97827")
                    )
                )
            );

        // Cria o primeiro controle TextBlock.
        // Note que o elemento precisa declarar dois namespaces XAML.
        XElement textBlock1 = XElement.Parse(
                @"<TextBlock 
    xmlns='http://schemas.microsoft.com/client/2007' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' 
    TextWrapping= 'Wrap'
    Width = '400'
    Canvas.Top = '10'
    Text=''/>");

        // Pega o primeiro elemento filho da árvore de contatos do xml.
        XElement contact1 = contacts.Element("Contact1");

        // Ajusta o valor do último atributo "Text"
        // com o conteúdo dos contatos da árvore do xml.
        textBlock1.LastAttribute.SetValue(contact1.ToString());


        // Pega o segundo elemento filho da árvore de contatos do xml.
        XElement contact2 = contacts.Element("Contact2");

        // Cria o segundo controle TextBlock.
        // Note que o elemento precisa declarar dois namespaces XAML.
        XNamespace xmlns = "http://schemas.microsoft.com/client/2007";
        XElement textBlock2 = new XElement(xmlns + "TextBlock",
            new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
            new XAttribute("Canvas.Top", 250),
            new XAttribute("Width", "600"),
            new XAttribute("Text", contact2.ToString())
            );


        // Adiciona o controle TextBlock à página
        LayoutRoot.Children.Add(XamlReader.Load(textBlock1.ToString()) as UIElement);
        // Adiciona o controle TextBlock à página
        LayoutRoot.Children.Add(XamlReader.Load(textBlock2.ToString()) as UIElement);
    
29.01.2014 / 22:46