Speaking about The future of Windows 8 desktop application


After unforgettable week in Redmond on MVP Summit 2012, it is time to announce some of my speaking activity. The first conference for this year will be MSNetwork 2 domestic Microsoft conference, which I will be speaking on two sessions. The first session will be Development with ASP.NET Web API and MVC 4.0, together with Damir Dobric , and the second session will be The Future of Windows Desktop Application.

The first session will talk about a new Microsoft project Asp.NET Web API, firstly announced as WCF Web API, which I blog posted already. The reason for changing the name of the API is in short because  WCF is far more than HTTP Web API, so Microsoft team has decided to change the name and include the library in to ASP.NET as a part of the MVC 4.0 beta.

On that way the ASP.NET Web API is finally included in to .NET Framework and ready to use in production when the .NET 4.5 would be released.Damir has also posted several blogs about ASP.NET API so if you want more info about, you can find there. This session brings a lof of demos and samples how to use Web API with routing,using HTML methods GET, POST PUT, standard and custom formatters as well as validators and custom errors. If you are Web developer, or just want to see how to build modern web application with MVC 4.0 and .NET 4.5 come to our session which will start after launch at 14:30 at 4. 4. 2012.

The Future of Windows Desktop application brings a lot of news about Windows 8 Metro style application, like compatible by design, Windows Store and Certification. Windows 8 brings the new future for desktop application and new way of using, developing and promoting desktop applications. Windows 8 also brings a new platform for desktop application, it is Tablet. So when you develop Windows application you can count on different devices like PC or Tablet, and far more different resolution. This is only scratch of the session about new future of the Windows desktop application and technique for development.

We can say the new future is born for Windows Desktop application. The session starts after launch at 14:30 at 5. 4. 2012.

More info about MS Network you can find here.

So much said about introductio of my two sessions on the Network 2 in Mostar. See you there.

Windows 8 Metro Tips and Tricks


Windows 8 Metro Style

It is important to know some key shortcats in order to be productive and efficient when working in Windows 8 specially in Metro style Mode. Since any Metro App does not have Exit option it is important to know how to return back or how to log off or turn off our PC.

Since there is no Exit command in Metro, applications cannot be exited in the traditional way. In fact every Metro style app has three states Active- when it is showed on screen, Suspended- when it is in background and Destroyed- when OS kill the process of your app. When other Windows 8 Metro app is launched, the previous application goes off the screen and becomes suspended. After about 5 seconds application goes to transient state. It is important to know that suspended apps are not removed from memory, but all their threads are suspended. On that way suspended application can be activated and showed on the Metro screen immediately. On the other hand suspended apps don’t consume energy or they consume energy in minimum amount. Suspended apps can be terminated of a low memory condition. This is the case when the application is relaunched as a new run. During relaunching it can be implemented so that the app can restore its state from persistent storage and appear to the user as if it has never been gone.

At the end of this blog post let’s review the shortcuts for some action in Windws 8 Metro:

  • winkey – switch between Desktop and Metro mode
  • winkey+L – Log off from the Window current session
  • winkey+F – Open Metro Search engine for searching different content: Apps, files, settings, etc
  • winkey+P – Change Monitors layout.
  • winkey+any – Starts searching for content.
  •  winkey+ESC – Back to Desktop mode (Windows 7++).
  • winkey+i –    Setting of Desktop, Power, Audio and Network.
  • winkey+o – Lock screen rotation.
  • winkey+d –  Whos Desktop mode (Windows 7++)
  • winkey+h –  Open the share charm
  • winkey+k –   Open the connect share charm
  • winkey+TAB – Toggles between opened apps
  • winkey+SPACE – Toggles input language and keyboard
  • winkey+c –   Open the “charms bar” (share, settings, shutdown)
  • Mouse RClick on App Tile – shows the App Bar with advanced option.
  • ALT+TAB – Switch between apps.
  • Press any key – Unlock screen
  • CTRL+SHIFT+ESC – force to close metro app, since there is no Exit option in app.

Consuming WCF in Windows 8 Metro style app


Asynchronous programming is introduced in a new version of programming languages (C++, C# and JavaScript) which are part of the WinRT and .NET. If you are programming in C# you can program asynchronous by using async and await keyword. In fact when you program with async and await you actually program as ordinary synchronous code. That’s the big thing about this type of programming model.
More information about C# asynchronous programming you can found in my previous posts.
Since we have asynchronous programming model in all languages calling service operations asynchrony is not relevant at all. We can just forget it and start writing code like synchronous. To see hot new pattern is reflected in WCF lets implement simple service and simple metro style app consumer.
Implementation of the WCF Simple Service:

  • Create new ASP.NET Empty Web App
  • Add new Item-> WCF Service
  • Name it as SimpleService.svc


Service Interface looks like the following implementation:

[ServiceContract]
public interface ISimpleService
{
    [OperationContract]
    [WebGet(UriTemplate="AllData",ResponseFormat= WebMessageFormat.Json)]
    List<string> GetSomeData();
}

Implement one operation in the service like the following code implementation:

public class SimpleService : ISimpleService
{
    public List<string> GetSomeData()
    {
        List<string> lst = new List<string>();
        for (int i = 0; i < 10; i++)
        {
            System.Threading.Thread.Sleep(100);
            lst.Add(string.Format("Item number {0}", i + 1));
        }
        return lst;
    }
}

The mentioned operation returns list of string but between populating the collection we sleep the current thread for 0,5 second in order to simulate log running operation.

It is necessary to publish the WCF service on remote host, in order to consume it. I still dont know why WCF service cannot be consumed by Metro app on the localhost. After the service is published on remoter host, we are ready to create Metro style Consumer app.

  • Right click on solution item
  • Add New Project
  • Choose Application template from the Metro Style app
  • Name it WCFConsumer

  • Add service reference to C# Metro Stryle app.
  • Add the GridView and some other input controls (buttons, editbox , etc) that we can test application responsiveness. Add the following XAML code in MainPage.xaml:
<Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="151*"/>
        <ColumnDefinition Width="326*"/>
        <ColumnDefinition Width="207*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="74*"/>
        <RowDefinition Height="185*"/>
        <RowDefinition Height="68*"/>
    </Grid.RowDefinitions>
    <GridView x:Name="gridData" Grid.Column="1" Grid.Row="1" />
    <HyperlinkButton Content="HyperlinkButton"   Grid.Column="1" HorizontalAlignment="Left" Height="25" Margin="58,30,0,0" VerticalAlignment="Top" Width="126"/>
    <Button Content="Button" Grid.Column="1" HorizontalAlignment="Left" Height="40" Margin="69,8,0,0" Grid.Row="2" VerticalAlignment="Top" Width="182"/>
    <TextBox Grid.Column="2" HorizontalAlignment="Left" Height="27" Margin="21,79,0,0" Grid.Row="1" Text="TextBox" VerticalAlignment="Top" Width="161"/>
    <TextBox HorizontalAlignment="Left" Height="15" Margin="24,79,0,0" Grid.Row="1" Text="TextBox" VerticalAlignment="Top" Width="102"/>
    <Image Grid.ColumnSpan="2" Grid.Column="1" HorizontalAlignment="Left" Height="100" Margin="312,240,0,-272" Grid.Row="2" VerticalAlignment="Top" Width="100"/>
</Grid>

The picture below shows the example ot the mainPage.

  • Implement Loaded event in to MainPage.xaml and insert the following code.
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
        SimpleServiceNamespace.SimpleServiceClient client = new SimpleServiceNamespace.SimpleServiceClient();
        var servicedata = client.GetSomeDataAsync();
        gridData.ItemsSource = servicedata.Result;
}

Even if GetSomeDataAsync method is asynchronous we don’t implement callback because this operation is implemented based on the async and await C# keyword and no need to do that.
Just assign the result of the servicedata to the GridView.ItemsSource.
If we run the app, we can see that the data is loaded asynchronously and UI is not blocked. We can click on buttons, tipe some texts while data is loading. This is awesome and now we can think about application logic instead of callback service implementation. This programming pattern works not just for Metro style apps, in fact it works for all .NET 4.5 based applications.