I had quite a technical challenge, as I couldn’t access the internet from inside the emulator, no matter what I did!
After a couple of minutes in silent panic, I tried to bypass the connectivity problems the best I could, doing anything from showing a YouTube video of Nokia Music features to running the demos on my personal phone and showing it off in the air to the audience!
Other than that, the event went really good, and hopefully I’ll start seeing more and more people using the Nokia Music API and Cimbalino Windows Phone Toolkit in their apps! ;)
From the feedback I got, I can see people requesting more information about MVVM, so I’m going to point to two articles I wrote about two years ago for Coding4Fun (now part of Microsoft Channel9):
One of the features I most enjoy in WPF is the MultiBinding class, which allows you to take several source properties, pass their values to an IMultiValueConverter implementation, and bind the result to a single target property!
By now you’re probably thinking “why do I need that?”, but in certain specific scenarios, having the possibility to take several values and produce a single result directly in the UI can be quite useful!
We can easily infer that the objective here is to set the TextBlock.Text property to the result of “LastName, FirstName”, given the two properties from the specified TextBlock.DataContext and a custom IMultiValueConverter instance called myNameConverter.
While useful, the MultiBinding is not available for Windows Phone developers - queue the whining…
I’ve seen a few alternative implementations around the internet, but none seems easy (and light!) enough to me, so I just decided to make my own!
The major difference here is the usage of MultiBindingBehavior.PropertyName, as we can’t bind the target property directly, it will be up to the behavior to get/set the value. All the rest of the code is really quite self explanatory!
Here’s another sample using two TextBox controls to input the FirstName and LastName values:
You can set the MultiBindingBehavior.Converter property to any IValueConverter instance (knowing that the value parameter is always an object[] instance) but I’ve also added an abstract MultiValueConverterBase that you can inherit and implement quite easily!
Here’s a short list of all the goodies you’ll find in the 2.3.0 version:
Breaking change: Location component now uses the new Windows Phone 8 Location API (Geolocator) on WP8 (WP7 version will mimic behavior with GeoCoordinateWatcher)
Breaking change: In WP8, the UserExtendedPropertiesService.AnonymousUserID now returns the “ANID2” value (WP7 version will still return “ANID”)
New MultiBindingBehavior
New OptimizedObservableCollection class
Several improvements and bug fixes!
I didn’t actually release a change log for the previous 2.2.0 version, so here it is now:
New MutexLock and NamescopeBinding helper classes
New BooleanToBrushConverter and BooleanToIntConverter
New UriExtensions class
The ApplicationBarBehavior and MultiApplicationBarBehavior can now be attached to the PhoneApplicationPage
The SystemTrayService implementation now allows for a global system tray across all pages
Removed the cimbalino = http://cimbalino.org XML namespace prefix (incompatible with WP8)
Several improvements and bug fixes
A big “thank you” is due to Scott Lovegrove for his continuous code contributions and articles about the toolkit!
Now to write some posts about the new features… ;)
This special class has a single property called Source that can be set to any object; in this sample, it’s pointing to a named control inside the pages LayoutRoot (hence the single Binding.ElementName property set)
If you look a bit down the code, you’ll find the ApplicationBarIconButton.CommandParameter where this resource is getting referenced:
If we just put the two parts together, we can see that the whole objective is to bind the ApplicationBarIconButton.CommandParameter property to ItemsMultiselectList.SelectedItems property.
Obviously you’d expect to just do it directly, like this:
In reality, this approach doesn’t work from the PhoneApplicationPage point of view, due to the different XAML Namescope.
XAML Namescope??
Here is the XAML Namescope definition found in this MSDN article:
…a XAML namescope stores relationships between the XAML-defined names of objects and their instance equivalents. This is similar to the wider meaning of the term “namescope” in other programming languages and technologies.
So think of the XAML Namescope as a hash-table with each control name and it’s instance (something like IDictionary<string, FrameworkElement>).
The problem is that the page’s LayoutRoot control gets a different XAML Namescope from the page itself, so searching for a child control from inside the page just won’t work!
The following image illustrates the Namescope boundaries for a default new page:
This is where the NamescopeBinding helper class makes its entry: just register an instance of this class as a page resource and bind the NamescopeBinding.Source property to the control you require outside the LayoutRoot XAML Namescope as shown in the sample code, and that’s it!
In the BindableApplicationBar sample, if you actually attach the MultiApplicationBarBehavior to the LayoutRoot instead of the page, it will work perfectly without the need for the NamescopeBinding helper class.
But my advice is to always bind the ApplicationBarBehavior and MultiApplicationBarBehavior to the page as it will provide better integration and more fluid animation, and use the NamescopeBinding helper class if needed.
I’ve created a small sample app that demonstrates the XAML Namescope behavior and how it affects your applications, which you can download from here.
The new Windows Phone Test Framework on Visual Studio 2012 Update 2 is a major step in the right direction for testing WP apps.
I particularly like the fact that all tests run on the emulator where they can get the full runtime environment of a real Windows Phone device.
Yet, the test framework still has some pitfalls where we realize it is just not enough!
A few weeks ago I wrote an article about one of these pitfalls, on how to run code in the UI Thread.
Now I’ve stumbled in yet another pitfall, this time on how to run the Assert.ThrowsException with async code!
Microsoft solved this issue on the Windows 8 version of the Test Framework, by adding some Task<Exception> Assert.ThrowsException(Func<Task>) methods, but there is no equivalent in Windows Phone.
This is my personal implementation of these methods for the Windows Phone Test Framework:
The NuGet team released the 2.5 version yesterday, and along with a bunch of really cool new features, there was one I was particularly interested: the ability to import MSBuild targets and props files!
This means we can now have NuGet packages without any type of assembly, targeting only the project build process.
And if you’ve read this article about the issue with the current build process for Windows 8 and Windows Phone 8 apps, where the Xml Documentation gets bundled in the output packages (APPX and XAP), I have some great news: no more need to change the project file manually, as I just published a NuGet package that does those changes for you!!
All you have to do is run the following command from the Package Manager console window:
If you have NuGet Package Manager extension installed on Visual Studio, just search for AppPackageCleanup to install the Package.
Note: this package requires NuGet 2.5 as it won’t show up in the search results with previous versions of NuGet!
If you’ve been following the news on the upcoming Visual Studio 2012 Update 2 (currently in CTP4), you know that we now have a fully working Unit Tests framework for Windows Phone projects!
Well, this seemed like the perfect opportunity to finally create some unit tests for the Cimbalino Windows Phone Toolkit and test this new test framework, all at once!
Given that this post will require installing the VS2012.2 CTP 4, a non-final preview version software that has a “go-live” license, I think it’s only proper to apply the WOMM certification to it.
So bottom line, install it at your own risk!
So I started by installing the update, launch Visual Studio 2012, and create a new Windows Phone Unit Test App.
This simple class will test the ColorToBrushConverter.Convert method as to check for the proper creation of a SolicColorBrush out of a specific Color.
When I ran the test, this is what happened:
As you can see here, we got an “Invalid cross-thread access” error message; this is the result of all tests running on a special “test app” inside the Emulator, but the really bad news is that they all run in a background thread.
Apparently, if this was a Windows Store app we could just change the [TestMethod] attribute with the proper [UITestMethod] one as to mark the requirement of running the test in the UI Thread, but unfortunately, the attribute is currently missing from the Windows Phone test framework!
So I decided to fix that and create my very own UITestMethodAttribute, and here it is:
Other approaches to solve this issue can be found in this post by Joost van Schaik or this gist from Jake Ginnivan, both relying on just running the necessary code in the UI instead of the full test method as with the above approach.