WPF Beta 2 Bug: Image.Source cannot handle mapped drive path

I’m basically going to explode if I don’t get this out soon; I’ve been sitting on this since the weekend. I’d prefer to enter this in the MSDN product feedback center and then link to it from my blog, but, alas, the Beta 2 version is not entered in the Version field of the bug report. I’ve contacted the team through their blog, so while I wait for them to enter it, I’ll blog it here

I expected to find some rough edges in WPF, but nothing quite so rough as this.

Take the following XAML:

<Window x:Class=MusicViewer.Window2

    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation

    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

    Title=MusicViewer Height=300 Width=300

    >

    <Grid>

       <Image Source=E:\Music\Anthrax\folder.jpg />

    </Grid>

</Window>

The analog to this in Windows Forms would be a Form, 300 x 300, with Text set to MusicViewer containing a TableLayoutPanel, itself containing a PictureBox.

The one thing I have to tell you that you cannot get from the code is that E: is a mapped network drive.

Run this code and you get an uncaught exception of type YourXamlISRetardedException, which is essentially meaningless, because there are so many nested exceptions and you have to dig through to get to the real one. The real exception is an ArgumentException stating in its message that only the file URI is supported.

In fact, it’s a little more clear what’s going on if you expand the XAML above into the following:

<Window x:Class=MusicViewer.Window2

    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation

    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

    Title=MusicViewer Height=300 Width=300

    >

    <Grid>

       <Image >

          <Image.Source>

             <BitmapImage>

                <BitmapImage.UriSource>E:\Music\Anthrax\folder.jpg</BitmapImage.UriSource>

             </BitmapImage>

          </Image.Source>

       </Image>

    </Grid>

</Window>

This is probably a more accurate representation to explain the problem. The UriSource property will create a Uri for me with the data provided. Note how succinct XAML can be with the first example. Very sexy.

Anyway, as I pointed out, E: is a mapped network drive. You can also write out the file uri for that jpg as a UNC path; something like \\server\share\music\anthrax\folder.jpg. Pass this format to the UriSource and everything works as expected; I can see the image.

Of course, I can’t discount that it might be me. I’ve tried against two machines with mapped drives and they both fail at the same place. Still, I’m confident it’s not me. I find it very hard to believe, and discouraging if it’s true, that this wasn’t included in a test plan somewhere before a Beta 2 release.

,

ReSharper 2.0 syntax highlighting problem isn’t a problem

I posted yesterday about ReSharper 2.0 shipping.

All through the beta I was having trouble with the syntax highlighting, both in VS 2003 and 2005. I thought it was a beta problem and it would be fixed by the production release.

It wasn’t.

Turns out it’s not a bug, the feature is just off by default. To turn on the glorious highlighting to which you are accustomed in ReSharper 1.5, go to ReSharper->Options… Select the Highlighting section, and check Color identifiers. Et voila!

I found this out in their forum. The guys at JetBrains were very quick to respond to the problems. Thanks to them for the tip.

Now playing: Hoobastank – Born To Lead

ReSharper 2.0 is here!

Super awesome: my favourite Visual Studio Add-in has just been upgraded.

I’ve been waiting a while for this release. The beta, which I’ve been using for the past few weeks, was showing a little roughness. Hopefully, they got rid of that. I’ve been pining for a few refactorings that they have in this release: push member up/pull member down. The error highlighting is still awesome, although they are a little aggressive on the warnings – I prefer prefixing member variables with ‘this’, for example – but they make it super easy to turn off the warning: Alt-Enter, disable warning. Too easy!

Even though there is some overlap with the features of VS 2005, I have to say they went beyond what MS provides, especially with code snippets – Resharper’s are context-aware and will put in what it thinks you want, usually they’re right; and now you can export them!

Some other features I love:

  • the code navigation is awesome – you never have to leave the keyboard. 
  • the code generation is really smart and it goes just as far as it needs to make you smile – for example, when implementing an interface with a property, Resharper will ask if you want to generate a member variable for the property too. Yes, please!
  • the refactorings kick the default Visual Studio refactorings all over the room, so much so that they took over the Refactor menu when you right click.
  • every feature has a keyboard shortcut. It’s totally worthwhile occupying a few brain cells with the short cuts, because they save you so much time.
  • the new preview documentation feature – hit Ctrl-Q over comments and get a preview of what they look like in a tooltip window.

To top it all off, the upgrade from 1.x is free! Go get it now.

Now playing: System Of A Down – Chop Suey

Using the Command Pattern in Windows Forms clients

I’ve been doing a lot of research with the Command pattern lately at work. Here’s what I found: You’re doing yourself a disservice – and your team – if you don’t play the hell outta this thing!

It’s a very powerful design for non-trivial Windows Forms clients. What’s non-trivial? Anything that uses a menu, toolbar or context menu to show perform the same action. In fact it’s so useful and so powerful that I’m a little disappointed that Microsoft didn’t provide something like it in the framework itself. They’ve provided something in Avalon WPF, but I haven’t had the chance to play with it. I know they use it: Visual Studio and Office take advantage of it. (For example, think of how many ways you can copy and paste text.)

What are some of the advantages?

  • Automation – how easy is it to automate tasks in Visual Studio with a macro? Or reassign a keyboard shortcut? Enough said.
  • Completely decouples the UI from the business objects. It’s easy to say this but really hard to do it, especially if deadlines are tight. With commands, you can keep the UI and throw away the business objects or vice versa.
  • Easily allows plugins or addins. Suppose you have a client with a unique look that you want to control, but you want to offer the ability to extend or introduce functionality to third parties. Commands allow that. It’s how Visual Studio Add-ins do it.

There are disadvantages, of course – every design has drawbacks:

  • It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.
  • Using commands complicate your controls and forms. Suppose you have a button that will be used to execute a command. Do you subclass Button so that it contains a Command and override OnClick? Do you use a normal Button and handle the Click event in the form? Change Button with MenuItem. Repeat questions.
  • Debugging is a little harder. There are features in .NET 2.0 to mitigate this one.

So what does a Command look like? Well, that’s largely up to you. The bare minimum would be one method that would allow you to perform the action associated with a Command:

public interface ICommand

{

void Execute();

}

All of your concrete commands would implement this interface and when the button or menu is clicked, call command.Execute(). Easy, eh? There is a whole lot more you can do with the shared interface, however. In fact, there is a lot of common code that you can take advantage of, which is why I prefer an abstract class, like so:

public abstract class Command

{

public string Key { get; protected set; }

public string DisplayName { get; protected set; }

 

public bool Enabled { get; }

 

public abstract void Execute();

}

All the properties would have an implementation, I leave it out because I’m only concerned with API right now. We still have that abstract Execute() method, but now we have a few properties that deserve some explanation. To further separate the UI from the business logic, if the text that the user sees is associated with the command rather than the UI, then you can easily replace the commands without modifying the UI at all. This is extremely powerful when you start dealing with collections of commands that can all be handled in the same way. So that explains DisplayName.

The Enabled property is fairly obvious: there are times when it would either be impossible or just plain wrong to perform an action; in those cases, you want to disable the UI so that the action cannot be performed. I’ll come back to the Enabled property in a second.

The Key property is for when Commands are grouped together in a collection. It makes sense to store most of your commands globally with the main form in a list or map. We’ll need a way to uniquely identify them so that we can retrieve them later. That’s what the Key property is for. Go to Tools -> Options in Visual Studio, select Keyboard. There you’ll see the keys for the commands in Visual Studio, no pun intended.

Now, by itself, the above Command is very powerful. However, we’re not done. Combine the above with Data Binding in Window Forms and you have to write almost no code at all. Data Binding is a very powerful technology, which you’ll know about if you’re one of the millions of ASP.NET developers out there. In .NET 2.0, Windows Forms apps got the same treatment. I can’t cover data binding in detail in this post, so I’ll just assume you know about it. To really take advantage of Data Binding I have to modify my Command class slightly:

public abstract class Command : INotifyPropertyChanged

{

bool enabled;

string key, displayName;

 

public event PropertyChangedEventHandler PropertyChanged;

 

public string Key

{

get { return key; }

protected set { key = value; }

}

 

public string DisplayName

{

get { return displayName; }

protected set

{

if (displayName != value)

{

displayName = value;

OnPropertyChanged(new PropertyChangedEventArgs(“DisplayName”));

}

}

}

 

public bool Enabled

{

get { return enabled; }

set

{

if (enabled != value)

{

enabled = value;

OnPropertyChanged(new PropertyChangedEventArgs(“Enabled”));

}

}

}

 

public abstract void Execute();

 

protected void OnPropertyChanged(PropertyChangedEventArgs e)

{

PropertyChangedEventHandler handler = PropertyChanged;

if (handler != null)

handler(this, e);

}

}

OK, I gave you the whole implementation. You’ll see that my Command class now implements the INotifyPropertyChanged interface. This allows the data binding code to update the control that has bound to the data when the data changes. You’ll note that DisplayName and Enabled will raise the PropertyChanged event. Therefore you can create a Button, hook it up to a Command with a minimum of code and let the power of data binding deal with turning the command on and off:

Button button = new Button();

Command command = new ArbitraryCommand(“My Data”);

button.DataBindings.Add(“Text”, command, “DisplayName”);

button.DataBindings.Add(“Enabled”, command, “Enabled”);

button.Click += delegate { command.Execute(); };

This technique is very powerful when your business objects change on their own without user interaction.

There are few things you have to note. The DisplayName property will have to be internationalized if your app is interested in the rest of the world, which will make the class more complex. Also, the Command pattern kind of falls down when you need to pass data to the command. You lose the polymorphism of the Command class when you have to either know the specific concrete type or what data must be passed to a particular command.

Still, if you are doing serious Windows Forms apps, you should consider the Command pattern.

Technorati tags: , patterns

Now playing: Santana – Just Feel Better (Feat. Steven Tyler Of Aerosmith)

Setting Up A Symbol Server in Visual Studio 2005

Through a serendipitous series of links on the ol’ intarweb, I came upon this excellent post by the always excellent Jessica (aka JFo) on setting up a symbol server in Visual Studio 2003. A symbol server allows you to debug code to which you don’t own the source. I knew you could do this, but never had the need to do it at work (I’m a library author at work). But recently, I’ve been writing my own controls and I find some things are happening that I’m not expecting, so I figured I’d set this up in Visual Studio 2005. If you follow Jessica’s instructions you’ll quickly become confused, like I did: they moved the spot where you configure this.

In Visual Studio 2005, click Tools -> Options. Expand the Debugging node and select the Symbols sub-node. Click the folder icon. Type in the location of the symbols. In my case I want Microsoft’s public symbols; you can find them here: http://msdl.microsoft.com/download/symbols. Type the above as the new entry. For Microsoft’s public symbols, you’ll have to enter a folder location to cache them. You should have something like the following:

Setting up a symbol server in Visual Studio 2005

Once you click OK, you’ll have to agree to a EULA. I used Jessica’s test  from the post above to see if it was working. It wasn’t. The symbols weren’t being loaded by the IDE automatically. Maybe I did it wrong? Went back tried again; checked my settings. Nothing. Hmmm. When in doubt right-click: I found the answer on something I clicked on, I can’t remember what it was.

But I do remember the answer: there’s a setting on the General sub-node of the Debugging node of the Options dialog called Just My Code that is selected by default. That’s why the symbols weren’t loaded. De-select that setting; run Jessica’s sample app and you get the breakpoint. Yay! You’ll also note that the red dot has a + sign in it when the breakpoint is hit, unlike a break point in your code.

Technorati tags:  

Now playing: Three Days Grace – Animal I Have Become