WP7 Application names and DRY just don't mix, why is that?
Monday, September 06, 2010 11:21 PM
Ever notice who WP7 apps ALWAYS repeat the hard coded application name and page name string? Ever wonder why? Ever wonder how you can FIX it? Read on for a quick'n'easy way to keep things DRY!
Do you hate repeating yourself? I said, do you HATE repeating.......oh you get the idea! LOL
But seriously, when you code, do you use cut'n'paste when you REALLY should be refactoring methods? Do you try to keep things DRY? If you do, and you're learning WP7 dev (which is why I hope you're reading my blog LOL) you probably noticed you're forever repeating the page name and application name on every new page/form. I'm HOPING something in your brain was/is twitching everytime you add a new page and you see the same'ol strings over and over again (like below with the new WP7 page templates).
Well, my brain was always twitching when I did it, but I'm still learning WP7, XAML, SL3 and MANY other MS TLA (Three Letter Acronyms) and well, I just had too many other things to learn first. UNTIL this week when I read a blog from Kevin Wolf titled, Windows Phone 7 - Quick Tip. Ok, so the title's not really indicative of the contents, but I read it, and thought it was brilliant (and I fully expect MS to implement this in an upcoming respin of the tools to be honest!).
The crux of this TIP is two fold, add a string resource to the App.XAML file like such (note, you'll probably need to add the namespace up top too, at least I had to with the current rev of the WP7 tools, could change in the future?).
Next, just use it!
That's it! You're all set. Need to add a new page? At least now you can cut'n'paste the ApplicationTitle static resource Text value. Now, if you're with a company who changes product names like the Sens going up'n'down the standings, then this will save your bacon! Change it ONCE in the app.xaml file and BINGO, all changed throughout the WP7 app.
Now that you know how to put your WP7 Application Name in one place and share it around, it's time to go grab a coffee and get coding!
PS That logo up there on the WP7 app, is from an old knitted Montreal Canadiens jersey (1909-1910). Yes, I am a Habs fan! If you're new to my blog, WELCOME! And you'll see a bit of a theme, pot shots at the Sens (like above), the Leafs (cause it's sooooooooo easy) and lots of Canadiens logos PLASTERED all over my demos. They're demos, they're meant for fun, I hope you take it all in the spirit it was created. If you're offended by any of this....uuhhhh.........hhhhhmmmm you better not read some of my Java posts! :> LOLOLOLOLOLOL!!!!! AAAAHHHHHAAAAAAA!!!!!!!!!!
Source Code: http://www.pchenry.com:8080/svn/blog/trunk/2010/ApplicationNameResource
The Wolf Bytes: Windows Phone 7 - Quick Tip by Kevin D. Wolf
Wikipedia: Don't repeat yourself