Wednesday, January 23, 2013

Windows Store Apps with F# (part 1) : Make things as simple as possible, but not simpler.


During the initial phase of the Windows 8 beta period it was not possible to integrate F# code in a Windows 8 store app. After the final release of Visual Studio 2012 it is possible to create hybrid projects. In this series of posts I want to show you some of the projects I use to experiment with F# and Windows App Store integration.
I will focus on the interoperability and assume that you are able to find resources to learn the details of XAML, Windows 8 store apps and F#.

In the first example we create a simple solution and just add some F# code.
The F# code will determine if a number is a prime number. The App UI should have a text box to enter a value, a button to start the check and a text block to display the result.
We start by creating a blank windows store app:
CreateProject
Open the created main page
MainPage
and start designing the layout of the page.
    <Page.Resources>
<
Style x:Key="FSharp" TargetType="TextBlock" BasedOn="{StaticResource BasicTextStyle}">
<
Setter Property="FontFamily" Value="Verdana"/>
<
Setter Property="FontSize" Value="32"/>
</
Style>
</
Page.Resources>

Next we change the color of the grid and add some columns and rows:
 <Grid Background="#FF8F0A0A">
<
Grid.ColumnDefinitions>
<
ColumnDefinition Width="10*"/>
<
ColumnDefinition Width="200"/>
<
ColumnDefinition Width="100"/>
<
ColumnDefinition Width="300"/>
<
ColumnDefinition Width="20*"/>
</
Grid.ColumnDefinitions>
<
Grid.RowDefinitions>
<
RowDefinition Height="10*"/>
<
RowDefinition Height="50"/>
<
RowDefinition Height="100"/>
<
RowDefinition Height="50"/>
<
RowDefinition Height="20*"/>
</
Grid.RowDefinitions>

And finally add the controls to the grid:

  <TextBlock Grid.Row="1" Grid.Column="1"
Text="Input value:"
Style="{StaticResource FSharp}"/>

<
TextBox Grid.Row="1" Grid.Column="3"
x:Name="inputValue"
FontFamily="Verdana" FontSize="32" />

<
Button Grid.Row="2" Grid.Column="1"
Grid.ColumnSpan="3"
Content="Is it a prime?" x:Name="calculate"
HorizontalAlignment="Center" VerticalAlignment="Center"
Foreground="#FFE68484"
FontFamily="Verdana" FontSize="32"/>

<
TextBlock Grid.Row="3" Grid.Column="1"
Text="Result:"
Style="{StaticResource FSharp}"/>

<
TextBlock Grid.Row="3" Grid.Column="3"
x:Name="result"
Style="{StaticResource FSharp}" />



This is page in de designer:



Designer

Now is time to add the F# code. We add an F# Portable Library call “MathLib” to the solution.

FSharpProject


Rename PortableLibrary1 to Lib and delete the Script file.

FSharpProject2

And add the F# code to the Lib file:
module MathLib.Prime
let IsPrime n =
if n < 2L then
false
else
seq
{ 2L .. n - 1L}
|> Seq.exists(
fun x -> n % x = 0L)
|> not

This is not the most sophisticated prime check but it will do for our projects.

We have created a module we can use MathLib is the namespace, Prime is a static class and IsPrime is a static function.

The code i in line with the definition of a prime number: A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Now we have to create interoperability. We add a reference to the MathLib project:

AddReference

and build the solution. This will make the meta data of the MathLib project available for the  WinRTFSharpSimple project.

Next we create add a button click event:
 <Button Grid.Row="2" Grid.Column="1"
Grid.ColumnSpan="3"
Content="Is it a prime?" x:Name="calculate"
HorizontalAlignment="Center" VerticalAlignment="Center"
Foreground="#FFE68484"
FontFamily="Verdana" FontSize="32" Click="OnClick"/>

and add an handler at the code behind:

private voidOnClick(objectsender,RoutedEventArgs e)
{
    result.Text =
Prime.IsPrime(Convert.ToInt64(inputValue.Text)).ToString();
}


and add a namespace:

OnClick

Time to test the app, F5:

screenshot_01232013_200741

screenshot_01232013_200947

This looks fine.

In case we enter 756771235126757131

screenshot_01232013_201358

the app freezes and we can not interact with the app (issue 1).

In case we enter “abc” the app crashes (issue 2).

How could we solve these issues?

We can fix them at the C# site or we could fix the at F# site, both are possible. In the next post I will discuss a solution at the F# site.

Monday, January 30, 2012

Dutch article: Monads voor de .NET-ontwikkelaar is available at the SDN site

I have written an article for the Dutch developer magazine of SDN (Magazine 111). It is titled: “Monads voor de .NET-ontwikkelaar”. It is now on line: http://www.sdn.nl/SDN/Artikelen/tabid/58/view/View/ArticleID/3206/Monads-voor-de-NET-ontwikkelaar.aspx.

I have tried to read the English version by Google translate. This is not a very good translation. I do not recommend to read it this translation. The German version is even worse. It contains Engels words that are not part of the Dutch version.

Tuesday, October 4, 2011

BCL Team Blog provides more information on code reuse (Portable Libraries part 4)

Today the BCL Team Blog addressed the issue of code reuse: Porting existing .NET code to Metro style apps

"One of the things we know people want to do is reuse existing .NET code but due to the constraints on Metro style apps it is not possible to directly reuse existing .NET class libraries nor is it always possible to simply recompile existing code. We realize this is going to be a pain point for many current .NET developers so we are working on a guide at the .NET for Metro style apps overview page which will assist developers in translating their existing .NET skill set into the Metro style apps world.”

Bringing existing managed code into Metro style apps" by Daniel Plaisted is a recommended resource.

Daniel ports a windows phone app to a Metro app. Most of presentation is mainly about porting the UI.

Some slides from the presentation:

move_code

differenes

Getting the pictures from a local folder.

folders

There is no database access.

Wednesday, September 28, 2011

.NET portability is not an afterthought (adding another layer of indirection - Portable Libraries part 3)

Before //build/ I was not aware of the Portable Class Libraries Project. The origin of the project is the notion that the number of platform that provide .NET support has increased from one to four and that this number will increase in the future. Making .NET available on more platforms is the future of .NET.

The Portable Class Libraries Project was the first attempt of solving the portability issue but it ran into some constraints. One of them was the increasing complexity: “Portability is an afterthought”.

After watching some of the presentation at //build/ and experimenting with Visual Studio Express for Windows Developer Preview I thought that the whole purpose of creating a portable library was to bridge the gap between .NET and the WinRT.

ClassLibrary

This is not the case. Yesterday I watched .NET 4.5: Portable Libraries by David Kean and Mircea Trofin at Channel 9. It all made more sense to me.

 

The show:

  • The first 15 minutes explains portability and the first version of the Portable Class Libraries Project.
  • 15:00 whiteboard time.
  • Explaining the first way to solve portability problem:
  • wb1
  • wb2
  • Explaining the problems with System.NET
  • wb3
  • 22:00 the new solution: one portable library by design.
  • Portability is not an afterthought
  • 28:00
    • You program against a contract
    • It is up to the platform to implement the contract
    • Type forwarding:
      wb5
    • You program against the exposed api.
    • wb4
    • And the GAC will forward to dll that implements the actual code.
    • 33:00 Portability is a first class citizen
  • 35:00 It is about granularity a method is too small a dll is too big.
  • 36:00
    • Q: Are there two gacs in the Windows 8 world?
    • A: there is one gac. The metro mode is under “us”.
  • 37:00 breaking changes:
    • Type
    • wb6
    • wb7
  • 45:00 Close and Dispose. Close is disposed, no more closing just disposing.
  • They reference: A .NET developer's view of Windows 8 app development by Krzysztof Cwalina
  • 50:00 .NET has a great future with portability in mind.

So it makes sense to fix and recompile your legacy libraries. You can port them to all platforms that will have .NET available.

Thursday, September 22, 2011

Reuse of legacy software by WinRT and Metro (Windows 8) (Portable Libraries part 2)

 

This is a follow up to my previous post Create a Metro App in F# (NOT).

Things become quit complex if you want to reuse your windows legacy software. In this case legacy has a positive connotation. Legacy software is an asset of you and/or your company.

Suppose one want to reuse it and make it available for a Metro app on Windows 8 what are your options? After watching some of the build presentations:

 

I came up with the following flow chart:

 

LegacyWinRT_4

So:

  • You can reuse html, css and JavaScript.
  • I am not an expert on C++, COM, etc. so I do not know.
  • If you have only a dll you cannot use it.
  • If you have source code that references a language dll (like F#) or have no options today. This will change when the final product becomes available.
  • If you want to create a Metro Style project in C++ or JavaScript you can reuse your components code by fixing issues and recompile it to a WinMD file (WinRT component). Some issues can be exposing:
    • non WinRT types
    • non sealed classes
    • non system provided generic types
  • If you want to create a Metro Style project and this is one of the .NET languages you can reuse your components code by fixing issues and recompile it to a dll. Some issues can be:
    • Streams

 

Disclaimer: This is my current understanding, based on the presentations I viewed. If you have other information available please leave a note in the comments.

Wednesday, September 21, 2011

Create a Metro App in F# (NOT) (Portable Libraries part 1)


update January 24, 2013: you are now able to create “Metro apps”: Windows Store Apps with F# (part 1) : Make things as simple as possible, but not simpler.


Today you can follow the all-F# approach (Build MVVM Applications in F#). This is not the easiest way to build a solution, it is not the preferred way, but it is an option.
Don Syme presented last Friday in his build talk the following slide.
FSharpMetro
When he explained this it was not completely clear to me what this meant.
Does this tell us that we cannot create a F# Metro front-end today but we will when F# 3.0 is ready? Or does this mean that you cannot create a F# 3.0 project and reference it from a C# or a VB Metro front-end app in one solution?
Today I watched some other presentations of day 4 of build:
Windows Runtime internals: understanding "Hello World" by Matt Merry
Being pragmatic by leveraging existing code in Metro style apps by Jason Olson
A .NET developer's view of Windows 8 app development by Krzysztof Cwalina
Ten Tips When Writing a Hybrid Language Metro style Application by Brent Rector
Using the Windows Runtime from C# and Visual Basic by  Harry Pierson and Jesse Kaplan
and experimented with Visual Studio 11 (Developer Preview and Express for Windows Developer Preview).
It became clear to me that you cannot create a Metro style solution that references a project that references .NET dll's that are not part of the .NET Profile for Metro style apps.
requirements
profile
The F# dll is one of them today. So creating a Metro app in C#/VB/C++/JavaScript that references a F# project is not possible.
This should be possible when F# 3.0 becomes available. Will it be possible to create an all-F# Metro app in the F# 3.0 time frame? For me, this is still an unanswered question.
WinRt
Another consequence of this approach: when you intend to reuse your .NET dll's you have to recompile your code as WinMD files. So you need to own the source code. WinMD
This are the restrictions:
Restrictions
Update(September 21, 2011):
Jason_Olson

Total Pageviews