Friday, June 26, 2009

WPF: Sorting Sets of Simple Strings

I've been doing a fair bit of WPF coding recently, and it's a compelling experience. The thing is, it isn't easy, and I think that surprises some people. Part of the reason for this is that the documentation still feels incomplete, in that undocumented kind of way. But the other reason is that it forces you to think very abstractly, all the time. If I were to think of a decent analogy, it would be one involving cooking: Coding in most UI toolkits is like cooking with lots of pre-made dishes (pre-cooked noodles, microwave dinners, pasta sauces out of the bottle.) Sure, you can make a decent meal, but the results are pretty predictable and maybe not that satisfying. Coding in WPF is like cooking with real ingredients - it can be challenging, and you can certainly mess up the results, but you can also whip up something fantastic.

Read more...

Friday, June 5, 2009

E3: Let's Get Physical

Seems like the latest E3 was full of surprises... or maybe not. With Nintendo's raging success in the video game market due to its unconvential Wii, you'd have to expect that both Microsoft and Sony would have to respond. They are now officially playing catch-up, and introduced their takes on the genre at the electronics tradeshow.
Microsoft gave a rather bold live demo of its newly acquired Project Natal, a smart camera that can motion-capture participants using a single camera device. This may sound like a gimmick, but the demos are surprising and effective. Included in the system are both image and voice recognition systems that are used to recognize the current user and interpret commands, respectively. I think the possibilities just for the social and device browsing features are pretty cool - built in voice and video chat, voice-activated, as well as being able to browse to your favourite downloaded media using hand motions. Kind of makes Minority Report seem a little clunky, actually.
Probably the most impressive demonstration of the technology is the Milo video, featuring Lionhead and Peter Molyneux.

Read more...

Thursday, May 21, 2009

I'm a Doctor, Jim, not a Horse Lord of Rohan!

I was reading an interview with JJ Abrams on aintitcool.com and had my mind blown. I knew the guy who played Bones in the fantastic new Star Trek film looked familiar, but I hadn't bothered to look him up.

Read more...

Thursday, May 7, 2009

KarmaTickle

What's the opposite of a Karma Slap? Good things happen to people who tip well. I took a cab today to pick up my car and left the cabby a $4 tip for a $19 ride, for no particular reason. I'd consider that a decent tip. As I was waiting for my car, the cabbie came back and dropped off a small brown bag I'd forgotten in the backseat. The weird thing is I thought I had checked that I had everything because I was also carrying my camera case and a chequebook. The package (which didn't look like much more than a work snack, honestly) actually contained some naturopathy pills, which I had just paid $50 for. So there you go.

The cabbie was obviously a good guy, but I'm guessing he's not turning the car around if I give him a twenty note on the nineteen dollar fare.

Wednesday, May 6, 2009

Dept. of Holy Shit This Is Awesome

Random Tweets

A few tweetbits from my twitter feeds this morning:
An impressive demonstration of the new Wolfram Alpha Computational Knowledge Engine. CNET News reporter Stephen Shankland runs through some sample (and sometimes amusing) searches.

Read more...

Wednesday, April 29, 2009

Less Briefly, in 3D

I'm astounded that entertainment media has found a valid, artistic reason for presenting movies in 3D. Apparently the movie industry is going to cut costs by attempting to produce movies that don't require barf bags.

Read more...

Monday, April 20, 2009

Follow the Moose!

So for all you peeps who have Google accounts - GMail included - you should check out Google Reader. Why? Well for one, it's a pretty neat universal feed aggregator that lets you follow a variety of frequently updated sites. And two, it's an easy way to follow this blog!

For those of you who are a little sick of the suck on the Internet - otherwise known as Facebook ennuie - and are looking to follow publications that provide a little insight, then this can be a very useful tool. And hey, if you think this blog doesn't suck, then feel free to try it out by Following my blog. Just click on the button in the Follow the Moose panel to your left, and this fine publication will be added to your Reader reading list. Go ahead and try it. Do it. DO IT. Or you know, subscribe to the RSS feed. I guess you could do that too.

Wednesday, April 15, 2009

Jing Screen Captures

Just wanted to give a shout-out to a really nifty WPF program called Jing, from the folks at TechSmith. This screencapture tool is extremely streamlined, allows you capture images and videos seamlessly, and you can post them online at the click of a button. The free version uploads them to the TechSmith hosting site, screencast.com, while the very cheap Pro version lets you upload straight to YouTube. It also supplies an MPEG 4 codec that produces better, smaller video files. And there's a Mac version too!

Read more...

Tuesday, April 14, 2009

iNIN: You Were Poked with a Nine Inch Nail

More Trent Reznor opinions on the music industry and how it should promote itself, as well as a description of the new Nine Inch Nails iPhone app, another experiment in social networking.
The article also describes the dynamics of the App Store, and how, with its low initial entry cost, it is spurring on development of small-scale applications similar to the ones developed in the late eighties and early nineties. John Carmack has even gotten into the act, porting Wolfenstein 3D, the game that put Id Software on the map, to the iPhone.

Read more...

Wednesday, April 8, 2009

Trent Reznor Interview

Variety.com is hosting this excellent interview with Trent Reznor, headman of Nine Inch Nails. It's really interesting to see him discuss the business side of music and how he perceives the reality of the music industry these days. He has been ahead of the curve in terms of mainstream bands and how they distribute their music. He no longer has a label, but rather self releases now, and he discusses the success of their latest album The Slip, which emulated the release strategy of In Rainbows (which, for some reason, is no longer available for download) except they were more honest about the release and took a price-point strategy. Basically, the music is free, but if you want the album, DVD, limited edition stuff that fans might be interested in, then they are available at a modest price. This lowers manufacturing costs, since they know exactly how many units to produce.

Wednesday, April 1, 2009

Movies: I Love You, Nick Cage!

I saw a couple of movies that I thought were surprisingly good. Yesterday my girlfriend and I caught Knowing on $5 night at the Paramount. This was actually exactly the kind of movie I like seeing for $5... somewhat plausible and a few holy shit movies that were genuinely surprising. And there's the magic of Nick Cage - you could tell he was on the verge of mailing this one in, and then he got a little hunch that maybe this movie wouldn't suck if he just put in enough effort to make it to the end. There are some pretty awful scenes at the beginning of the movie, but things really pick up at about the 30 minute mark. Again, for $5, it was a great popcorn flick. Plus, this movie will probably make enough movie that it will continue the Nick Cage leading man phenomenon for another five years. (As an extra, I just read that Knowing was directed by Alex Proyas, who did Dark City. And that explains why this movie didn't suck at all.)

Read more...

Wednesday, March 25, 2009

Podcast Review: Adam Carolla

I'm not a big podcast guy, but I do have a few regulars. I'm a fan of Bill Simmons, the ESPN Sports Guy and probably the most read sports columnist in the US, and I love the CBC Radio: Search Engine podcast, but lately I've found myself regularly tuning in to the digital musings of Adam Carolla.
It seems to me like Hollywood works in posses. I'm not sure if this is a certifiable trend, but the Judd Apatow phenomenon is not unique to Seth Rogan and Co. In fact, this phenomenon might be another instance of viral marketing - or maybe "marketing through networking" is a better term. With Apatow films (and some mimickers, like Zach and Miri Make a Porno,) the same cast of actors each take turns starring and guest starring and making cameos in each other's movies. Even Kevin Smith, with Porno, got an honourary pass into the Apatow guild when he got Rogan and Elizabeth Banks to star in his film. Hollywood loves this because it garantees a certain minimal box office and makes marketing easier for them.

Read more...

WPF: Great Resource

I recently had a severe memory leak issue relating some latent WPF animations - a very innocuous animation in a third-party toolkit was causing a complete memory leak of my user control within my application. What's worse is that our workflow called for us to destroy and re-create the control, and as a result the control was leaking several times over.

I found the solution to this problem on Ramon's weblog. I searched quite a long time for this issue and his blog was the only place where I found the information I needed (including MSDN, of course.) So I wanted to give him a shout-out over here. He lists a few interesting and nasty WPF-related memory leaks that are being worked on by Microsoft.
I'll explain the particular issue I was facing in a future post.

Tuesday, March 24, 2009

WPF: Generalizing using generic.xaml

In my previous post, I went on (and on) about a mechanism for loading generic styles and data templates using a Uri load component scheme. This would load your styles and apply them to the control or window that does the loading.

Since then, I've discovered the somewhat magical and better solution to all this, which is to define a file called \Themes\Generic.xaml in your project root. This apparently tells WPF to tap into its uber styling code and plug in your styles defined here.
I had to dig deeper here, because my templated code was working for my main component, but as soon as I added it to the definition of a floating frame outside of the control (which is a third-party component that I don't have control of) it failed to read the template. Adding the Data template definition to \Themes\Generic.xaml did the trick.

Saturday, March 21, 2009

WPF: Loading Generalized DataTemplates in Code

I've been working WPF quite a bit recently and am discovering the ins and outs of this beast. It's nothing if not ambitious.

I personally like it. A lot. I worked with .NET Forms previously and Win32, and Java Swing before that, and this is far and away the most sophisticated UI framework I have ever seen. However, it's still relatively new and documentation (as well as good examples) can be scarce.
One thing that I've noticed when browsing examples online is that most people present ideas through trivial examples - in fact, they repeat the same information through repeated trivial exercises that can be found on other sites. I'll try to avoid doing that, but that also means that I'm going to assume that you know what you're doing. I'm not going to post tutorials or explain basic WPF concepts that are covered in the all the main WPF resources (see another post in the future.) I'm going to post annoying factoids that have halted progress for me personally at work, and hopefully this will help someone out there.
Let's jump in and take a look at generalized DataTemplates in WPF. By generalized, I'm talking about the DataTemplates that set their type via the DataType property. Sampled from MSDN:
<DataTemplate
  DataType="{x:Type c:GreekGod}">
   <TextBlock Text="{Binding Path=Name}" Foreground="Gold"/>
</DataTemplate>
Like the Style element, DataTemplate uses the DataType property as a way of hooking itself into the general scope of your application. Style's version of this is TargetType.
Now, here's the problem: Where do you specify this DataTemplate? Where does it go in the project? No one seems to make this clear, or provides a satisfactory answer. The real answer is that this DataTemplate needs to exist in a loaded Resources dictionary somewhere up the logical chain of elements where you expect it to be used.
Everyone assumes a trivial layout for a project. "If you need to create a DataTemplate, then just add it to the Window.Resources element, dumbass." However, this is not very helpful, for anyone who is working outside the standard layout conventions (for instance if you're developing WPF controls for a mixed managed application, and blending them into a Win32 frame.) Sometimes, you don't have the benefit of simply adding the template to a higher level component because, wait for it, the XAML might actually be dynamic. Maybe your application reads in XAML to construct a class at runtime. Maybe popping in a DataTemplate definition here is not such a good idea.
So now I'm scratching my head and going back to the root of the problem - how does WPF know about this template in the first place? If I put it in a loose XAML file in my project, and have it compiled, shouldn't the template be properly registered at runtime? Turns out that that isn't the case. Loose XAML does not a loaded component make, which I thought was interesting, and actually makes a lot of sense. This is something that may not be clear from MSDN: Just because you put a DataType property on a DataTemplate in a ResourceDictionary element without a x:Key in a XAML file that is compiled into your project, doesn't mean that it's actually registered with WPF at runtime. It needs to be loaded into the logical chain somewhere.
XAML is cool, but it's still a static definition of components. If you're going to need to sew anything in, you're best bet is to go back to code.
In my case, the answer lay in dynamically loading the XAML and adding it to my root control's MergedDictionaries set (no handy top-level Window, sorry), in the root control's constructor.
To do this, you create a Uri object that points to your project XAML resource (compiled BAML, actually), and use the Application.LoadComponent() static method to load and create the root node of the XAML. If you store your DataTemplate in a ResourceDictionary, the code might look something like this:
public RootControl() {
 InitializeComponent();
 Uri uri = new Uri("MyAssembly;component/Templates.xaml", System.UriKind.Relative);
 Resources.MergedDictionaries.Add((ResourceDictionary) Application.LoadComponent(uri);
 
 // ... continue initialization
}
Take a look at the MSDN Uri class definition for details as to why this syntax works. The above code is written for an assembly called "MyAssembly.dll", and with a XAML file called Templates.xaml in the root of the project. If you place your loose XAML files in subfolders, add the path after "component" in the Uri. "component" is really just the root of your project.
MergedDictionaries is a handy way of importing and aggregating resources from different definitions into a single Resources object. By loading the XAML into the root component's merged dictionary, you are indirectly registering the DataTemplate into the current logical scope, meaning it will be applied to your instantiated data.
A note concerning Merged Dictionaries and performance. If you have many resources and they are merged into a control that instantiated many times over, be aware that each Merged Dictionary is created once per component - you may want to look into ways of statically organizing your resources. I'll probably be looking at this on my side and will share any advice concerning this in future posts.
Of course, if your root class has a XAML definition, then go ahead and use this simpler notation:
<RootControl x:Class="UserControl"
 xmlns="..."
 xmlns:x="..." >
<RootControl.Resources>
 <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
       <ResourceDictionary Source="Templates.xaml"/>
    </ResourceDictionary.MergedDictionaries>
 </ResourceDictionary>
 </RootControl.Resources>
</RootControl>
And that's that. Hopefully this post manages not only to convey how to load templates via code, but also provides a bit of insight into how WPF does things under the hood.

A Note About This Page

I have a reasonably wide variety of interests, among them music, movies, politics, photography, tango, and sports (mostly hockey.) I might use this space to talk about any of those things, but my profession is software design and programming. I was trained as a Software Engineer at Concordia University at the turn of the millennium, and have been working at Autodesk Canada for the past five years in Montreal, Canada. It would be fantastic if people other than programmers read these humble words, but I'm guessing that if anyone stumbles on this page, it'll be a programmer googling some inane or arcane detail concerning WPF or .NET or something (what I currently work with at my job.) Hopefully some of those programmers will have other interests and will stick around for the other things I have to say.

So here goes...

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP