Live Fast, Code Hard, Die Young

Posts tagged ‘.net’

My impressions from MIX10

image I’m finally back home after a crazy and wonderful week visiting MIX10 in Las Vegas. It was a great conference and I really had a good time watching the sessions and meeting people. I got the chance to talk to Brad Abrams, Nikhil Khotari and Colin Blair about WCF RIA Services, chatted with the devs working on Bing, talked to Pete Brown about his cool C64 emulator built in Silverlight, met Roland Weigelt who created the wonderful GhostDoc add-in for Visual Studio and got a tip about Sonic File Finder from his friend. All in all the atmosphere was great and a lot of the speakers and people from Microsoft were hanging around chatting with people all the time which I think was great.

So what impact did MIX10 had on me personally? Well, after doing some thinking I want to present the five most important things that made an impression on me:

1 – Windows Phone 7

image

Microsoft is really pushing the new version of their mobile phone OS. Yeah, they have stolen a lot of ideas from Apple but it’s not the first time Microsoft does that. Copying a successful concept is also a way of making business. However, they’ve introduced some really great innovations as well, improved some of the shortcomings of the competitors and I must say that the phone UI feels really slick and modern. I could actually see myself using this instead of my Iphone! 🙂

What made me really interested in the phone is the fact that it seems so simple to get started with developing for it. Since it runs Silverlight it is really easy to get an application up and running if you’re familiar with .NET development. This is radically different from developing for the Iphone or Android for me. It also supports writing applications in XNA which is a great framework for developing more advanced applications like games.

Additionally Microsoft has released the tools for free so you can build apps with no up-front investment at all. That’s a pretty nice deal for such great tools as Visual Studio and Blend.

http://developer.windowsphone.com/

2 – OData

image The Open Data Protocol (or OData for short) is a pretty awesome concept that Microsoft presented at MIX10.

Many successful internet businesses today enable their users a rich web API that is used to access their service in many different ways with various clients. The web browser is only one type of client in a larger ecosystem of mobile phones, desktop applications and other devices that wants to work with data over the web. Twitter is an example of this, where they only provide a rudimentary interface on the website and the entire experience is greatly enhanced by a large variety of clients available for different platforms.

OData is a REST based API that aims to be a standard way of accessing services over the web. Microsoft enables you to easily expose an OData interface from your ASP.NET application as well as client libraries for consuming OData from various environments. They obviously provide a .NET and WP7 client libraries, but surprisingly they have developed libraries for accessing OData also from PHP, Java and Iphone. That’s really neat!

Another cool thing is a service that goes by the name Codename “Dallas”. This is Microsoft’s solution for people to expose and make money off their data. Even if you’re a small one man business you can take advantage of this and make money if you have something interesting to share. One guy I met in the “RIA Services suite” had a lot of historical baseball data that he wanted to share and I think he could make some money with data.

Check out the keynote from day 2 at about 59:00 for a demo of this stuff:

http://live.visitmix.com/MIX10/Sessions/KEY02

3 – Azure

I saw some demos of Azure at MIX10 and it is starting to look really smooth. I haven’t really had the time to try it out yet but it is looking more and more compelling. Previously I’ve heard that it’s been quite instable and buggy but it looks like that has been straightened out. Maybe it is finally time to start building stuff for the cloud now! 🙂

I really like that you can test your stuff out in the “DevFabric” which is a simulated cloud environment on your dev machine. Then you can for example start by moving up your data storage to the cloud while still running the code on your dev machine and continue testing your solution, and when you’re ready you move it all up into the cloud. It all seemed really simple and all you need to do to get started is to install the Azure SDK which is available from here:

http://dev.windowsazure.com/

4 – IE9

image Microsoft is really taking a leap forward with version 9 of Internet Explorer. IE has lost a lot of users the past years and Microsoft is really committed to improving its performance and standards compliance. It was interesting to see a demonstration of cases where the new IE version really shines in a head to head comparison with other browsers, especially Google Chrome (which is one of the fastest browsers at the moment). Competition like this is really good for the browser market. At the same time Microsoft is really trying to help out its competitors by setting up this website with test cases that they can run to check their performance.

Some notable things about IE9 is the GPU accelerated HTML5 and better standards compliance, for example with rounded and dotted CSS borders…Examples can be found at this address: http://www.ietestdrive.com/

Also check out the keynote from day 2 where they start off talking about the new IE9 engine: http://live.visitmix.com/MIX10/Sessions/KEY02

5 – MVVM

The Model-View-ViewModel design pattern has been around for a while but it is now finally gaining more and more attention among Silverlight developers and most importantly in the tools coming from Microsoft. The new version of Blend will have some really useful features for working in a MVVM oriented way like for example being able to generate sample data based on a ViewModel class in your project.

I watched a couple of great sessions about MVVM at MIX. If you want to learn more you can check out the introductory session by Laurent Bugnion:

http://live.visitmix.com/MIX10/Sessions/EX14

I also saw the session by Rob Eisenberg and was deeply impressed by his elegant solutions and ideas:

http://live.visitmix.com/MIX10/Sessions/EX15

Conclusion

Okay, so that’s a list of things that I found most important at this conference. Of course, there was a lot more going on so I recommend that you take a look at all the videos that have been released and find the things of interest to you:

http://live.visitmix.com/Videos

Feel free to comment if you think I’ve missed something significant!

Advertisements

Getting the base domain URL from the Uri class

If you’ve used the System.Uri class in .NET you may have stumbled upon the need to extract the base part of the URL and found no obvious solution. You know, you have a URL that looks like this:

http://www.mydomain.com/somepath/somepage.aspx?param=x

And you want to get only this part:

http://www.mydomain.com

If you put the long URL in a Uri object you would expect to be able to get the domain name part via a simple property but as you look through the class you won’t find it. There is a property called Authority for getting “www.mydomain.com” but then you need to concatenate that with the Scheme property which contains “http” and add “://” in between them to get a valid URL. Looks quite cumbersome to me.

I seem to hit this problem every once in a while and I keep forgetting how to do it properly. To me this indicates that the class is missing something obvious. Actually it has a method for getting what I want, and it is called GetLeftPart().

The GetLeftPart() method takes a UriPartial enum that describes how much of the URL to return. If we pass “Authority” to it we get what we want!

Interesting solution
Now I wouldn’t have blogged about this if the GetLeftPart() method was the end of it all. What I really wanted was an easy way to access the base URL and ideally that would be via a property. To achieve this we can create our own class that derives from Uri and add the property there.

Another interesting solution that I found is to add an extension method to the Uri class. Unfortunately there are no extension properties (yet) so we will have to settle for a method called GetBaseUrl(). Here is some code to do it:

namespace System
{
    public static class Extensions {
        public static string GetBaseUrl(this Uri uri)
        {
            return uri.GetLeftPart(UriPartial.Authority);
        }
    }
}

Preserving transparency when converting icon to bitmap

Recently I wanted to show shell icons in a custom control on a Windows form. The custom control only supported bitmaps so I needed to convert the icons into bitmaps. This is quite easy in .NET since there is a nice Bitmap.FromHIcon() method. The problem is that when you use this method the icon transparency is lost, at least when you use it to extract shell icons (using the SHGetFileInfo API).
 
I tried to search for a solution but wasn’t able to locate any so I decided to fiddle around a bit on my own. Here is the way I came up with that works.
 

SHFILEINFO fi;

SHGetFileInfo(filename, 0, out fi, Marshal.SizeOf(typeof(SHFILEINFO)), FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_SMALLICON);

using (Icon icon = Icon.FromHandle(fi.hIcon)) {

    Bitmap bm = new Bitmap(icon.Size.Width, icon.Size.Height);

    using (Graphics g = Graphics.FromImage(bm))

        g.DrawIcon(icon, 0, 0);

    return bm;

}

 

In other words, the solution is to draw the icon on an existing bitmap. The draw method will consider the transparency information when drawing which is what we want.
 
Here is the full class for retrieving a shell file icon as a bitmap:
 

public class FileInfoHelper {

 

    struct SHFILEINFO {

        public IntPtr hIcon;

        public int iIcon;

        public int dwAttributes;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]

        public string szDisplayName;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]

        public string szTypeName;

    }

 

    [Flags]

    enum FileInfoFlags : int {

        SHGFI_ICON = 0x000000100,    // get icon

        SHGFI_DISPLAYNAME = 0x000000200,    // get display name

        SHGFI_TYPENAME = 0x000000400,    // get type name

        SHGFI_ATTRIBUTES = 0x000000800,    // get attributes

        SHGFI_ICONLOCATION = 0x000001000,    // get icon location

        SHGFI_EXETYPE = 0x000002000,    // return exe type

        SHGFI_SYSICONINDEX = 0x000004000,    // get system icon index

        SHGFI_LINKOVERLAY = 0x000008000,    // put a link overlay on icon

        SHGFI_SELECTED = 0x000010000,    // show icon in selected state

        SHGFI_ATTR_SPECIFIED = 0x000020000,    // get only specified attributes

        SHGFI_LARGEICON = 0x000000000,    // get large icon

        SHGFI_SMALLICON = 0x000000001,    // get small icon

        SHGFI_OPENICON = 0x000000002,    // get open icon

        SHGFI_SHELLICONSIZE = 0x000000004,    // get shell size icon

        SHGFI_PIDL = 0x000000008,    // pszPath is a pidl

        SHGFI_USEFILEATTRIBUTES = 0x000000010,    // use passed dwFileAttribute

        SHGFI_ADDOVERLAYS = 0x000000020,    // apply the appropriate overlays

        SHGFI_OVERLAYINDEX = 0x000000040,    // Get the index of the overlay in

        // the upper 8 bits of the iIcon

    }

 

    [DllImport("shell32.dll", CharSet = CharSet.Auto)]

    static extern Int64 SHGetFileInfo(string pszPath, int dwFileAttributes, out SHFILEINFO psfi, int cbFileInfo, FileInfoFlags uFlags);

 

    /// <summary>

    /// Creates a new bitmap with the small icon for a file.

    /// </summary>

    /// <param name="filename"></param>

    /// <returns></returns>

    public static Bitmap GetFileIconAsBitmap(string filename) {

        SHFILEINFO fi;

        SHGetFileInfo(filename, 0, out fi, Marshal.SizeOf(typeof(SHFILEINFO)), FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_SMALLICON);

        using (Icon icon = Icon.FromHandle(fi.hIcon)) {

            Bitmap bm = new Bitmap(icon.Size.Width, icon.Size.Height);

            using (Graphics g = Graphics.FromImage(bm))

                g.DrawIcon(icon, 0, 0);

            return bm;

        }

    }

}