Subscribe to:
Everything
Just Tech

A “Week of WPF” begins on Channel 9 with the announcement that the third major release of WPF (3.5 SP1) has gone beta!

Tim Sneath’s blog entry has fantastic detail about what’s coming in this release and all the download/update details (Silverlighters developing using 2.0 Beta: see caveat below).

Folks attending my “WPF for Line Of Business apps” presentation on Thursday will be happy to hear about the new WPF controls, like the DataGrid, Ribbon and WebBrowser. (Silverlight, it seems, has been a good influence on big brother WPF.) They’ll also value the performance improvements, the new “Client Profile” that can reduce the size of the .NET framework required for deployment, and the little-but-very-useful things like support for string formatting for data-bound text.

Me, I am also utterly stoked about the shader support, the D3DImage, and improvements to WriteableBitmap. Greg Schechter is walking us through the details on his blog.

I am going to check this stuff out this week and promise to demo what I can at my DevTeach presentation on Thursday!

Important caveat: When Visual Studio 2008 SP1 and .NET 3.5 SP1 Betas went live last week, I didn’t update my dev box because the SP1 Beta release is incompatible with Silverlight 2 Beta 1 development. This will be resolved for Silverlight 2 Beta 2, which is imminent - so I’m going to make the update, get a VPC ready for Silverlight development, and start playing with WPF development on my main box now.

I had been merrily using WPF’s built-in support for the Command Pattern for ages (see Commanding Overview, MSDN Docs, and article on implementing the command pattern in WPF, Jeff Druyt)… when suddenly it occured to me that I had no idea what triggered WPF to determine whether or not a command can be executed.

Let me explain by reduction to an absurd example:

Say I have a command that can only execute when

DateTime.Now.Second % 2 == 0.

I construct this command by home-brewing a static RoutedCommand instance:

public static class Commands

{

public static RoutedCommand MyCommand { get { return m_MyCommand; } }

private static RoutedCommand m_MyCommand = new RoutedCommand

(

“Execute My Command”,

typeof(Commands),

new InputGestureCollection()

{

new KeyGesture(Key.C, ModifierKeys.Alt)

}

);

}

And then I add a Command Binding for that command to my Window, and assign the command to a Button:

<Window x:Class=”TestCommands.Window1″…>
<Window.CommandBindings>
<CommandBinding
Command=”{x:Static local:Commands.MyCommand}”
Executed=”MyCommandExecuted”
CanExecute=”MyCommandCanExecute”
/>
</Window.CommandBindings>

<Button Width=”200″ Height=”200″
Command=”{x:Static local:Commands.MyCommand}”
Content=”{Binding Path=IsEnabled}”
/>

</Window>

By nature of WPF’s awesomeness and WPF Commanding in general, the above Button’s IsEnabled property should automatically be set to true or false based on whether or not the command can or can’t be executed.

Speaking of which, let’s set up my Command’s absurd logic in the CodeBehind by implementing its Execute and CanExecute event handlers:

private void MyCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)

{

DateTime now = DateTime.Now;

if (CanExecuteOutput != null)

{

CanExecuteOutput.Text = “MyCommand CanExecute determined at “ +

now.ToLongTimeString() + ” (and “ + now.Millisecond + “ms)”;

}

e.CanExecute = DateTime.Now.Second % 2 == 0;

}

private void MyCommandExecuted(object sender, ExecutedRoutedEventArgs e)

{

TextOutput.Text = “MyCommand executed at “ + DateTime.Now.ToLongTimeString();

}

So, my example is absurd but I bet you see my point by now: WPF is meant to automatically set the IsEnabled Property on that button to true or false, based on the results of the CanExecute method. But in this case, the results of CanExecute are a function only of time, and thus change repeatedly and independently of “obvious” application events. So… how does the Commanding system know when to query CanExecute and consequently enable/disable the button once a second?

In this case, without further intervention, it doesn’t. It seems that when events are raised on the Window (a mouse button click, etc.), CanExecute is re-evaluated. (I don’t know the details and wish I did.)  But, without further programmatic or user intervention, the button will not automatically change its IsEnabled state once a second.

This led me back to the MSDN docs, where I discovered the aptly-named InvalidateRequerySuggested event. To coerce - er, suggest - that WPF should query CanExecute, I set up the following DispatcherTimer:

m_DispatcherTimer = new DispatcherTimer()

{

Interval = TimeSpan.FromSeconds(0.25),

IsEnabled = true

};

m_DispatcherTimer.Tick += delegate

{

CommandManager.InvalidateRequerySuggested();

};

Now, the IsEnabled property of the Button blinks on and off as the Command’s ability to be executed changes with the passing seconds.

Only then did I discover there’s an MSDN Docs sample called “Disable Command Source Via Dispatcher Timer Sample” which is remarkably similar.

There you have it. Now go forth and command WPF’s Commanding. I’m sure you can all execute on that request <g>

P.S. Code for this sample is here.

P.P.S. What are folks using for pasting XAML and C# code into their blogs? This entry is looking a little rough…

The WPF Performance Suite includes the following tools for profiling WPF applications at runtime:

Perforator: for analyzing rendering behavior.

Visual Profiler: for profiling the use of WPF services, such as layout and event handling, by elements in the visual tree.

Working Set Analyzer: for analyzing the working set characteristics of your application.

Event Trace: for analyzing events and generating event log files.

ETW Trace Viewer: Record, display, and browse Event Tracing for Windows (ETW) log files in a WPF user-interface format.

The WPF Performance Suite can either be found online or in your Windows SDK Directory at [WinSDK]\bin (mine was at c:\Program Files\Microsoft SDKs\Windows\v6.1\Bin).

The WPF Performance Suite

This weekend, 10 minutes with the WPF Performance Suite boosted my application’s FPS (frames per second) dramatically.  I had made an incorrect assumption about why my performance was degrading (I thought it was prolific use of texture memory, but it turned out that a textbox related to debug spew that was running wild).  The Visual Profiler revealed this to me immediately.

So, don’t forget the WPF Performance Suite.  It makes a very useful addition to your dev/test arsenal in addition to more traditional static/dynamic analysis tools like those found in Visual Studio Team Suite.

Mike Holmes: 'Make it Right, the first time'Mike Holmes is well on his way to being my new idol.

If you’re reading this from Canada, you know Mike: the contractor who arrives at botched renovation jobs and works his magic.

So why do I love this guy?

1. He knows everything there is to know about construction. He seems to have deep knowledge of every subdomain of home renovation. It’s knowledge (and wisdom) that can only come from years of experience.

2. His mantra is “Make it right [the first time].” Fellow software developers, need I say more?

3. The concept for his show is an entrepreneur’s dream.

Take an existing idea: shows about home renovation, which are a dime a dozen.

Make a clever addition: start on a site where the contractors screwed up.

Suddenly, look at this innovative format you’ve got:

a show with conflict (in the dramatic sense only - Mike has a policy of not naming and shaming)

episodic story arcs that are much more engaging

(most importantly) a useful tweak to the format that gives Mike and his crews ample opportunity to relay relevant information about a reno to the audience, without relying on awkward asides.

More information: Check out the Holmes on Homes Website.

Custom WPF Bitmap Effects, authored in a Managed C++ assembly, complete with sample project to help you roll your own. It works, complete with live preview, in Expression Blend. Done by a guy called Rob who has a blog called Run To The Hills. ‘Nuff said.

Check it out!

p.s. My suggestion to Rob: turn this into a Visual Studio Project Starter Kit. The process is relatively painless and this would be very useful as a Starter Kit!

Update 15 Apr: Although this is a truly valiant attempt, I think at this point you need to heed the caveat Rob adds in his blog: “Although bitmap effects are very costly in terms of rendering in WPF since they force the whole stack to render your control hierarchy in software - the effects aren’t really all that slow and although the advice to avoid bitmap effects if possible still stands, I find judicious use of them acceptable, especially if used on something that is static.”

Expression Blend designI’m currently consulting independently for an incredibly cool team of developers and designers. The process has been a joy, both for me and for my clients. In part, it has shown me that Expression Blend (2.5) has matured to the point where it delivers on the promise of dramatically improving the developer-designer workflow.

I had the good fortune of working with my clients’ lead designer about 5 years ago, on a Managed DirectX project that proceeded very much like “tweak, compile, run.” The current WPF project is, instead, very much “nudge, nudge, nudge.”

Their lead designer, upon seeing his artwork and user interface come alive in Blend, reflected back on the not-quite-as-good old days and said: “Mate, this is ‘the future’.”

How we collaborate

Even though he doesn’t always work with the main branch when adding new content, he routinely authors XAML content (including Storyboards) in side Projects. Us developers then just grab and paste it into the application. And then the designer “nudges” it some more.

This “side Project” technique helps us transfer from designer to developer the burden of enforcing a coding guideline like the Stovell Conventions, and lets designers focus instead on being creative with Blend. (Relevant aside: I really like Paul’s Guidelines, particularly regarding Resource organization).

It’s gone so well that next week I’m introducing the lads to the nerd+art snippets so they can increase their superpowers. (The first time their lead designer wrote a C# event handler himself, he took a screen shot of the C# code and sent it to the rest of the developers just to show off) :)

I have all the Artistic Ability of a Slug

I mentioned that this experience has also been a joy for me as a developer. I feel like I’ve put a creative framework into the hands of someone who can make it work wonders. And it cost me a lot less ($0) than it would have cost me to buy my musical little sister a Baby Grand…

DevTeach LogoI’m in the lineup to speak at the DevTeach conference, which is taking place in Toronto from May 12-15. My presentation is going to be WPF-centric, but I have deliberately left the abstract flexible.

What would you like to hear about WPF at DevTeach from someone who’s been consulting in WPF and Silverlight?

Building a WPF business app end-to-end? WPF Top Tips? My notes and thoughts on collaborating with designers?  Silverlight-WPF crossover?

What would be useful? I want this to be useful. Please let me know.

NLargeBarry’s teaching a course this week and noticed that NLarge didn’t support multimon - or rather, it always zoomed in on the primary monitor. So NLarge got another update. Good thing I don’t sleep!

Changelist:

  • Added Multimonitor support - zooms in on the monitor currently containing the mouse pointer.
  • Added Text support - annotate zoomed-in images with text by using the ‘T’ key

[Update 5 Apr:]

  • v1.1.3 - Fixed a bug in the multimonitor support that caused strange behavior after Hibernate/Restore

Download the update here on Codeplex.

NLargeI immediately wanted to update NLarge again because its new default hotkeys (Ctrl-1 for Zoom, Ctrl-2 to Draw, Ctrl-3 for Break Timer) conflict with Outlook 2007’s hotkeys for Inbox, Calendar, and Contacts.

So NLarge v1.1.1 is out.

Changelist:

  • Added ability to select your own hotkeys for zoom, draw and countdown timer.
  • Added ability to Alt-Tab away from break timer, and Alt-Tab back
  • Added ability to press ‘w’ while zoomed in to clear canvas for drawing.
  • Added a little opacity fade before and after zooms to remove glitching (and for effect).
  • Automatic smoothing of lines sketched on the canvas.

NLargeNLarge is the screen magnification and annotation utility that I use for technical presentations. It magnifies the screen through a smooth animation, and allows you to pan and zoom around the magnified screen. You can annotate the magnified image using the mouse or a Tablet PC pen.
Today I added a few features to NLarge to respond to feature requests, and released version 1.1.

Here are the updated features:

  • Removed glitching from start of zoom (hooray!)
  • Added ability to change drawing pen colour and width
  • Added a countdown timer
  • Changed hotkeys from using Alt to Control to avoid conflict with Office 2007 default hotkeys
  • Moved settings from the registry to a Settings file
  • Updated solution to Visual Studio 2008 (leaving target .NET framework at 3.0)

NLarge is available for download from CodePlex. The code is all available there as well.

I noticed that since I last updated NLarge, its inspiration, ZoomIt, has been updated to provide a smoother zoom-in animation. However, ZoomIt still lacks NLarge’s smooth zoom-out animations, which is a shame, because I find the snap-out jarring and potentially disruptive.  I believe NLarge adds to the professionalism of my presentations by not potentially losing the context of what I’m presenting before or after I draw the audience’s attention to something via a zoom.

If ZoomIt could just zoom smoothly in and out, I’d retire NLarge, but for now, I hope you enjoy v1.1.

Older Posts »