While building a Windows Phone 7 application that uses Bing Maps I saw myself going down a path where I was duplicating data. Namely, the Bing Maps ApplicationIdCredentialsProvider ApplicationID property must be set to an application-unique value on each instance of the map control. You can learn about obtaining a Bing Maps Key here.
Since I was using multiple map controls I was duplicating the value of my Application ID across my application. Since I’m teaching myself to build application declaratively using XAML I decided to eliminate my duplicate data using a Resource Dictionary. To make things more interesting, I chose to create and bind to the dictionary using Expression Blend for Windows Phone instead of editing source code directly.
Step 1: Create a Resource Dictionary
Creating a resource dictionary in Blend is quite easy. Just right-click on your project name and select Add New Item… and then select Resource Dictionary.
Step 1: Add New Item…
Step 2: Resource Dictionary
Step 2: Create an Application ID Resource
The first time I tried this step I could not figure out how to use the Blend UI and reverted to writing raw XAML by hand [more on this below].
Add your Application ID to an Existing Map Control
To start, find an existing Bing Map control in your application and make sure it has the Bing Maps Key set correctly. To do this:
- Click on the Map Control
- Load the Properties Pane
- Paste [or type very carefully…] your Application ID into the first CredentialsProvider box.
Once you enter your key and press Enter blend will add your key to the Map’s XAML. It also replaces the Key you entered in the Properties Pane with the Type name of the CredentialsProvider object it used and fills in the small box to the right of the field:
Add the Application ID to the Resource Dictionary
With your Map’s CredetialsProvider defined you can now use Blend to refactor your application by extracting the Application ID to your Resource Dictionary. This step is even more simple than the last:
- Click the little white box to the right of the CredetialsProvider field and select Convert to New Resource
- Define your Resource
- Set the name to something like BingMapsAppID
- Set the Define In control to Resource dictionary
- Select your resource dictionary from the drop down
- Click OK
And that’s it! Blend will now automatically update your source code for the Map control to add a StaticResource binding. Blend will also add your Application ID, along with the rest of the CredentialsProvider, to your ResourceDictionary. Check out the XAML source for both for details!
Step 3: Apply the Static Resource to Other Maps
Now that the Static Resource is available you need to take advantage of it by using it anywhere where your Application ID is needed. To do this, find your remaining Map controls. On each control click the little box to the right of the first CredentialsProvider field you see. Hover over Local Resource and select BingMapsAppID. That’s it!
How I expected to use the Resource Dictionary
I’m not used to tools being quite so… streamlined. Typically, editing resources means:
- Go into the resource file
- Figure out how to add the right type of resource
- Add the resource
- Use the resource across your app
Blend takes a different, and in my opinion better, approach. You create your resource using the designer for the first instance of a thing, and then you refactor it out into a separate resource file. Blend does all the heavy lifting of figuring out how/what to add in the designer and then automatically carries that info into the dictionary while figuring out the wiring in between.
I think this is a great model. However, I wish Blend paid a bit more attention to one unfortunate fact: many of us are not used to such simplicity. My first attempt at using these features left me feeling frustrated. I created my resource dictionary, I opened it up, and saw this:
Ok, I guess this makes sense… I’ll use the Resources Panel to edit my resources. Unfortunately, the Resources Panel does not support adding new resources! With no further handy guidance, I just assumed that Blend has no real support for Resource Dictionary management and went back to editing XAML by hand.
I’m glad I did some more research and learned to use this great Blend feature. I do wish, however, that the Blend folks added one more link in the Resource chain and helped me transition away from the cumbersome designers of yesteryear and to this new model.