Recent posts about LINQ and related topics

WinRT Contacts

In preparation for recording a session of Deep Fried Bytes on the WinRT Namespaces, I scanned through the public ones in the Developer Preview to see what I could find. One of the interesting ones that I hadn’t heard about yet is the Contact class in the Windows.ApplicationModel namespace. This appears to be preparing for an integrated contact experience similar to the one found on the people hub on the Windows Phone. 

Similar to accessing files, music, pictures, etc, for contacts, you access them through the ContactPicker object. It has two main methods: PickSingleContactAsync and PickMultipleContactsAsync. Unfortunately, it appears that there is a bug in the PickMultipleContacts implementation making it unusable from type-safe languages at the moment. The WinRT samples do include a working version in JavaScript if you want to go that route.

The ContactInformation object supports the following properties: Name, Locations (addresses), Emails, PhoneNumbers, InstantMessages, and CustomFields. Custom Fields support Name, Value, Category and Type. With this, you can handle a wide variety of information about your friends.

I was able to create some sample code to get a contact:

Dim contPicker As New Windows.ApplicationModel.Contacts.ContactPicker
Dim contact = Await contPicker.PickSingleContactAsync()
Dim contactName = contact.Name

Unfortunately, when running this code on the current bits, the following message appears.

image

I guess we’ll have to wait until the store opens and contact apps start appearing to use it, or will we. If you open the Package.AppManifest and navigate to the Declarations, you can add the Contact Picker declaration to your package and your own search page and algorithm to search your proprietary store similar to how you can enable your application to search files from Facebook, Skydrive, Flicker, etc.

image

Posted on 10/26/2011 7:38:00 PM - Comments(0)
Categories: VB Dev Center

Windows 8 Hands On Labs available

build_logo_smallAt Build, we had the opportunity to preview a number of Hands On Labs for development and managing Metro styled applications. Unfortunately, they were not available to non-build attendees and we weren’t able to bring them with us from the conference.

Thankfully, the hands on labs are now available for everyone. You can download them now from the Build website. I tried a couple of them at the conference and the ones I tried appeared to be fairly well done. Labs are available in C#, VB, C++ and JavaScript. If you’re looking to learn the preferred way of doing standard tasks, I recommend trying them out. The standard caveats apply. These are initial labs for a pre-beta operating system. Both the labs and the platform are likely to change as Windows continues to evolve. That’s the price you get for living on the bleeding edge, occasionally you will get cut.

Update: In addition to the Build Hands On Labs, Microsoft has also released the Visual Studio 11 Developer Preview Training Kit to help you understand the new features of Dev 11 including more hands on labs.

Posted on 10/18/2011 1:12:00 PM - Comments(0)
Categories: VB Dev Center

Windows 8 Samples

imageIf you are playing with the Windows 8 developer preview as I have been since the Build conference, you may have noticed a lack of documentation causing you to hunt and peck through the Object Browser to try to find methods to accomplish tasks that you used to do. To make things easier, Microsoft has released a number of Metro style application samples that you can download individually, or all of them together in the bulk sample pack. At this point, they are the best way to learn the ways that the WinRT libraries were intended to be used. They are reasonably well documented. In addition, expect for the number of samples to continue to grow. I know that the teams added 23 VB samples alone in the last 2 weeks.

If you have further questions about Windows 8, your best bet is to head over to the Windows 8 Forums and join in on the conversation. If you still need to download the bits, grab them from the Windows dev center.

Posted on 10/6/2011 1:26:00 PM - Comments(0)
Categories:

Ix Interactive Extensions return

If you’ve been following the Reactive Extensions for any time, you may have seen that the team utilized the duality between IEnumerable and IObservable to not only create parallel extension methods of the enumerable versions on IObservable, but they also created IEnumerable versions of the additional methods that they added to IObservable as well. This formerly was in the Interactive libraries that came as part of the Rx bits. When the version 1 release of Rx came out however, these IEnumerable extensions were not included as part of the shipping bits.

Yesterday Microsoft released the v1.1.10823 version of these extensions branded as Ix.Note that this is an Experimental release, which means that it is highly subject to change, so if you use it, be prepared to make changes as new versions are released. When you download and install it, you can find the binaries in your C:\Program Files (x86)\Microsoft Interactive Extensions SDK\v1.1.10823\ directory. The current release includes versions for .Net 3.5 and 4, Silverlight 4 and 5 and Windows Phone 7. Glancing at the methods added in the EnumerableEx class we can find the following:

  • While, DoWhile
  • If
  • Case
  • For
  • Do
  • Buffer
  • DistinctUntilChanged
  • Repeat
  • Throw
  • Catch
  • Finally
  • OnErrorResumeNext
  • Retry
  • Publish
  • Memoize

In addition, this release adds the System.Interactive.Async library including a set of extensions that allow you to turn Enumerables into AsyncEnumerables and perform the same sets of queries you could with IEnumerables and IObservables.

If you’re interested in these, make sure to keep an eye on the Rx team’s blog and Channel 9. Also, Bart talked about the System.Interactive extensions on Channel 9 in July. He also mentioned that the bits are also available on NuGet. Look for the Ix_Experimental-Main and Ix_Experimental-Providers packages.

Posted on 8/26/2011 8:04:00 PM - Comments(0)
Categories: Rx LINQ

Select Many with Rx and RxJs

A while back, I showed how you could use the query syntax and Rx to code a drag-drop operation similar to the way you might describe  the process to your mother. As a review, let’s take another look at the code, this time in C# as a preparation for moving it over to javaScript.


var mouseDown = from evt in Observable.FromEventPattern<MouseButtonEventArgs>(image, "MouseLeftButtonDown")
                select evt.EventArgs.GetPosition(image);
var mouseUp = Observable.FromEventPattern&;lt;MouseButtonEventArgs>(this, "MouseLeftButtonUp");
var mouseMove = from evt in Observable.FromEventPattern<MouseEventArgs>(this, "MouseMove")
                select evt.EventArgs.GetPosition(this);

var q = from startLocation in mouseDown
        from endLocation in mouseMove.TakeUntil(mouseUp)
        select new
        {
            X = endLocation.X - startLocation.X,
            Y = endLocation.Y - startLocation.Y
        };

What might not be evident from this code is that the query syntax is actually using the SelectMany extension method when we use the from x in xs from y in ys above. If we wanted to, we could re-write this using the Lambda/method syntax as follows:


var q = mouseDown
        .SelectMany(startPos =>
              mouseMove
                    .TakeUntil(mouseUp)
                    .Select(movePos =>
                        new
                        {
                            X = movePos.X - startPos.X,
                            Y = movePos.Y - startPos.Y
                        })
                     );

Personally, I prefer the query syntax and find the lambda syntax a bit messy with SelectMany. Let’s try to tear this down a bit to see if we can understand what’s going on. Here we still have 3 different sets of Observables—mouseDown, mouseMove and mouseUp. The SelectMany extends mouseDown taking in the instance of each observed value as it is produced. SelectMany then takes a function (lambda) which will generate a new set of Observables of some other type which may or may not include values from the first observed value and values from the second variable.

In this case, we create the second observable set from the mouseMoves that we start listening to as the result of the SelectMany. We can then generate an new observable set by projecting (Select) the mouseDown’s startPos and mouseMove’s movePos offsets. For those who think pictures say more than words, here’s a marble diagram illustrating how SelectMany works:

image

In this case the Ox represents the MouseDown observables. fx represents the function supplied in the Lambda parameter and Oy represents the resulting Observable set of offsets. Wes and Jeffrey walk through SelectMany in their Channel 9 video as well.

Moving to RxJs

So what does all of this have to do with JavaScript? In the process of rewriting some of my Rx talks to RxJs, I wanted to include the MouseMove sample. Since JavaScript doesn’t support the syntactic sugar that enable query expressions, we are forced to use the method syntax. If you want to skip to the chase, you can download my RxJs samples and look at the DragDrop.htm to try this out. As we did with the Silverlight/WPF version of DragDrop, we start by setting up three sets of Observables to track the MouseDown, MouseMove and MouseUp events.


    <script type="text/javascript" src="Scripts/rx.js"></script>
    <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
    <script type="text/javascript" src="Scripts/rx.jQuery.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            var dragTarget = $("img");
            var mouseUp = dragTarget.toObservable("mouseup");
            var mouseMove = dragTarget.toObservable("mousemove");
            var mouseDown = dragTarget.toObservable("mousedown")
                .Select(function (event) {
                    return {
                        left: event.clientX - dragTarget.offset().left,
                        top: event.clientY - dragTarget.offset().top
                    };
                });
    </script>
    <img alt="logo" src="Images/RxLogo.png" style="position: absolute; top:400; left:400; height: 100;
        width: 100" id="RxLogo" />

Now that we have our observables, we can merge the streams using SelectMany and subscribe to the end result to actually move the image on the screen by altering the css left and top positions accordingly.


            var moves = mouseDown.SelectMany(function (imageOffset) {
                return mouseMove
                        .Do(function (event) { event.preventDefault(); })
                        .Select(function (pos) {
                            return {
                               left: pos.clientX - imageOffset.left,
                                top: pos.clientY - imageOffset.top
                            };
                        })
                        .TakeUntil(mouseUp);
            });
            moves.Subscribe(function (pos) {
                dragTarget.css("left", pos.left);
                dragTarget.css("top", pos.top);
            });

With the exception of the JavaScript function syntax which I tend to think of as a hybrid of C# and VB’s lambda syntaxes, we essentially have the same code that we did with the C# lambda syntax for SelectMany.

It can take a bit of mind twisting to get your head around the lambda syntax for SelectMany, but once you’ve done that, you can start doing some powerful manipulations to extend and coordinate observable event streams in both .Net and RxJs.

Posted on 8/26/2011 7:36:00 PM - Comments(1)
Categories: Rx JQuery C#