Is it possible to program using the Winrt API without resorting to XAML?

Asked

Viewed 505 times

13

Some time ago declarative languages took 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 the declarative language and the library Qt framework with its QML (Qt Markup Language).

Unlike Qt that does not publicly expose the code (classes) responsible for the effective execution of the interface, requiring 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 does not allow to do 100% in code), the WPF allows two programming modes: 1) declaratively through the XAML, which is the most used mode and 2) imperative/object oriented programming with code through the exposed classes, After all, XAML is all built on top of existing classes, even counting on auxiliary classes to convert values from XAML to code. Roughly there is a 1:1 relationship between classes and their members and the markings of the XAML code and its attributes.

Although ostensibly demonstrated that XAML should be used primarily - this has several advantages - and even some people believe it is the only way, the documentation, books on WPF, and some websites even specific on the subject (also here and in the ONLY ORIGINAL) clearly indicate the possibility to use only nondeclarative programming.

I have a software that generates all the screens on-the-fly, through code. Adapting this software to Windows 8, it would be absurd at runtime I generate the XAML to then 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 already searched extensively on the internet in general and on specific websites if that is also possible and found nothing. I couldn’t find anything that indicated that it is possible or even that it is not. I couldn’t find an example even showing only code. I certainly found a lot of code that helps 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 construction without using anything XAML, only with code, and of course, where I can find information/documentation about it, if possible with examples. In other words, is 100% of the code responsible for the construction and interaction of the GUI exposed to the programmer? Some example here would be very welcome.

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

For me it’s the same for Winui.

  • 1

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

  • 1

    @Marcoszolnowski You’re right that’s not quite what I’m looking for, but it’s still an answer that can help those who can use it this way and that’s important too. I wouldn’t accept how To answer, but it is a possible one. You have knowledge whether this is the only way, or would not risk saying this?

  • Here is the url of a video, which although old, deals with what would be the past, present and future of XAML: http://channel9.msdn.com/Shows/Going+Deep/Rob-Relyea-Theres-Something-About-XAML

2 answers

6


WPF has nothing to do with HTML!

It is possible to use WPF without XAML (XAML is actually always translated to C#).

It’s as simple as Windows Forms:

E.g.: Let’s assume I want to make a page (not to be confused with a web page) 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;
        }

        }

}

Answering the doubts of the comment:

This is Winrt (ie Silverlight for Windows Store Apps).

I have no references beyond the XAML documentation. Everything XAML does, there is a corresponding class. If you have Grid, StackPanel, Button, etc. in XAML, you will inevitably have them in the WPF namespace (whatever version). This even includes features like 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 in XAML that does not exist in this namespace (after all, there is no XAML).

You can accomplish any task this way, although this is very bad. XAML facilitates writing and performs some shortcuts that, to do manually, you should turn to documentation. An example of this is putting a text inside a button. In XAML, it’s simple. In code you must explicitly specify that the contents of the button is a TextBlock (something like a Label).

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

  • Very interesting. I think you are getting where I want. Let’s have a few questions: you said "It is possible to use WPF without XAML". Do you understand that I know this and that my doubt is about doing the same with Winrt? Where was your example taken from? Do you have a reference where I can find more examples? Is there anywhere with authority that says you can use 100% Winrt with code and zero XAML? More complex things can be done this way too? Do you have any idea why it’s all in Xaml namespace when it’s not being used? If you can edit to put these doubts...

  • Is there a documentation link that shows some examples like the one you posted so I can do some studies? I’ve only seen XAML. That’s why I kept thinking that only XAML was possible. It seems they hide that it can use code. Unlike WPF that documentation often shows the two forms.

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 (no XAML and only via code). But I believe that is not the answer you are looking for, if it is, I will transfer to the answers section.


I would add that it is possible to make the dynamic creation of elements thus:

        // 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);
  • I appreciate the example, but it’s really not what I’m looking for yet. I’d like to avoid creating all this. Maybe there’s no way. Then I need to ask about how it is done on iOS and Android using Xamarin.

  • @Explain a little better what you need then, please. I had understood that you wanted to be able to create all the components dynamically without XAML. This is very verbose even when everything is done programmatically.

  • When to Xamarin.iOS and Xamarin.Android open a question there that I answer. These I use always and with some frequency need to create in hand the components.

  • These questions I’m going to ask you slowly. What I wanted is to program as in WPF, where you directly access the classes that will execute the code. XAML is a layer over these classes. In the case that you showed the XAML is being used. Indirectly, but it is he who is commanding the execution. I do not know if it is possible. But I have never found anyone saying clearly that it is not. Your solution can even be used if there is no other way, but it is more or less what I knew you could do. It can be a little difficult to understand why almost nobody uses WPF programmatically.

  • I think that’s the most programmatically you can do. As far as I know the WPF was built to have an abstraction layer of the View which is HTML or XAML. I believe it is not possible to do with it. But if you are going to do everything programmatically why use WPF, why not use Winforms or another Framework?

  • I can say from experience that you can do 100% programmatically in WPF and it’s almost the same as doing for Winforms which is a technology that didn’t meet the needs well. Now there is a need to create a presentation layer for other devices where neither Winforms nor WPF work. And anyway it would be better to always use something native. There is a lot of misinformation about WPF. And I found that information about Winrt is missing. I hope you don’t miss Xamarin. But that’s a subject for later :)

  • In Xamarin you’ll use the native Apis, so the information you’ll need is how to create Views on Android and iOS and then just port the language. Libraries are the same and mapped 1-1. You’ve thought of more cross-platform things like GTK or similar?

  • I prefer native Apis. In practice GTK is only good on Linux. And most of its bindings are outdated. But I’m not so worried now because it’s a product that’s sitting still for now.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.