Wednesday, May 09, 2018

I just "gave" a load of code to Microsoft

TLDR: Announcing a new open source project for making XAML development easier.


What's this about giving away code?

I had an idea for something, built a proof-of-concept that impressed and excited some people, and we decided that the best way to get it to lots of people quickly was for Microsoft to release it as an Open Source project. So, that means I've "given" the code to Microsoft on the basis that they'd make it open source and thereby giving it away to everyone. In practice, it means it's in a Microsoft owned repository but I'm an admin and main contributor.


What is this thing then?

Introducing the Rapid XAML Toolkit.

It's a Visual Studio extension that provides functionality to accelerate XAML development.
It's rare for developers to build the XAML UI for an app before having some sort of data model that the UI will represent and allow interaction with. It might be from a database, web service, or somewhere else but if you've got some code that describes what should be there, why not let the tool create it for you?


Of course, it's more than just a file. It contains suitable XAML that represents the ViewModel, wires up the bindings, and sets the data context. It's not going to be the final XAML you need but it's going to do in two clicks what could otherwise take a few minutes. Given the choice, would you rather have a blank page or something that works and you can just tweak to your needs?

As an example, given a class that looks like this:

    public class OrderDetailsViewModel : ViewModelBase
    {
        public int OrderId { get; private set; }
        public Guid CustomerId{ get; set; }
        public DateTimeOffset OrderDate { get; set; }
        public string OrderNotes { get; set; }
        public decimal OrderTotal { get; }
        public ObservableCollection<OrderLineItem> Items { get; set; }
    }

It could produce this:

    <StackPanel>
        <TextBlock Text="{x:Bind ViewModel.OrderId}" />
        <TextBlock Text="{x:Bind ViewModel.CustomerId}" />
        <DatePicker Date="{x:Bind ViewModel.OrderDate, Mode=TwoWay}" />
        <TextBox Text="{x:Bind ViewModel.OrderNotes, Mode=TwoWay}" />
        <TextBlock Text="{x:Bind ViewModel.OrderTotal}" />
        <ListView ItemsSource="{x:Bind ViewModel.Items}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="OrderLineItem">
                    <StackPanel>
                        <TextBlock Text="{x:Bind OrderLineId}" />
                        <TextBlock Text="{x:Bind ItemId}" />
                        <TextBlock Text="{x:Bind ItemDescription}" />
                        <TextBlock Text="{x:Bind Quantity}" />
                        <TextBlock Text="{x:Bind UnitPrice}" />
                        <TextBlock Text="{x:Bind LineTotal}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>


How does it know what XAML to create?

The generated XAML is based on the type and name of the property and whether it is read-only. There are some carefully chosen options provided by default but everything is configurable. In the future, it may also be powered by AI too ;)


How does it know where to put the created file?

It's based on conventions but is fully configurable. It even supports having the View and ViewModel in different projects if that's what you prefer.


That all sounds like one feature. Why call it a toolkit?

Because there are plans for much more. XAML generation is just the start, but it enables lots of scenarios and opportunities.

Even today, it doesn't just provide the ability to produce whole files. It also provides the ability to generate XAML without having to create the file. You can generate the XAML for an entire class, an individual property, or a selection of properties. This raises the question of where the XAML will go. We leave that up to you. The generated XAML can either be copied to the clipboard (so you can paste it wherever you wish) or sent to the clipboard (so you can drag it where you want it.)

The final part of setting up the basic association between VM and View is to ensure that the data context for the page is correctly set. If it's not, then it provides the option to do this for you.








Does it do X?

Probably not...yet! There are lots of features planned but if you've got a suggestion, please raise an issue in the GitHub repository.


How does this compare to Windows Template Studio?

Windows Template Studio will help you to scaffold a UWP app. The Rapid XAML Toolkit is a sister project and can help you once you've created the stub of your app, or if you have an existing app. It doesn't have to be a UWP app either. We hope this will be helpful to Xamarin developers too.


Wait, what's this about Xamarin? I thought it was a UWP thing.

Nope, it's a XAML thing!
While there may end up being some elements or features that are UWP only, the intention is that it will be beneficial to any developer who is working with XAML. This means Xamarin.Forms and WPF. (It will work with Silverlight too, but I'm just not providing any default configuration for it.)


Really, WPF?

Yes, WPF too.


Anything else?

Yes, it works with both C# AND VB.Net.


Really, VB.Net?

Yes, because all developers deserve nice things. Plus, we think this will be particularly beneficial to developers converting WinForms and WPF apps to UWP, and we know a lot of them use VB. If they're learning UWP, they shouldn't have to learn C# as well.


Where can people find out more?

GitHub is the place. Go to https://github.com/Microsoft/Rapid-XAML-Toolkit and check out the current features.


p.s. As you'd expect, I'll be sharing more details soon, but, if you're at //build, I can show you more. You'll find me at the Windows Template Studio & Community Toolkit booth.




2 comments:

  1. Hi, can I check how you managed to publish the repo under MS (under their GitHub repo)? Thanks! Thanks also for this interesting post!

    ReplyDelete
    Replies
    1. It was a combination of the right project, in the right place, at the right time, and knowing the right people. This is an exceptional scenario and not something broadly available.

      Delete

I get a lot of comment spam :( - moderation may take a while.