Category Archives: visual studio

Microsoft’s new open source direction for C# and .NET (and native compilation too): Anders Hejlsberg explains

At the April 2014 Build conference Microsoft made some far-reaching announcements about its .NET platform and the C# programming language. Yes, there was talk of C# 6.0, the next version, but the real changes are more profound. Specifically:

C# and Visual Basic have a new compiler, itself written in C#, code-named Roslyn. Roslyn is not just a new compiler; Microsoft now calls it the “.NET Compiler Platform”.

There is a new commitment to open source for .NET projects. Microsoft formed the .NET Foundation to oversee existing open source projects, including  ASP.NET, Entity Framework, the Azure .NET SDK, and now Roslyn as well. “When it comes to development projects we are going to operate from the premise that open source is the default. Unless there are reasons why it does not work,” said C# lead architect Anders Hejlsberg.

image

Note that open source does not mean chaos. It does mean that you can fork the project if you want – the Roslyn license is Apache 2.0 – but getting Microsoft to accept new features you have contributed will not be trivial. Hejlsberg makes the point that language features are easy to add, but impossible to take away, so extreme care is necessary.

Microsoft is also supporting cross-platform C# to a greater extent than it has done in the past. The most obvious sign of this is its cooperation with Xamarin, which provides C# compilers for iOS and Android. Xamarin’s Miguel de Icaza got a top billing at Build, and is also involved in the .NET Foundation.

There is more though. The idea of standardised C# is re-emerging:

“The last ECMA standard was C# 2.0. There wasn’t a lot of demand for it, but that demand has recently risen and we have re engaged with the ECMA community to produce a standard for C# 5.0,” said Hejlsberg.

This bears some unpacking. Why was there little demand for ECMA C#? Partly I would guess from the assumption the C# was firmly in Microsoft’s grip, with Java the obvious choice for cross-platform development. The main interest was from the Mono folk (Miguel de Icaza again), which implemented .NET for Linux and the Mac with some success, but nothing to disturb Java’s momentum.

The focus now though is on mobile, and interest in C# is stronger, mainly from Microsoft-platform developers reaching beyond Windows. There is also Unity, which uses C# as a scripting language for developing games for multiple platforms, including iOS, Android, Windows, Mac, Linux, Xbox, PS3 and Wii – PS4 is coming very soon.

Microsoft has now consciously embraced multiple platforms, as evidenced by Office for iOS as well as the Xamarin collaboration. “We want C#developers to build great applications across different form factors and different device platforms,” said Jay Schmelzer Director of Program Management for Visual Studio.

You might observe that this position has been forced on the company by the rise of iOS and Android, a view which likely has some merit, but the impact it has on C# and .NET itself is still real.

I asked Hejlsberg to unpack the difference between the Roslyn project and C# 6.0, bearing in mind that both are covered on the Roslyn open source site; you can see the current status of C# 6.0 and the next Visual Basic here.

Roslyn is the name for the project that encompasses the new C#compiler and the new VB compiler and the new language services that they share. C# 6.0 is the name of the next version of the C #language which will have a specification and which will have an implementation. We are implementing C# 6.0 on the Roslyn platform. We are not going to continue to evolve our old C++ C# compiler – the C# compiler was originally written in C++ and has been evolved up through C# 5.0. That is where we are going to retire that code base, and going forward versions of C# will be built on Roslyn and therefore will be built open source. Unlike previously where, boom. C# came down from the sky with a set of features, it is going to happen more organically now, people will submit pull requests, open up issues, and you will see us work on these features. You will see them from inception to fruition.

“The C# team, the Roslyn team, the VB team, their day to day workplace now is the open source site. That is where they check-in code. It is a community in the making.

Even that is not all. At Build, Microsoft also announced .NET Native, which is a native compiler for C# and Visual Basic, now in preview for x64 Store apps. What is the difference between .NET Native and the existing NGen native compiler for .NET? Over to Hejlsberg:

NGen is the native feature that we currently support. NGen is really, “I’m going to JIT [Just in time compile] your code and then snapshot all the data structures and dump them in a file so that I can quickly rebuild that file later when you run this particular application”. But it is the same code generator and all the same features, and JIT is still there. NGen is really a way to pre-cache the JIT output and therefore get better performance, but it adds to the size of your app because you still have all the assemblies and metadata and then the NGen image as well.

.NET Native is a completely different approach. Instead of the JIT we use the backend from the C++ compiler. You can think of it as a linker that takes as input assemblies, and as output produces a PE [Portable Executable] executable. In the process this linker or code generator will analyse all the IL [Intermediate Language] that goes into the application and it will apply a thing known as tree-shaking where it eliminates all of the code that will never execute based on known execution roots.

In other words, the public static main of your program and also whatever pieces of your app that you designate as reflectable, they also become roots. Based on that we produce an optimised exe, and into that exe we link the pieces of the framework that you are referencing. We link in a garbage collector [GC], and it looks to the operating system just like an exe. When you run it, it runs a local GC in there and it is as efficient really as C++ code.

There are some restrictions associated with .net native, mainly that you can’t just willy-nilly reflect on the whole world. You can’t just generate new code and ask for that to be jitted because they may not be a JIT compiler. We are considering allowing you to link in a JIT compiler, but there are certain execution environments which don’t permit jitting, like Xbox. If you use reflection in your lap you have to tell us what to keep reflectable, because otherwise we will optimise it away.

According to Schmelzer:

The preview out today is scoped to Store app x64 and ARM. We haven’t run into any technical limitation that shows it can’t be done across the breadth, it is just a matter of request and need.

Open source, native code compilation, and an innovative compiler: it adds up to huge changes for C# and .NET, positive ones as far as I can tell.

The Xamarin connection is intriguing though. Developers in general admire the technology as far as I can tell, but it is expensive, and paying out for a Xamarin subscription on top of maybe MSDN for Visual Studio is too much for some smaller organisations and does not encourage experimentation. Might Microsoft acquire Xamarin and build Visual Studio into an IDE targeting all the major mobile platforms, but with special hooks to Azure-hosted services?

That prospect makes sense to me, though it would be a shame if the energetic Xamarin culture became bogged down in big-company bureaucracy. Currently though: no news to report.

XAML and C#, or HTML and WinJS for Windows Store, Universal and cross platform apps?

Microsoft designed the Windows Runtime (WinRT, the engine behind the controversial touch-friendly “Metro” user interface in Windows 8) to support three development platforms. These are C++ with XAML (for most GUI apps) or DirectX (for fast games); or C# and XAML; or HTML and JavaScript using the WinJS library for access to Windows-specific functions.

Microsoft’s line is that all three approaches are fine to use, with little performance difference other than that C++ avoids an interop layer. Of course if you have any arbitrary code that runs faster in C++ than in C#, then you will still see that difference in the WinRT environment.

It is also obvious that if you are an HTML and JavaScript expert but know nothing of C# or XAML, you should use WinJS; similarly if you have a lot of C# code to port and know nothing of HTML or JavaScript, C# and XAML chooses itself.

But what if you approach the decision from a neutral perspective. I am going to leave aside the C++ option for the moment as it is more of a leap than C# versus JavaScript. Which is best?

On the WinJS side, a common misconception is that this library is only for Windows. At Build 2014 Microsoft announced that WinJS is now open source, and works on other browsers and devices:

The library has been extended to smaller and more mobile devices with the release of WinJS 2.1 for Windows Phone 8.1, which was announced today at //build. Now that WinJS is available for building apps across Microsoft platforms and devices, it is ready to extend to web apps and sites on other browsers and devices including Chrome, Firefox, iOS, and Android.

In order to sample this, I went along to try.buildwinjs.com on an iPad. All the things I tried quickly worked fine on iOS.

image 

If you used WinJS to build an app, you could use PhoneGap or Cordova to package it as a native application for the iOS or Android app stores.

A further reflection on this is that some of the WinJS controls which you might have assumed are native WinRT controls instantiated from JavaScript are in fact implemented in CSS and JavaScript. That is an advantage for cross-platform, but does suggest that Microsoft has been busy duplicating the look and feel of XAML controls in HTML and JavaScript, which seems a lot of work and an approach which is bound to result in inconsistencies.

Another snag with this approach – leaving aside the questions of performance and so on which I have not investigated – is that you end up with the distinctive look and feel of a Windows 8 app, which is going to be surprising on these other devices.

C# also has cross-platform potential, thanks to the great work of Xamarin and not forgetting RemObjects C#. Note though that I wrote C# rather than XAML. There is no cross-platform XAML implementation other than the abandoned cross-platform Silverlight efforts, Silverlight for the Mac and Moonlight for Linux. Xamarin expects you to rewrite your UI code for each platform – which may in fact be a good thing, though more effort.

If you are focused on the Windows platform though, it seems to me that the pendulum is swinging away from WinJS and towards C# and XAML. Wordament is an interesting case. This is one of the better games for Windows 8, and also available for Windows Phone, iOS and Android. Originally this was implemented in HTML and JavaScript. The developers have blogged about the choices they made:

Wordament grew up very fast. It seemed like it went from an indie game with a handful of players to a full on Microsoft title with millions of users, on lots of platforms, almost overnight. In that transition we ended up writing lots of code. For instance, in the course of a year we had ports of the Wordament client written in JavaScript, Objective C, C++ and C#. Each one of these ports has its own special issues, build processes and maintenance challenges. At the time, we were still a two person team and Jason and I were struggling to continue to innovate on Wordament, while supporting the “in-market” code bases we had shipped. So we started looking for a solution that would allow us to share more code between all of the platforms we were targeting. Funnily enough, the answer was sitting in our own backyard: C#.

As we looked around at the state of “cross platform development” on Windows, Windows Phone, iOS and Android we started to realize that C# was an excellent choice to target all modern mobile devices. So we did just that. With the help of Visual Studio for Windows and Xamarin for iOS and Android, we started a project to build a single version of Wordament’s source code that could target all the platforms we ship on. This release on Windows 8 marks the end of that journey. All of our clients are now proudly built out of one source tree and in one language. Even our service, which runs on Windows Azure, is built in C#. This is a huge efficiency win for our team of four.

Notable also is that the forthcoming Office for WinRT, previewed at Build, is written in XAML and C++ (according to what I was told). This means XAML will get some love inside Microsoft, which is bound to be good for performance and features.

An advantage of the C# approach is simply that you get to use C#, which is well-liked by developers and with some compelling new features promised in version 6.0, many thanks to the Roslyn project – which also promises a smarter editor in Visual Studio.

What about XAML? This is harder to love. XML is out of fashion – too verbose, too many angle brackets – and the initial promise of a breakthrough in designer/developer workflow thanks to XAML and MVVM (Model – View – ViewModel, which aims to separate code from user interface design) now seems hollow. I am writing a game in XAML and C# and do not much enjoy the XAML part. One of the issues is that the editing experience is less satisfactory. If you make an error in the XAML, the design view simply blanks out with an “Invalid Markup” error. Further, the integration between the XAML and C# editors is a constant source of problems. Half the time, the C# editor seems to forget the variables declared in XAML, giving you lots of errors and no code completion until you next compile. Even when it is working, a rename refactor in the C# editor will not rename variable references that are within quotes in the XAML, for example property names that are the targets of animations.

On the plus side, XAML is amazing in what it can do when you work out how. The user interfaces it generates are rich, scalable and responsive. It is way better that the old Win32 GDI-based approach (also used in the Windows Forms .NET API), which is hard to get right for all the combinations of screen sizes and resolutions, and has odd dependencies on system font sizes and dialog units (don’t ask).

Despite the issues with XAML, C# and XAML (or C++ and XAML) is my own preference for targeting the new Windows platform, but I am interested in other perspectives on this.

Microsoft completes Nokia acquisition: what now for Windows Phone?

Microsoft has completed its acquisition of Nokia today, a milestone in the turbulent story both of Nokia and of Windows Phone, which Nokia adopted in the hope of establishing a “third ecosystem” to challenge Apple iOS and Google Android.

Rumour has it that the Nokia acquisition was controversial within Microsoft and a large factor in the departure of Steve Ballmer as CEO. However, even if Microsoft took the view that an independent Nokia was better for Windows Phone, it faced the risk that market pressure would drive Nokia to Android and weaken the platform. The beginnings of that process may have been under way, with the launch of the Nokia X Android-but-not-Google range of phones, but we will never know, since Microsoft decided on acquisition.

image

How important has Nokia been for Windows Phone? In my view, life-saving. Before Nokia, there was no manufacturer nor operator which really cared about the platform, and it showed in lacklustre hardware and half-hearted marketing efforts. Nokia came up with the distinctive Lumia brand and style, added a decent mapping service, and with its focus on the PureView camera technology, gave enthusiasts a reason to take a close look at its devices. It also saw an opportunity at the low end, and created some great value devices that opened up a new market for the operating system.

There were some blunders (the original Lumia 800 suffered many faults and terrible battery life on launch) and Lumia did not grow fast enough to restore Nokia to health, but to my mind it was a good effort.

Today, the general opinion of Windows Phone is that it is a strong smartphone operating system but suffers from a lack of high-quality apps. Users have to put up with the fact that most app vendors feel they are done if they support iOS and Android; and if there is a Windows Phone version of their app, it is often poor. That is not a great position for Microsoft/Nokia to be in, but it could be worse. Blackberry 10, which is also a decent mobile operating system, has been all-but written off as a viable contender.

Microsoft is fortunate in that, unlike Blackberry, it can to some extent create its own ecosystem. Office 365, Bing, OneDrive, Nokia’s maps, Azure for developers needing a cloud back-end: taken together they form a viable alternative. In this respect, Microsoft actually has an advantage over Apple, which lacks this breadth of services.

I have been reading the latest Developer Economics report from Vision Mobile. It is a good example of the neutral perspective on Windows Phone, though you will find it somewhat inconsistent:

Windows Phone sales picked up significantly in Q3 2013, showing a 140% increase year-on-year, fuelled primarily by low-end device sales. According to Kantar, Windows Phone sales in the three months running to Oct 2013, reached double-digit figures in some Western European markets. While this is certainly a positive sign for Microsoft they will continue facing an uphill struggle, in an increasingly unfavourable race against the two runaway leaders, iOS and Android.

The report emphasises that iOS and Android have won the mobile OS wars, but says that there are signs of hope for Microsoft:

Windows Phone Developer Mindshare has finally moved upwards, following positive market signals in the last two quarters. As we have frequently highlighted in past reports, the developer intent has always been there, with Windows Phone figuring at the top of our Developer Intentshare chart, but needed positive market signs in order to convert this interest into Mindshare. While the 26% Developer Mindshare is still less than half of that for iOS, Microsoft can now claim that over a quarter of developers that target mobile platforms are now actively developing for Windows Phone. […]

As a latecomer to a mobile market dominated by strong network effects, establishing a credible footprint in mobile remains a formidable challenge
for Microsoft. We believe that Microsoft may be better served in the long-run by leveraging the Android ecosystem as the deployment platform for Office
and Server businesses which are still growing.

Microsoft is in fact supporting iOS and Android as clients for its cloud services, as noted again at yesterday’s financial webcast, where CEO Satya Nadella talked about a strategy that goes across “devices some ours, some not ours.” It is a bit of both though, and the company is not showing any signs of weakening its own mobile efforts.

In my view reports like that from Vision Mobile miss a couple of factors. One is that Windows and Windows Phone are converging. They already use the same OS kernel, and at the Build conference earlier this month Microsoft announced Universal Apps that will run on both, and the ability for developers to sell an app once and have users install on both phone and full Windows.

This means that the future of Windows Phone and that of Windows itself are closely bound together. Longer term, they will either both fade away, or both succeed.

Windows remains a huge business for Microsoft, despite the decline of the PC, especially in business. Microsoft’s problem though is that adoption of Windows 8 has been relatively weak, and that those who do use it, largely live in the desktop environment rather than running Store apps (of which Universal Apps are a variant).

Despite the dismal progress so far for the Store apps platform and ecosystem, I believe it should be taken seriously. On paper it has many advantages, not only for touch control, but also in deployment, security, roaming data driven by the cloud, and discoverability through the store. Isolation from the core operating system protects users against the things that destroy desktop Windows, like unwanted extras foisted on users who simply need to update Java or Flash.

At Build we saw not only Universal Apps, but also a preview of Office in the Windows Runtime (Store app) environment. We also saw a preview of Store apps running within a window in the desktop environment, solving the jarring transition between desktop and Store app environments that unsettles users. If Microsoft gets this right, both Windows Phone and Windows tablets will be substantially more attractive.

Microsoft also has the ability to bind Windows Phone into its enterprise device management environment, System Center and InTune. In Windows Phone 8.1 the device management and security features businesses need are much improved. More is still needed; but the company should be able to build integration points that make it attractive to business customers already using products such as Active Directory, Microsoft Office, Office 365, System Center or InTune.

Another factor is the strength of Visual Studio for developers, especially as Microsoft improves its integration with cloud services like Azure and Office 365. You can use C# everywhere from cloud or server to mobile client.

image

Cortana is sure that Windows Phone is the best; but check out the Bing ad.

What then is the future of Windows Phone? Uncertain, as ever; but if Microsoft pulls off a smooth Nokia acquisition – leaving in place the things that enabled the company to build the Lumia brand – and if it delivers on the promise we saw at Build, of a strong unified platform, then I expect market share to continue to grow. If it can climb to 10% or 15%, it will be on the map for vendors and the app problems will ease.

On the other hand, if Microsoft/Nokia means a return to the ineffective marketing and strategy we saw before Nokia adopted Windows Phone, then I expect Windows Phone to follow Blackberry into oblivion.

I am positive, but Microsoft needs to execute carefully and quickly to win market share for its mobile platform.

Notes on styling a Windows Store app ListView to vary item appearance according to the data

Problem: You have a ListView containing data. You want to vary the appearance of items in the ListView according to the value of the data.

I spent some time on this in relation to a panel for a game I am writing. For example, you have a ListView containing numbers. How can you have negative numbers appear in red?

image

In desktop WPF (Windows Presentation Foundation) you could do this with Property Triggers but these are not supported in Store apps.

One way to do this is with a value converter. Add a class to your project called MyValueConverter. Make the class public, and inherit from Windows.UI.Xaml.Data.IValueConverter.

Right-click IValueConverter and choose Implement Interface to have Visual Studio create two stub methods, Convert and ConvertBack.

This class is going to return an object which will be applied to the Foreground property of a ListViewItem. The Convert method looks like this:

public object Convert(object value, Type targetType, object parameter, string language)

At runtime, the value argument will contain the item displayed in this row of the ListView. The targetType will match the type of the property we are setting, which in this case is a Brush object.

Now add an instance of MyValueConverter to MainPage.xaml (or App.xaml) as a resource. If there is no Page.Resources element, create it, and add an instance of MyValueConverter with the Key “NumberForegroundConverter”:

<Page.Resources>
<local:MyValueConverter x:Key="NumberForegroundConverter" />
</Page.Resources>

Next, select the ListView element in the XAML editor or designer. Right-click the selected element in the designer, and choose Edit Additional Templates – Edit Generated Item Container (ItemContainerStyle) – Edit a Copy …

image

Accept the default name of ListViewItemStyle1 and click OK.

This generates an element that defines the layout and appearance of items in the ListView. Currently it does not appear to do anything, since it is a copy of the default settings.

Find the element called <ListViewItemPresenter> which is nested within <ControlTemplate TargetType=”ListViewItem”>. No Foreground attribute for ListViewItemPresenter is generated, but we can add one:

Foreground="{Binding Converter={StaticResource NumberForegroundConverter}}"

If you now run the project, you will get an exception, because the methods in MyValueConverter do not yet have any code. Now we have to think about the type of the items in the ListView. In this example, I just typed some strings into the XML editor:

<ListView ItemContainerStyle="{StaticResource ListViewItemStyle1}">
<x:String>145</x:String>
<x:String>-30</x:String>
<x:String>442</x:String>
</ListView>

All the items are strings, so the Convert method can look like this:

String s = (String)value; //note this ONLY works if the item is always a string

if (float.Parse(s) < 0)
{
return new SolidColorBrush(Windows.UI.Colors.Red);
}
else
{
return new SolidColorBrush(Windows.UI.Colors.White);
}

We don’t care about the ConvertBack method so can use this code:

return Windows.UI.Xaml.DependencyProperty.UnsetValue;

It works but there are some issues. One oddity is that when you roll the mouse over a negative number, it looks the same as a positive number.

image

This is because we did a converter for the Foreground property but not for the SelectedForeground property. XAML in Store apps makes extensive use of themes, and themes include a lot of brushes.

Another issue, which may or may not impact your application, is that the converter code does not run again if you change the displayed item dynamically. That is, if you replace the item it updates OK, but if you update the existing item it does not.

A slightly more complex example will demonstrate this. Let’s say that rather than displaying strings, the ListView is displaying Widget quantities, where a negative number indicates backorders. The ListView is bound to an ObservableCollection<Widget>, and the Widget class implements INotifyPropertyChanged so that the ListView will update automatically when a Widget property changes. Note that the NumberForegroundConverter must be updated to accept a Widget value rather than a string.

Here is what happens if a Widget had a negative quantity when the ListView was first populated, but got dynamically updated to a positive value (some stock arrived):

image

Oops! Now the positive quantity is in red.

We can fix this by abandoning the converter, and instead giving the Widget class a Foreground property of its own, calculated to return Red for negative quantities and White for positive. Make sure it fires a NotifyPropertyChanged event when updated. Now the Foreground property in <ListViewItemPresenter> looks like this:

Foreground="{Binding Foreground}"

It works:

image

I used this approach in my game in order to implement an Enabled property that indicates items which are unselectable. This changes dynamically according to the state of the play.

Note that you are unlikely to want a Foreground property in your business objects, but could create a DisplayWidget class for the purpose.

I realise that these are not the only ways to create a ListView which styles items differently according to their values, but they may be the simplest. Other suggestions and comments are welcome.

Update: Mike Taulty has some comments and suggestions here.

A quick hands-on with native code compilation for .NET

I had a quick look at the .NET Native Preview. I am interested to see what the benefits might be. Note that currently the preview only supports 64-bit Windows Store apps.

Here is what is promised:

For users of your apps, .NET Native offers these advantages:

  • Fast execution times
  • Consistently speedy startup times
  • Low deployment and update costs
  • Optimized app memory usage

I created a small C# app that counts prime numbers using a simple approach. I created it first as a Universal App that does not use .NET Native, and then as a second app that does use .NET Native.

image

My initial results are disappointing. The time taken to count prime numbers is similar in both apps.

image

As a further test, I added code that adds the prime numbers found to a ListView control using an async task. The idea was to see if GUI interaction and multi-threading would be more revealing than simply crunching numbers in a tight loop. The apps takes much longer with this enabled, but again, there is nothing to choose between the two.

Did I really succeed in compiling the app to native code? I think so. Here are the contents of the AppX folder for the standard .NET executable:

image

and here for the native compiled executable – note the additional DLLs:

image

I am not actually surprised that there is no performance benefit in my example. JIT (just-in-time) compilation means that any .NET application is native code at runtime, though optimization might be different.

I have ended up with a much larger app to deploy, though the relative difference would be less, I imagine, with an app that contains more code.

I can also readily believe that start-up time will be better for a native compiled app, since there is no need for the JIT compiler. However my app is so small that this is not significant.

My question though is what kind of app will benefit most from native compilation? I would be interested to see guidance on this.

Of course it is also possible that later iterations of the technology will yield different results.

Microsoft Build 2014: what happened

It’s curious. Microsoft’s new CEO Satya Nadella has been in place for only a month which means that almost everything announced at Build, Microsoft’s developer conference which took place last week in San Francisco, must have been set before he was appointed; yet there was a sense of “all things new” at the event, as if he had overseen a wave of changes.

The wave began the previous week, with the simultaneous announcement and delivery of Office for iPad. The significance of this is threefold:

  • It demonstrated Microsoft’s decision to give first-class support to mobile platforms other than Windows
  • It demonstrated that Office can be redesigned to work nicely on a tablet
  • The quality of the product exceeded expectations, showing that in the right circumstances Microsoft can do excellent non-Windows software

image

Next came Build itself. It was a tale of two keynotes. The first was all about Windows client – both Phone and PC. The core news is the arrival of the Windows Runtime  (WinRT, the engine behind Metro/Store Apps) on Windows Phone 8.1. This means that WinRT is now the runtime that developers should target for apps that run across phone and desktop – and even, we were shown, Xbox One, which will support WinRT apps written in HTML and WinJS (Microsoft’s JavaScript library for Windows apps).

In support of this, Microsoft announced a new Universal App project for Visual Studio, which lets you share both visual and non-visual code across multiple targets. How much is shared is a developer choice.

There is more. A Universal App is now (kind-of) a desktop app as well as a Store app, since in a future free update to Windows 8, it will run on the desktop within a window, as well as appearing in the Start menu on the desktop. We were even shown this; apparently it is a mock-up. This was the biggest surprise at Build.

image

What did Executive VP Terry Myerson say about this? Here is the exact quote:

We are going all in with this desktop experience, to make sure your applications can be accessed and loved by people that love the Windows desktop. We’re going to enable your Universal Windows applications to run in a window. We’re going to enable your users to find, discover and run your Windows applications with the new Start menu. We have Live Tiles coming together with the familiar experience customers are looking for to start and run their applications and we’ll be making this available to all Windows 8.1 users as an update. I think there will be a lot of happy people out there.

This is significant. When Myerson says, “we are going all in with this desktop experience”, he does not mean backtracking on Windows Store apps, to return to desktop windows apps (Win32 or WPF) as the future of Windows development. Rather, he means Windows Store apps integrated into the desktop.

There is a further twist to this. Windows Store apps are sandboxed and cannot communicate with each other or with the operating system other than via carefully designed and secured paths. This is in general a good thing, but restrictive for businesses designing line of business apps. It also means that legacy code cannot be carried over into a Store app, other than by full porting.

In the just-released Windows 8.1 Update this has changed. Side-loaded apps (in other words, not deployed from the Windows Store) can now escape the sandbox thanks to Brokered Windows Runtime Components. There are some limitations (32-bit only on the desktop side, for example) but this will make it possible to implement business applications as Store apps even if they need to interact with existing desktop applications or services.

There is still a huge blocker to Store apps from a business perspective, which is that you need Windows 8. Still, my guess is that once the update with the restored Start menu appears, most of the objections to Windows 8 will melt away.

We also saw Office for the Windows Runtime, which will run on both Phone and PC. It is written, I discovered later, in XAML, DirectX and C++ (“Blazingly fast”, we were told). Corporate VP Kirk Koenigsbauer introduced a preview of this, or at least PowerPoint.

image

No detail yet, and several references to “early code” suggest to me that this is a year or more away from full release (giving Office on iPad a big head start); but it will come. Koenigsbauer did not call it cut-down; in fact, it was instanced as proof that WinRT is suitable for large-scale apps, so I would expect something more complete than Office on iPad; yet it is hard to imagine things like the VBA macro language appearing here in its current form (VBA is based on the ancient Visual Basic 6.0 runtime), so there will be some major differences.

We also saw Windows Phone 8.1, including the Cortana virtual personal assistant who responds to voice input. For me other things in Windows Phone 8.1 are more significant, including new swipe-style keyboard for fast text input, VPN, S/MIME secure email, and a new notification centre. Unlike touch Office, Windows Phone 8.1 is coming soon; Nokia’s Stephen Elop (soon to be in charge of Windows Phone at Microsoft) said that the first 8.1 Lumia devices could be out from May, depending on territory, and that all Lumia Windows Phone 8 devices will get the update in the summer.

On to day two, which was Cloud day, though we also got significant .NET developer news.

Executive VP Scott Guthrie introduced a new portal for Microsoft Azure, the cloud platform. This is not just a new look, but integrates with Visual Studio online so you can easily view and edit the code and track team projects. There are also new monitoring and analytics features so you can check page views, page load time, browser usage and more. Guthrie also announced integration with Puppet and Chef for deployment automation.

image

Language designer Anders Hejsberg also came on stage. He announced the release version of TypeScript, a “typed superset of JavaScript” which is suitable for large applications. He also announced a new preview release of the compiler project code-named Roslyn, and on stage pushed the button that published it as open source. What is Roslyn? It is the next generation compiler for C# and VB, and is itself written in C#. This enables compiler and workspace APIs, which in turn enable rich editor features:

The transition to compilers as platforms dramatically lowers the barrier to entry for creating code focused tools and applications. It creates many opportunities for innovation in areas such as meta-programming, code generation and transformation, interactive use of the C# and VB languages, and embedding of C# and VB in domain specific languages.

Roslyn will be fully released in the next version of Visual Studio, for which we do not yet have a date. Roslyn will be delivered alongside C# 6.0.

There is also a new .NET Foundation which will oversee open source projects for .NET, with backing from folk including Xamarin’s Miguel de Icaza and Umbraco’s Niels Hartvig. It is all a bit vague at the moment:

In the upcoming months, the .NET Foundation will be inviting many companies and community leaders to join the foundation, including its Board of Directors and will then finalize its operational details, including governance models for its open source initiatives, membership structure and industry and community engagement.

Another significant event in the .NET story is the arrival of true native code compilation for .NET, although currently only for 64-bit Store apps. More on this soon.

A couple of events during Build caught my eye. One was de Icaza’s session on using C# to build for iOS and Android, not so much for the content itself (though there was nothing wrong with it), but rather for the huge attendance it drew.

image

The session was moved to the Build keynote room, and while there were spare seats, the room felt well filled. This speaks loudly about the importance of those platforms even to Microsoft platform developers, as well as of Microsoft’s support of Xamarin’s work.

Another was the appearance of John Gruber, author of the Daring Fireball blog and an Apple enthusiast. He appeared in a video during the keynote, explaining how a project in which he is involved uses Azure for back-end services, and then in person at another session, interviewing journalist Ed Bott about what is changing at Microsoft.

image

Gruber seems to me representative of a group of smart observers who have not in general been impressed with Microsoft’s endeavours over the past few years; but he for one is now more positive on the subject. Windows Phone is much better than its market share suggests, he said. This alongside Azure and a new openness to supporting third-party clients has made him look more favourably on the company.

My summary is this. On the Windows client side, Microsoft is taking its unpopular Windows release and its minority Phone platform and making them better and more compatible with each other, making sense of the client platform in a way that should result in growth of the app ecosystem both on Phone and PC/Tablet. On the cloud side, the company is building Azure and Office 365 (two platforms united by Azure Active Directory) into a one-stop platform that is increasingly compelling. The result was a conference and a direction that was largely welcomed by those in attendance, as far as I could tell.

That does not mean that the PC will stop declining, or that iOS and Android will become less dominant in mobile. There is progress though, and more clarity about the direction of Microsoft’s platform than we have seen for some years.

For the official news from Build, see the Build Newsroom.

What is a Universal Windows App?

At its Build developer conference in San Francisco, Microsoft has announced a new kind of Windows app: a Universal App. In fact, you can download the latest Visual Studio 2013 update (Update 2 RC) and

image

A Universal App runs on both Windows Phone 8.1 and Windows 8.1. But what is it really?

The place to start is with the runtime. In Windows Phone 8, Microsoft migrated the kernel in Windows Phone from the cut-down CE version of Windows, to the same kernel used by desktop Windows. However the app runtime in Windows Phone 8 remained Silverlight, Microsoft’s Flash competitor which was originally designed as a browser plug-in.

In Windows Phone 8.1 Microsoft has taken the next logical step, and ported the Windows Runtime (WinRT) to the phone. WinRT is the runtime behind the Metro/Modern/Store App environment introduced in Windows 8.

A Universal App runs on WinRT. This means that Windows Phone 8.1 supports the same variety of development options as Windows 8: XAML and C#, XAML and C++, HTML and the WinJS Javascript library (now open source), and DirectX for games.

The port is not 100%; there are some platform-specific APIs. Apparently compatibility is about 90% in terms of APIs available.

That said, a Universal App is not a universal binary. Apparently you can have a universal binary, but it is not the approach Microsoft is taking. A Universal App is a project type in Visual Studio.

image

When you create a Universal App you get a project with multiple targets.

image

By default you get two targets, but we have also seen Xbox One as a target, and conceptually we could see more: maybe Xamarin might extend it to support iOS and Android, for example.

The way this works is that at compile-time any code (which can include XAML and project assets such as images as well as C# code) that is in the Shared project gets merged into the target-specific project.

This means that a Universal App could contain very little shared code, or be almost entirely shared code. This is a developer choice.

Separately, Microsoft has now enabled an app identity to run across multiple Windows platforms in the Store. This means a user can purchase an app once for multiple platforms. However, this is more a business than a technical feature. It would be possible for the developer to offer a multi-platform app in the Store, but keep the development for each platform entirely separate.

That said, the shared WinRT aspect means that code sharing in a Universal App is very feasible. Most if not all non-visual code should work fine, and XAML experts will be able to share most of the UI code as well, thanks to the flexibility built into the XAML UI language.

That is the good bit. There is a problem though. Neither the Windows Phone app platform, nor the Windows 8 app platform have been hugely successful to date, but of the two, Windows Phone has fared better. There are now 500 new apps per day for Windows Phone, we were told here at Build.

Unfortunately, porting those Windows Phone apps to become Universal apps is not easy. Developers have to port their app from Silverlight to WinRT, before they can add a target for Windows 8. They will also need to maintain the old Silverlight app for users with versions of Windows Phone earlier than 8.1. Nokia has promised to offer upgrades for all Windows Phone 8 Lumia models, but that will not the base for all Windows 8 phones out there, and as ever operators have a role here.

Life is easier for Windows 8 app developers who now want to support Windows Phone 8.1; but there are not so many great Windows 8 apps for which Windows Phone users are anxiously awaiting.

Still, the Universal App approach makes perfect sense for the future, once Windows Phone 8.1 is established in the market. It also makes sense for enterprises with internal apps to deploy for mobile and tablet users.  

Microsoft Build Sessions published: Windows Phone XAML and HTML/JS apps, new Azure APIs and more

Developing for Windows Phone is now closer to developing for the Windows 8 runtime, according to information from Microsoft’s Build sessions, just published.

Build is Microsoft’s developer conference which opens tomorrow in San Francisco.

image

Building a Converged Phone and PC App using HTML and JavaScript states that “An exciting part of Windows Phone 8.1 is that you can now start building applications natively in HTML and JavaScript.”

Other sessions refer to the Common XAML UI Framework, which seems to refer to a shared UI framework for Windows Phone and WIndows 8, but using XAML rather than HTML and JavaScript.

This is in addition to Silverlight, not instead, judging by this session:

We’ve been doing a lot of work with new converged XAML app support on Windows Phone 8.1, but what about legacy Windows Phone Silverlight XAML based apps?  Come learn about all the new features we’ve enabled with Silverlight 8.1.

Microsoft has also come up with new APIs for applications that integrate with its Azure cloud platform and with Office 365. The Authentication library for Azure Active Directory lets you build both Windows and mobile applications that authenticate against Azure Active Directory, used by every Office 365 deployment. There is also talk of using Azure for Connected Devices, meaning “Internet of Things” devices using Azure services.

Some other sessions which caught my eye:

Connected Productivity Apps: building apps for the SharePoint and Office 365 platform.

What’s new in WinJS: the road ahead. XAML vs HTML/JS is a big decision for Windows developers.

Anders Hejlsberg on TypeScript

Automating Azure: “The Azure Management Libraries and Azure PowerShell Cmdlets allow this type of automation by providing convenient client wrappers around the Azure management REST API”

Authentication library for Azure Active Directory: The Active Directory Authentication Library (ADAL)

Panel discussion on desktop development: is there a future for WPF? Maybe some clues here.

Miguel de Icaza gets a session on going mobile with C# and Xamarin. I recall when de Icaza ran sessions on Mono, the open source implementation of the .NET Framework which he initiated shortly after Microsoft announced .NET itself, in nearby hotels at Microsoft events; now he is inside.

Learning from the mistakes of Azure: Mark Russinovich on what can go wrong in the cloud.

Looks like both cloud and apps for Windows Phone/Windows 8 are big themes at Build this year.

Entering Microsoft’s XAML labyrinth: is it worth it?

I spent some time at the weekend working on a Bridge game for the Windows Store. I am writing it in XAML and C#. The UI is hardly demanding, given that Bridge is a card game, but it has made me take a fresh look at XAML, the markup language for a Windows Store App user interface (unless you use HTML and JavaScript). XAML is also used in Windows Presentation Foundation and in Silverlight/Windows Phone.

As part of the game, the user selects a “bid” which consists of a number from 1 to 7 and a suit of cards (or double, redouble or pass). Most bridge games show this as a grid though functionally it is like a combo-box (choosing from a pre-defined range of options).

Naturally I looked for the easiest way to accomplish this. The solution I came up with was to nest TextBlock controls in Border controls in Grid cells. Then I wrote C# code that detects which cell the user taps and updates the background of the selected Border accordingly.

image

I have in mind to replace the text with graphics and make the numbers a bit smarter at some future date. My solution works fine; here it is at runtime:

image

At the weekend I happened to be chatting with a developer more expert in XAML than myself, who told me I had done it wrong. In XAML everything should be in Style definitions. I should use a ListView and design it in Blend.

Well, I knew that I had somewhat subverted how XAML is meant to work, so I sat down to investigate this different approach. A ListView looks nothing like what I want out of the box.

image

However, with the magic of XAML it can be transformed. I made the ListView horizontal by defining an ItemsPanelTemplate in Application.Resources:

<ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
  <StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>

and adding 

ItemsPanel="{StaticResource ItemsPanelTemplate1}

as an attribute of the ListView.

Then I added an ItemTemplate to draw the kind of block that I wanted:

<ListView.ItemTemplate>
    <DataTemplate>
        <Border BorderThickness="1" Height="130" Width="130" BorderBrush="Black">
            <TextBlock FontSize="24" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center"
                       Text="{Binding Name}" Foreground="Black" />
        </Border>
    </DataTemplate>
</ListView.ItemTemplate>

Note that I am using a DataTemplate because the ListView is bound to an ObservableCollection in the proper XAML way.

At this point I am close to what I want – never mind that the numbers are missing, they can easily be added with a second ListView:

image

However I do not want that little tick mark appearing, the selected background colour is not to my taste, and the spacing of the items is wrong. How do I fix that?

My search led me to this post which explains a far-from-obvious series of steps you can take in Blend. The steps did not quite work for me but got me on track to create a new Style resource which I called ListViewItemStyleNoGlyph and which lets me adjust the margin and also a previously hidden property called SelectionCheckMarkVisualEnabled. Blend generated a substantial block of code for this:

image

This has helped and now my ListView looks like this:

image

Well, it is nearly there and I can see that with a bit more effort I can get what I want. Even so, I am beginning to wonder whether my initial approach, in which I understood all the code, had advantages over this exploration into the labyrinth.

Is XAML well loved out there? I came across this post by Paul Stovell from a couple of years back which seems relevant. “I’ve lived and breathed the technology for the last six years”, he says, but writes:

What’s disappointing is that WPF started out quite positively during its time. Concepts like dependency properties, styles, templates, and the focus on data binding felt quite revolutionary when Avalon was announced.

Sadly, these good ideas, when put into practice, didn’t have great implementations. Dependency properties are terribly verbose, and could have done with some decent language support. Styles and templates were also verbose, and far more limited than CSS (when WPF shipped I imagined there would be a thousand websites offering high quality WPF themes, just like there are for HTML themes; but there aren’t, because it is hard).

Data binding in WPF generally Just Works, except when it doesn’t. Implementing INotifyPropertyChanged still takes way too much code. Data context is a great concept, except it totally breaks when dealing with items like ContextMenus. ICommand was built to serve two masters; the WPF team who favored routed commands, and the Blend team who favored the command pattern, and ended up being a bad implementation for both.

Stovell mentions the verbosity of XAML, and that it is hard, both of which sound right to me. He contrasts the way ASP.NET has evolved, with ASP.NET MVC a great improvement on web forms. Read the full post for more detail.

It seems to me that XAML does offer much that is wonderful: flexibility, capability, and the ability to separate presentation from data. At the same time, neither XAML nor Blend are intuitive tools for developers; they may make more sense to designers, but it seems to me that removing a tick mark from a ListViewItem should be more straightforward. Perhaps it is, in which case there is a failure of documentation or tooling rather than functionality, but it makes little difference to the developer.

How to crash your Windows Store XAML app

I am working on a Windows Store app, of which more soon. I am writing the app in XAML and C#. I was tweaking the page design when I hit a problem. Everything was fine in the designer in Visual Studio, but running the app raised an exception:

image

WinRT information: Failed to create a ‘Windows.Foundation.Int32’ from the text ‘ 2’.

along with the ever-helpful:

Additional information: The text associated with this error code could not be found.

The annoying this about this error is that debugging is not that easy. The exception is in Framework code, not your own code, and Microsoft does not supply the source. Once again, everything is fine in the designer and there are no compiler errors.

Puzzling. I resorted to undoing bits of my changes until I found what triggered the problem.

This was it. In the XAML, I had somehow typed a leading space before a number:

Grid.Row=" 2"

The designer parses this OK (it would be better if it did not) but the runtime does not like it.

Actually, I know why this happens. If you are typing in the XAML code editor (which I find myself doing a lot), then auto completion inserts the blank space for you:

image

I wish all bugs were this easy to solve, though I regard it as a bug in the Visual Studio editor. Posted here mainly in case others hit this problem; but I also observe that Windows Store development still seems less solid in Visual Studio than the tools for desktop or web apps.

Other problems I have hit include the visual designer changing to read-only of its own accord; and a highly irritating issue where the editor for a XAML code-behind class sometimes forgets the existence of all the controls you have declared in XAML, covering your valid code with red squiggly lines and reporting numerous errors, which disappear as soon as you compile. Once this starts happening, the problem persists for the rest of the editing session.

It is not all bad. I am pleased with the way I have been able to put together a touch-friendly game UI relatively easily. Now comes the fun part: writing the logic for the AI (Artificial Intelligence).