Archives For silverlight

iStock_000011532992SmallThe other day Jeff Wilcox had a great post on Metro design guidance for Windows Phone developers. While it covered a lot of ground is an outstanding resource, there was a couple of topics that weren’t touched upon, globalization and localization. Given the exponential growth we’ve seen in the mobile marketplace, you’ll need to understand these are concepts if you want to create a great user experience for as many users as possible around the world. You’ll also want to think about them while you’re designing your application, NOT RIGHT BEFORE YOU SHIP IT. I’ve seen numerous applications on a variety of platforms (Windows, web, and mobile) try to throw globalization and localization in at the 11th hour only to realize that UI’s need to be refactored to account for right-to-left languages, or languages with extremely long words. The goal of this post to help you understand how to implement globalization and localization in your Windows Phone applications so you can get your next great app on as many devices as possible!

In case you have never had to deal with globalization or localization in the past, let’s start with some definitions. First globalization:

A globalized application targets multiple languages or cultures. A globalized application can display content such as date, time, currency, numbers, calendars and sort order in different formats for specific regions or cultures. For example, in the U.S. the date is displayed as month/day/year, but the date is displayed differently in other countries or regions. A globalized application also supports different input methods provided by the operating system.

Next localization:

Localization describes the process of adapting an application for a specific local market. This includes translating the user interface, resizing dialog boxes, customizing features, and testing results to ensure that the application works for the target market.

Now that we have a general idea of what globalization and localization refer, let’s tackle making a Windows Phone application globalized. There are four specific things you need focus on to ensure your application is globalized:

Dates. Different countries and regions display dates differently. For example, in the United States dates are typically displayed as month/day/year. However, in many other countries dates are displayed as day/month/year. It’s important that you take these differences into account as a date like 12/11/2011 can have two different meanings (December 11th, 2011 in the US and November 12, 2011 elsewhere).

Time. Different countries and cultures display time differently as well. In the US, it’s common to display time using a 12 hour clock (i.e. 10:04 PM). However, many countries use a 24 hour clock (i.e. 10:04 PM = 19:02).

Currency. Many countries have their own currencies (i.e. US Dollar, Japanese Yen, etc.) When displaying currencies you’ll want to make sure to display the appropriate currency symbol. You’ll also want to make sure that the numeric value is formatted appropriately as well since some countries use commas and decimal points differently (some countries use 1,000.00 while other use 1.000,00 to represent the number 1000).

Sorting. NEVER, EVER implement your own sorting algorithm in a globalized application. The CompareInfo property of the CultureInfo class defines how sort strings for each culture. By simply using the Sort method of something like the Array class, you will get the benefits of this property doing the culture sensitive sorting for you.

Below is a picture of how a globalized application should look when running under different cultures:

GlobalizedApp

 

Did you know that you can change the language the emulator runs in? Simply go to the Settings application, select region & language, choose your display language of choice (current options are German, English (UK), English (US), Spanish, French and Italian). To apply the changes you’ll then have to click a link at the top of the main page in the Settings application that will “reboot” the emulator into your desired language. Pretty handy for testing your application’s globalization/localization.

So far so good, our app is globalized. However, we have some issues in terms of localization:

NonLocalizedApp

As you can see in the picture above our application isn’t localized for Spanish as the various labels as well as the text associated with ApplicationBar is not translated appropriately. Taken care of this is pretty straightforward:

  1. Add resource (.resx) files to your project to store various translated strings and/or images.
  2. Perform the translations (for strings I like to use Bing Translator).
  3. Set the neutral language for the application via the Assembly Information that can be accessed in the Application tab of the project’s properties page.
  4. This next one is an bit tricky. You need to close (or unload) the project in Visual Studio and manual modify the xml that is part of the .csproj file. Specifically you’ll need to update the SupportedCultures element to include the cultures you plan on supporting. In the case of my sample app, it would look something like <SupportedCultures>es-ES</SupportedCultures>.
  5. You’ll then need to bind your UI elements to the translations. You can do this in a number of ways. Since I use MVVM for my Windows Phone development, I typically expose the resources as a property on the ViewModel being used by the page. However, there is one caveat here. If your application uses the ApplicationBar (as my sample app does) you can’t bind the text associated with its buttons and menu items in xaml. This is due to the fact that the ApplicationBar is not actually a Silverlight control. As a result, you’ll need to construct your ApplicationBar in code to localize it properly.

Once you do this, you should get an application that looks something like this:

LocalizedApp

As you can see it’s pretty easy to globalize and localize your Windows Phone applications, so there’s no reason why you shouldn’t being doing it RIGHT NOW!

If you want the source code for this demo app to see how all the pieces work, you can get it here.

Happy Coding!

Additional Resources:

On Tuesday, November 9, 2010 Presented at the Twin Cities Developer Group on Silverlight and the Managed Extensibility Framework (MEF). Here’s brief overview of the session:

In certain .NET circles there has been a lot of talk as of late about the Managed Extensibility Framework, or MEF. You may have even heard some of this chatter, but you’re probably asking yourself “What the heck is MEF and why should I care?” At its core, MEF is a library for creating lightweight, extensible applications. MEF is an integral part of the .NET Framework, and Silverlight 4 shipped with out-of-the box support for MEF. In this session we’ll dive into the guts of MEF looking at what it is, why you might want to consider using it, and how to actual implement MEF in your Silverlight applications.

You can download the source code for the session demos here.

PDC10 Recap

November 5, 2010

On Thursday, November 4, 2010 Brent Stineman and I recapped PDC10 with an open discussion at the Twin Cities .NET User Group. The discussion had two main areas of focus, one being, what I’m calling, “The HTML5 Incident” and the other being announcements regarding Windows Azure. Below are links to resources from each focus area.

“The HTML5 Incident”

Windows Azure Announcements

Additional Resources

(On a side note, I had originally planned on giving a presentation on the Managed Extensibility Framework. I’ll work with the organizers of the Twin Cities .NET User Group to get that session back on the schedule some time 2011. Between now and then I will be posting a multi-part series on MEF, so stay tuned!)

On Sunday, October 10, 2010 I presented at Twin Cities Code Camp on Silverlight and the Managed Extensibility Framework (MEF). Here’s a brief overview of the session:

In certain .NET circles there has been a lot of talk as of late about the Managed Extensibility Framework, or MEF. You may have even heard some of this chatter, but you’re probably asking yourself “What the heck is MEF and why should I care?” At its core, MEF is a library for creating lightweight, extensible applications. MEF is an integral part of the .NET Framework, and Silverlight 4 shipped with out-of-the box support for MEF. In this session we’ll dive into the guts of MEF looking at what it is, why you might want to consider using it, and how to actual implement MEF in your Silverlight applications.

You can download the source code for the session demos here.

On Saturday, October 9, 2010 I presented at Twin Cities Code Camp on Multi-Targeting WPF, Silverlight, and Windows Phone 7. Here’s a brief overview of the session:

With WPF, Silverlight, and now Windows Phone 7, all using xaml, multi-targeting applications can be a reality. However, it’s not without its challenges. In this session I’ll talk about which things are easiest to share, and which are hardest. I’ll also show you some recommended approaches on how to structure your project, as well as a comparison of the different ways by which you can share code and xaml. I’ll cover topics such as assembly portability, code sharing using linked files, usercontrol encapsulation and abstraction, and even loading xaml at runtime after tinkering with the source via code.

You can download the source code for the session demos here.

On September 8, 2010 I led a Hands On Lab at HDC10 entitled Silverlight Bootcamp. The purpose of the hands on lab was to get attendees’ feet wet with Silverlight. The session was a sellout, and while we had some technical difficulties at the beginning (not everyone had the pre-reqs downloaded, SQL Azure connectivity issues, etc.) I think it went OK. Ideally we would have had a longer session, with appropriately configured machines for each attendee, but some times you just have to take what you’re given and make the best of it.

During the class we covered a number of topics from the Entity Framework to WCF RIA Services to MVVM to Unit Testing to OData to Windows Phone. I’ve decided to post the content from the class for you to take a look at.

To run all the labs you’ll need the following pre-requisities:

Microsoft Visual Studio 2010

Microsoft Silverlight 4 Tools for Visual Studio 2010

WCF RIA Serivces V1.0 for Silverlight 4 and Visual Studio 2010

Windows Phone Developer Tools Beta

OData Client Library for Windows Phone 7 Series CTP (yes, it does work with the beta tools)

Silverlight Media Framework

Applicaiton Bar Icons for Windows Phone 7 Series

I created a .zip file that contains all of the pre-reqs (with the exception of Visual Studio). You can grab that file here.

You can download the labs here. Each lesson within each lab contains begin and end code so you can see what we started with and where we ended up.

During the class we used a SQL Azure database. For hopefully obvious reasons, I’ve disabled the login we used during class. However I have uploaded the .mdf for you if wish to run the database locally. You can get the .mdf here.

Questions, issues, thoughts? You know where they go…

I’m Back!

September 7, 2010

After my last post I unplugged…completely unplugged…for three months! I did this for several reasons, but the main one was that I felt the noise to signal ratio I was getting from a number of inputs was way too high. I was overloaded with, what turned out to be, primarily meaningless, unactionable information. I’ve also been moving into a new role at my company, and the demands the role has placed on me requires me to be very efficient with my time. Spending my time sifting through information to separate the wheat from the chaff is a luxury I can no longer afford. To that end, I spent the past three months digesting J.D. Meier’s new book Getting Results the Agile Way and coming up with a strategy for making the best use of my time. There are several practices that I’ve come to rely heavily upon:

  1. Daily results The goal is to identify three, AND ONLY THREE, important outcomes that you want accomplish in a given day. Often times we try to accomplish more than we can in a given day, leaving us with a feeling of failure. The reality is that we’re simply failing to focus. By limiting our daily scope we can actually get more done.
  2. Brain dumps The goal is to factor your thinking from your chatter and reminders. At it’s essence, you simple take anything, and everything that is on your mind and write it down (either to physical or digital format – I prefer OneNote & Evernote). The result is that you relieve yourself of a certain psychic weight that enables you to focus on the important things.

I also hold a daily personal standup meeting. I know it sound a bit silly, but it helps me focus on my priorities for the day. Spending just 10-15 minutes to plan each day has helped me get a better handle on the day’s tasks.

During this three month period I also had the goal of getting a grip on my email. Like many of you I have a number of email accounts. Between my corporate, client, and personal email addresses, I have over 10 accounts to maintain. That can be a lot of information coming from a lot of different directions at the same time. Unfortunately, due to various, client firewall policies, consolidating these accounts into a unified inbox with something like Exchange Web Services {LINK} isn’t an option. I figured the best approach would be to process messages the same way for each account. I also set a standard of having zero emails in my inbox at the end of each day. To accomplish this, I once again turned to J.D. Meier. In case you don’t know who J.D. is, he is a legend within Microsoft for getting things done. The guy is a machine. He multi-tasks better than anyone I’ve seen. Several years ago J.D. wrote a blog entry on the Zen of a Zero Mail. I took his general approach, made some customizations based on personal work style, and applied to each my email accounts. I’m happy to say that as of today I’ve been experiencing the Zen of a Zero mail for a solid two months.

With all of that said, just because I’ve been silent here and other places, doesn’t mean I haven’t been busy. I’ve been spending a fair portion of my time getting immersed in Windows Phone 7 development. I have a couple apps that are near completion, and a couple of more that are in the early phases of development. I’ve also been working with J.D. Meier and the Patterns and Practices team at Microsoft on the following projects:

  1. Microsoft Developer Guidance Maps. The Microsoft Developer Guidance Maps Project is an effort to create a model and a map of resources for developer guidance. The goal is to simplify as well as better organize and share our catalog of developer guidance resources. We’re using this catalog as a way to model, prototype, and test ways to find, organize, and share developer guidance in a meaningful way. As part of the exercise we’ll be creating and sharing our maps of pointers to useful code samples, how to’s, videos, etc. for the Microsoft application platform.
  2. Windows Azure Security Notes. This is a collection of our notes and learnings from exploring the cloud security space and working through Windows Azure security scenarios.

There are also a couple of projects I can’t talk about just yet, but they bring together Microsoft’s cloud and mobile platforms in some very interesting ways. As these projects progress and clients are willing to share a bit more you’ll be the first to know.

I’ve also been busy prepping content for the following presentations I’ll be giving this fall/winter:

  1. Heartland Developers Conference
    • Silverlight Bootcamp
  2. Minnesota Developers Conference
    • Introduction to Windows Phone 7 Development
    • Tablets: The iPad and the Future of Mobile Computing
  3. Twin Cities Code Camp 9
    • Multi-targeting WPF, Silverlight, and Windows Phone 7
    • Getting Down with MEF – Silverlight Edition
  4. Twin Cities .NET Developer User Group
    • Getting Down with MEF
  5. Twin Cities Developers Guild
    • Getting Down with MEF – Silverlight Edition
  6. Twin Cities Cloud Computing User Group
    • Windows Azure Security Guidance

(I hope to see you at a least one of these great events).

I’m also working on a screencast series that I’m tentatively calling "Silverlight Sprints". It’s an adaption of my Silverlight Bootcamp class. The goals is to have a series of episodes (no longer than 10 minutes each) that will get you up an running with Silverlight development (both for the desktop and Windows Phone 7). The content and code for the series is complete I just need to sit down and record it.

Finally, I’ve also been asked to co-host this year’s season of the Spaghetti Code podcast with Jeff Brand. The format is going to be different than in seasons past. Each month we’re going to talk about what’s new in the Microsoft development space. Our first episode is up and you can listen to it here. Let me know what you think!

That’s it for now, but they’ll be a lot more coming soon. Stay tuned!

I’ve uploaded the code and slide deck from my presentation at the Twin Cities Silverlight User Group. You can download the content here.

A couple of quick notes:

  1. The code includes the source for the TekPub media player I demo’d during the presentation.
  2. To build and run the code you need the following installed on your machine:
    1. Microsoft Visual Studio 2010 RC
    2. Windows Phone Developers Tools CTP

Enjoy!

{Every now and then I find that I have to do some kind of configuration magic to get my development environment working the way I want it to. I promptly forget it until I have to do it again 3-6 months later when I replace my machine. By then I have no idea what I did so I have to look it up again. The point of this Note to Self series is to remind me of exactly what I did so I don’t have to try to remember the name of some odd command line tool and the various parameters that go along with it.}

So, I’m a big fan of Test Drive Development (a.k.a. TDD). One of the big statistics I like to get out of TDD is code coverage, that is, what percentage of code is being covered by my unit tests. I’m well aware that 100% code coverage doesn’t mean your application is completely and thoroughly tested, but it gives you an idea of how much of your code is covered and where you need to write some more testes (in case you didn’t write them prior to writing your code – but you wouldn’t do that now would you?)

Anyway, I’m currently working on a project with a large suite of unit tests and I wanted to get the code coverage metrics for the project. Naturally, I fire up the solution in Visual Studio 2008, go to the Test menu, click Edit Test Run Configurations, and click Local Test Run. I then click Code Coverage to turn on code coverage for a given assembly and *POOF* the Local Test Run Configraution window just disappears. This has happened to me several times in the past after re-paving machines and then trying to turn on code coverage in a solution that contains a Silverlight project. To remedy the problem, download and install this hotfix and you should be good to go.

Hope this helps.

(Note: I typically don’t tweet my blog posts, but I did so with this one as I want to make sure those following the developments at MIX10 around Windows Phone 7 and Silverlight are aware of some key differences between the platforms.)

Tuesday at MIX10 there was a big release that went completely unannounced Silverlight 3.7 is now in CTP! That’s right 3.7! I can’t believe it! It’s amazing! It’s, uh, um, hey, wait a minute, isn’t the release candidate for Silverlight 4 in the wild?

No, I’m not losing it (well maybe I am, but that’s neither here nor there). In my last post I discussed one of the key shortcomings, in my opinion, of the WP7 platform, the lack of browser support for Silverlight. I pointed this out because my fear is that a lot of people are going to assume that WP7 supports browser hosted Silverlight since native apps can be written for with Silverlight. As a result, people may not be willing to adopt the WP7 platform quickly since they can’t leverage existing Silverlight assets. The goal of this post is to try and show a couple of different ways you can reuse code across both the “browser hosted” and “phone hosted” silverlight platforms.

First we’ll set up a solution with two projects, a Windows Phone application, and a Browser Hosted (Silverlight 4) application:

cxag_ss001_hosts

Next we’ll want to add a project that will contain code we want to reuse across the the two applications. In this case we’ll choose a Silverlight class library project that targets the Silverlight 4 runtime:

cxag_ss002_sl4

Next we’ll want to our two applications projects to reference the class library. Adding a reference to the Browser Hosted application works without problem. However, when we try to add the reference to our Windows Phone project we get the following error:

cxag_ss003_sl4_error

Let’s try the reverse. Let’s first add a Windows Phone class library project:

cxag_ss004_wp7

Now let’s add the project references. Adding the references to the Windows Phone application works as expected, but now the Browser Hosted applications gives us ethe following error:

cxag_ss005_wp7_error

So, what the heck is going on here? To find out we’re going to need to take a peek at the assemblies referenced by our Browser Hosted and Windows Phone application. The assemblies for the Browser Hosted application can be found in the following directory: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0. Let’s choose one assembly to look at. In this case we’ll use the System.Windows.dll. Here are the details of the assembly:

cxag_ss006_systemwindowsdll_sl4

The assemblies for our Windows Phone application can be found in the following directory: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone. If we take a look at the System.Windows.dll assembly in this directory we’ll see the following:

cxag_ss007_systemwindowsdll_wp7

See the difference? Windows Phone uses, what is, essentially, Silverlight 3.7, which is why we can’t add a reference to a Silverlight 4 class library. If you take a look at some of other assemblies in this directory, such as System.Core.dll, you’ll see something like this:

cxag_ss008_systemcoredll_wp7

These core assemblies are actually compiled for the .NET Compact Framework 3.7.10056.0. This is why we’re unable to add a reference to a Windows Phone assembly to our Browser Hosted project.

So, how can we share Silverlight code between our Windows Phone and Browser hosted applications? There are two approaches that initially come to my mind. (I have a third idea involving T4, but that needs to get flushed out yet). The first thing we could do is create a Silverlight 3 class library project:

cxag_ss009_sl3

All we need to do now is add the reference to our two application projects, which will work. However, I don’t like this approach as it’s least common denominator (LCD) programming. We’re actually holding the code in both applications back from full potential by using a lower version of Silverlight.

Another approach is to use linked files. In this case we’ll delete the Person.cs file from our CxAg.Shared.Phone project. We will then add the Person.cs file from the CxAg.Shared.Silverlight4 project to our CxAg.Shared.Phone project as a linked file. The result will look like this:

cxag_ss010_linkedfile

See that little arrow by the Person.cs file? It indicates that it isn’t actually a file, but, rather, a pointer to a file located somewhere else. In our case, it’s just a point to the Person.cs file in the CxAg.Shared.Silverlight4 project.

One thing you’ll want to keep in mind is how to handle code that compiles for one platform, but not for the other. An approach I’ve taken in similar scenarios looks something like this:

cxag_ss011_partialclass

I like to declare my shared classes as partials. These allows me to include platform specific code in each project (as separate, non-linked files) without breaking the build. In my opinion neither of these solutions are ideal, but they’re workable. As I mentioned earlier, I’m working on a third, T4 based solution, but that’s still in progress. Keep in mind too that we’re working with what is, for all respective, purposes a new development platform, so there are kinks to be worked out. I hope this gets you on your way to successful Silverlight 4/Winodws Pohne 7 development!