Sunday 22 December 2013

Microsoft Technology Summit 22nd October, 2013 Warsaw

       I wanted to post a short summary of the Polish MTS and my experience of it since just after the MTS. Finally found some time to go through the lectures again and do a small write-up. I've heard several bad reviews of the previous events in Warsaw, about how they were nothing more than a marketing event and how badly planned they were. I was going to see the event with slightly mixed feeling but also a wanting to see it all by myself. Contrary to what I heard earlier the event was really interesting and I enjoyed nearly all sessions I selected for myself.

The lectures:

- Async in practice by Jakub Binkowski
   I've seen several lectures on this topic and I was wondering whether I can get anything more from seeing        another one. The lecture was really good and focused on mistakes often made in multi-threaded                    programming using async and await and how difficult they are to diagnose.

- Adventures in the underland: what passwords do when no one is watching by Paula Januszkiewicz
   Very entertaining session, Paula speaks with passion and great fluency, adds a bit of humour to every            topic. I haven't seen a better Polish speaker yet. The session touched on different ways of grabbing a            password off of unsuspecting users. Although some of it seemed to be added just to wow the audiance.

- What's New in Visual Studio 2013 for Application Lifecycle Management by Brian Keller
   I didn't read, listen or watch much on VS 2013 earlier, so this was definitely an interesting session for me.      It was about the project and task management mechanisms added to VS. I really liked how you could          connect and synchronize your tasks with lines of code, and do your code reviews - facebook style.                Practically it's like adding a little jira / trello to your source code. These were just some of the many                features presented.

 - Modular Javascript Typescript code by Maciej Grzyb
   A really informative session about using type script for creating more maintainable javascript code by            introducing namespaces. I wanted to take a look at either typescript or coffeescript for some time already,    but could never find the time to. This was a very good introductory session for me despite it focusing on        namespaces.

 - Entity Framework 6 - open access for all by Piotr Bubacz
   Quite a typica tech walkthrough, with some of the hurdles of older EF versions finally resolved.

Overall it was a pretty good conference. I'm still hoping though one day we will have a conference of the NDC caliber in Poland.

Sunday 15 December 2013

Adding Drag and drop behaviour to ItemsControls Part 2

     It's been some time since I last posted something, and the drag and drop behaviour tutorial completion was sitting all this time on a back burner. It is high time to finish it. I'd like to start with a slight refactoring of the code I posted previously for IsDropSource property change callbacks, and another piece of code for IsDragTarget property change callback.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Adds passed ItemsControl to the drag sources collecton
private static void IsDragSourceUpdated(DependencyObject dp,
    DependencyPropertyChangedEventArgs args)
{
    var isDragSourceEnabled = (bool)args.NewValue;
    var dragSource = dp as ItemsControl;
               
    if (isDragSourceEnabled)
    {
        Add(dragSources, dragSource);
        dragSource.PreviewMouseMove += OnMouseMove;
        dragSource.PreviewMouseLeftButtonDown += OnLeftButtonDown;
    }
    else
    {
        Remove(dragSources, dragSource);
        dragSource.PreviewMouseMove -= OnMouseMove;
        dragSource.PreviewMouseLeftButtonDown -= OnLeftButtonDown;
    }
}

// Adds passed ItemsControl to the drop targets collection
private static void IsDropTargetUpdated(DependencyObject dp,
    DependencyPropertyChangedEventArgs args)
{
    var isDropTargetEnabled = (bool)args.NewValue;
    var dropTarget = dp as ItemsControl;

    dropTarget.AllowDrop = isDropTargetEnabled;            

    if (isDropTargetEnabled)
    {
        Add(dropTargets, dropTarget);
        dropTarget.Drop += Drop;
    }
    else
    {
        Remove(dropTargets, dropTarget);
        dropTarget.Drop -= Drop;
    }         
}

The code initializes the ItemsControl adorned with those properties in xaml as a new drag source or drop target. For that to be possible it adds the control to particular collections and adds required events. Beside the obvious mouse move and drop events we need to remember about servicing the Loaded and Unloaded events to add or remove the control from the drag and drop when the View is being either opened or closed.
Part of the above execution is further contained in following Add and Remove methods:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Adds item control to a group
// (this item control will only be allowed to participate in d&d in this particular gorup)
private static void Add(Dictionary<String, List<ItemsControl>> dictionary, object sender)
{
    InitializeDragDropCollections();

    var dp = sender as DependencyObject;
    var itemsControl = sender as ItemsControl;
    var groupName = GetGroupName(dp);

    var foundGroup = dictionary.FirstOrDefault(p => p.Key == groupName);
    if (!foundGroup.Value.Contains(itemsControl))
        dictionary[groupName].Add(itemsControl);

    itemsControl.Unloaded += dropTarget_Unloaded;
    itemsControl.Loaded += dropTarget_Loaded;
}

// Removes item control from group
private static void Remove(Dictionary<String, List<ItemsControl>> dictionary, object sender)
{
    var dp = sender as DependencyObject;
    var itemsControl = sender as ItemsControl;
    var groupName = GetGroupName(dp);

    var foundGroup = dictionary.FirstOrDefault(p => p.Key == groupName);
    if (foundGroup.Value.Contains(itemsControl))
        dictionary[groupName].Remove(itemsControl);

    itemsControl.Unloaded -= dropTarget_Unloaded;
    itemsControl.Loaded -= dropTarget_Loaded;
}

       We make sure both collections are initialized and an empty "global" drag and drop group is added to both by using the InitializeDragDropCollections method:'s

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Intizialize drag drop collections
private static void InitializeDragDropCollections()
{
    if (dragSources == null)
        dragSources = new Dictionary<string, List<ItemsControl>>();
    if (dropTargets == null)
        dropTargets = new Dictionary<string, List<ItemsControl>>();

    if(!dragSources.Any(p => p.Key == ""))
        dragSources.Add("", new List<ItemsControl>());
    if (!dropTargets.Any(p => p.Key == ""))
    dropTargets.Add("", new List<ItemsControl>());
}

That's it for now, I'll finish the tutorial quite soon by adding the rest of events and methods. Along with the link to the whole class.