Whip WPF Snippets Into Shape
Learn how to work around some ugly behavior in WPF when relying on the provided code snippets; change the output of provided WPF snippets; resolve dependency issues in Windows Workflow; and drill down on Try… Cast.
by Kathleen Dollard
June 13, 2007
Technology Toolbox: VB .NET, C#
I'm getting a runtime error in my Windows Presentation Foundation (WPF) project that says: "Cannot create instance of 'Window3' defined in assembly 'MyAssemblyName, Version=184.108.40.206, Culture=neutral, PublicKey-Token=null'. Exception has been thrown by the target of an invocation. Error in markup file 'Window3.xaml' Line 1 Position 9."
Window3 is the name of my startup window. Can you tell me what's going wrong?
There is an ugly issue with the WPF compiler that you can encounter if you use the provided snippets. When you inserted your snippet, you changed the type from the default "int" to "string" and made some other changes. But you didn't change the value passed to the UIPropertyMetadata constructor. The snippet provided zero as the only parameter, and this parameter is the default for your dependency property. Because it's accepted by the constructor as an object, the .NET compiler sees no problem. However, when the WPF runtime loads the XAML, it tries to assign the default and encounters a type mismatch. When this occurs only once, you'll probably find the name of the property with the type mismatch deep in a nested exception. However, with multiple dependency properties set to invalid defaults, the inner exception isn't available. This happened in your code, making the problem nearly impossible to track down.
I'd highly recommend that you change the dependency property snippets for C# and VB in both WPF and WF. In WPF, don't pass any parameter by default to the UIProperty-Metadata constructor. It sets the value to Nothing in VB or default in C# (zero for numerics), which is all the dangerous snippet code was doing anyway. In the case of WF, you should clean up the namespace usage so it's consistent, at a minimum. I've included the templates I use for dependency properties in the sample code download (download the code here).
I don't like the output from some of the provided WPF snippets. Can I change them?
There are two tools you'll want to use to manage snippets. The Code Snippet Manager, which you'll find in the Tools menu of Visual Studio, manages how snippets are loaded and the Snippet Editor lets you edit snippets without touching the XML that defines them directly.
Before you can edit snippets, you have to find them. The Snippet Manager searches for snippets in two default locations: <Visual Studio project directory>\Code Snippets\<language>\My Code Snippets and <Program Files>\Microsoft Visual Studio 8\<language>\Snippets. The default snippets are in the second location, and you'll want to make backups first. You can also add additional locations for the Snippet Manager to search. Within all these directories, subdirectories provide category organization that appears in the Snippet Picker you see when you right-click and tell Visual Studio to "Insert Snippet."
The snippet structure is a specialized XML format with a file extension of .snippet. It's a real pain to work with this format, but the VB community saves the day here. Bill McCarthy and the team on GotDotNet workspaces (now MSDN workspaces) created an output-focused snippet-editing tool that features highlighted tags, support for functions like the class name, and support for references and imports. It's a great tool and its use isn't limited to VB (access the Options button to select your development language). You can download this file at http://tinyurl.com/l955n.
Some of the VB snippets delivered by Microsoft fail to load because they have an empty CodeSnippet element at the top of the file. If you can't load a snippet, open it in Notepad, remove this extraneous element, and reload in the Snippet Editor. The default snippets are in a directory under Program Files, so you'll need to run the Snippet Editor as Administrator to edit them.
The Snippet Editor is extremely easy to use. Available snippets appear in a treeview. You'll probably have to add the default WPF snippet path manually by right-clicking on one of the root nodes and adding a path to <Program Files>\Microsoft Visual Studio 8\<language>\Snippets\NetFX30. Tabs at the bottom of the Snippet Editor workspace allow you to manage tokens, imports, references, and detailed information about the snippet, including the snippet's abbreviation. When you return to your code in Visual Studio, you'll find the easiest way to use common snippets is to type this snippet abbreviation and then hit Tab twice (Figure 1).
Neither VB nor C# take advantage of all the functionality in the snippet definition. VB doesn't support functions, so you have to enter the classname of the enclosing class, which is entered automatically in C#. C# ignores the references and imports that you must enter manually if they don't already exist.
When you include your new snippets through Visual Studio's Snippet Manager, be aware that there's a known bug in the Snippet Manager that can cause your snippets to fail to show up immediately. You will eventually see your new or altered snippets if you persevere.
Back to top