0 Comments

Caliburn.Micro v1.1 offers a completely new way of executing Windows Phone 7 launchers and choosers. The new version takes advantage of the EventAggregator-class when executing the tasks. For example if you want to execute the MarketPlaceReviewTask, it’s now as simple as calling the following code:

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">            eventAggregator.RequestTask&lt;MarketplaceReviewTask&gt;();

But how about if you have to configure the task before running it? It’s a common scenario that the WebBrowserTask is configured with the website’s URL or that the MarketplaceSearchTask is filled with some keyword. In previous versions one used the IConfigureTask to set the parameters. But now things are much simpler: The configuration action can be given as the parameter to the RequestTask’s method. You have two options: 1. Implicitly declaring the configuration method or 2. Lambda expressions. Let’s take a look at both options.

1. Configuration method

Here’s a code example where the EmailComposeTask is executed and the configuration happens in a implicitly declared method:

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Contact()
        {
            eventAggregator.RequestTask<EmailComposeTask>(ConfigureContactTask);
        }
        <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> ConfigureContactTask(EmailComposeTask emailComposeTask)
        {
            emailComposeTask.To = &quot;<span style="color: #8b0000">info@site.org</span>&quot;;
            emailComposeTask.Subject = &quot;<span style="color: #8b0000">Comment from app</span>&quot;;
        }

2. Lambda expression

The RequestTask-methd takes System.Action as its parameters so the above code could be written without the ConfigureContactTask-method. Here’s an example::

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Contact()
        {
            eventAggregator.RequestTask<EmailComposeTask>(x =>
                                                              {
                                                                  x.To = &quot;<span style="color: #8b0000">info@site.org</span>&quot;;
                                                                  x.Subject = &quot;<span style="color: #8b0000">Comment from app</span>&quot;;
                                                              });
        }

0 Comments

I spent the last weekend working with a small Windows Phone 7 news reader app. First I used the default ListBox control to layout the news items but when I later on decided that I would like to group the items nicely, I replaced the ListBox with the LongListSelector from the Silverlight Toolkit.The control brought me the ability to group the items, and it brought me tons on problems and peculiar behavior.

There’s couple big problems with the control which forced me to write stupid hacks in the app:

Problem: When you navigate back to a page which contains LongListSelector, the list may stay empty until you scroll it.

Solution: Call the ScrollTo-method of the LongListSelector when you navigate back to the page. ScrollTo forces the control to redraw itself. ScrollTo requires an object (not index) so you may need get the first item from your item collection and use that as the parameter. You can also store the current selected item from the list (SelectedItem) to somewhere safe when navigating away. When you navigate back to the page, call the LongListSelector’s ScrollTo-method to move back to the correct point.

Problem: The LongListSelector may throw NullReferenceException when you navigate back to the page.

Solution:If your LongListSelector is bound to a collection and you call NotifyOfPropertyChange for the collection when you navigate back to the page, the control may throw NullReferenceException (in its balance-method). The solution is to avoid the call to NotifyOfPropertyChange. There’s also some more tips available from the CodePlex-forums.

Overall, I recommend anyone to think twice before using the LongListSelector. The grouping-functionality is nice but in my humble opinion it’s not worth the problems the control causes.

0 Comments

When using the msysgit and the git svn fetch you may encounter the error:

"can't locate config_heavy.pl in @INC ... at /usr/lib/perl5/5.8.8/msys/Config.pm line 66".

This has been reported to the msysgit-team and the fix should be available in an upcoming version as stated here: http://code.google.com/p/msysgit/issues/detail?id=433. In the mean time you can download the git-svn-fix.zip from the following GitHub-repository to get around the problem. After downloading the file, extract it into the following folder: C:Program Files (x86)Git.

You can check if the installation succeeded by making sure that the file Config_heavy.pl exists in the folder C:Program Files (x86)Gitlibperl55.8.8msys.

Git-svn-fix.zip location: https://github.com/mikoskinen/blog/tree/master/git-svn-fix

102 Comments

The IIF-statements in VB.NET are a handy way to shorten our code and to make it more readable. It’s also an easy way to introduce subtle bugs into our systems.

Given the following code:

        Dim amount = 100

        IIf(amount = 100, amount = 30, amount = 50)
        Console.WriteLine(amount)

When run, the app will print out 30, yes? Well, actually no. Here’s the actual output when the app is executed:

image

What happens is that the code inside the true-part of the IIF-statement is not an assignment.Instead, it’s interpreted by the compiler as aboolean expression. Now let’s try to modify our code so that it stores the return value of the IIF-statement in a variable:

        Dim amount = 100

        Dim outPutValue = IIf(amount = 100, amount = 30, amount = 50)
        Console.WriteLine(amount)
        Console.WriteLine(outPutValue)

This is the output when executed:

image

As you can see, the IIF statement returns False, because it compares the equality of variable “amount” to value 30.

To correct the above code one should change it to the following:

        Dim amount = 100

        amount = IIf(amount = 100, 30, 50)
        Console.WriteLine(amount)

Bottom line: IIF-statements in VB.NET shouldn’t be used for assignments.

0 Comments

NSQLFormatter is a light-weight .NET-library for beautifying your SQL. NSQLFormatter is an open-source project, written with C# and .NET Framework 4.0. It is based 100% on the work done in the NHibernate-project.

Usage

1. Add reference to NSQLFormatter.dll.

2. var formattedSQL = NSQLFormatter.Formatter.Format(originalSQL);

No external references required.

NuGet

NSQLFormatter is available through the NuGet with the package name NSQLFormatter. Type “install-package NSQLFormatter” to install it.

Source code

Source code and the compiled binary is available from the following  GitHub-repository: https://github.com/mikoskinen/NSQLFormatter-dotnet

Background

NSQLFormatter was created when a project needed a way to easily beautify the inputted SQL. The excellent NHibernate-project (http://nhforge.org) contained the necessary code but adding the reference to NHibernate's dlls was deemed a too heavy-weight solution. So, the source code in question was copied from the NHibernate and all the requirements for external references (IESI Collections) were removed.

Licenses

NSQLFormatter is distributed under the terms of the FSF Lesser GNU Public License.

The project includes source code from the NHibernate-project, written by Fabio Maulo, Mike Doerfler, Sergej Košcejev, Ayende Rahien and others. Visit the NHibernate SVN-repository for more information: http://nhibernate.svn.sourceforge.net/viewvc/nhibernate/trunk/nhibernate/src/NHibernate/