views:

328

answers:

4
A: 

Unfortunately, there are problems with the Cider designer in Visual Studio 2008 where this type of error is all too common. The version in VS2010 is vastly improved, but that's no comfort when you hit this issue in VS2008.

Pete OHanlon
Not trying to be contentious, but if you can add a link to a reference that shows this is a Cider error and not something I can fix, I'll accept this answer. If I get enough corroboration in the next day or so, I'll accept this as well.
Klay
A: 

I'm not sure what "Grapher2" is but you might check to make sure that there isn't code in its constructor that can't run correctly when the designer instantiates it. Something like a database call would be problematic.

DancesWithBamboo
Grapher2 is my custom control. Here's a thought: does every custom control need to have its own XAML file? This one doesn't.
Klay
A custom control would not have a XAML file. It would have a theme stored as a ResourceDictionary in generic.xaml file.Do you really want a custom control (you need consumer defined theming)? Or would a user control be sufficient for what you are doing.
DancesWithBamboo
Yeah, I'm doing low-level pixel manipulation of an image in response to mouse movement: http://stackoverflow.com/questions/1487831/wpf-2d-high-performance-graphics. But I have no need at all for consumer- defined theming.
Klay
A: 

I've seen similar issues.

This is only a partial solution as it won't render in the parent in design mode, but it'll get rid of the error. It's the best solution I've been able to find so far.

In the Constructor of your custom control.

if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            InitializeComponent();

Hope that helps, or maybe it'll help you find a better solution. If you do please post it so I can use it ;)

Edit
I get a different error when not using the code above: Could not create an instance of type ''

So may be a different issue then what I've seen, but sounds like it might be related.

Chris Persichetti
My control extends Canvas. But it doesn't recognize InitializeComponent. Could this be an issue?
Klay
I'm fairly new to WPF, but I think you may be on to something. I just tried extending Canvas and it has an InitializeComponent method.
Chris Persichetti
How exactly did you extend Canvas? Did you do Add New Item > Class and type ": Canvas" at the end of your declaration? That's how I did it. Is your class partial?
Klay
I added new item "User Control" and then replaced the : UserControl with : Canvas in both partial files that were created.
Chris Persichetti
Unfortunately there's a very important and non-intuitive difference between a user control and a custom control. The question then becomes: why does a user control that extends Canvas get InitializeComponent when a custom control does not?
Klay
That's the way I've always created custom controls by first creating a User Control then replacying UserControl with the item I want to extend. Here's a link:http://timfanelli.com/item/178 of someone extending Canvas the same way
Chris Persichetti
Custom controls in WPF are not supposed to enforce a particular look. They can have a default template but the user gets to define the UI. You are simply providing functionality. Think of how the Button class works in WPF. Thus a custom control does not have an InitializeComponent because it does not need to find the partial class and run the parser over a XAML file to build a UI.User controls on the other hand are intended to be reusable UI elements including functionality. At least this is how I understand it so far. I think of it like the difference between asp.net custom vs composite.
DancesWithBamboo
+2  A: 

I had a similar issue recently. Basically my understanding is that in designer some things happens in not exactly the same order as during run-time and some things that you would think could never be null actually are null during design-time.

I solved the problem this way: commented large parts of code in my control to the point there was no error in Cider and then uncommented them until I got the error again. Then when source of the error was localized, I surrounded problematic parts with

if (something != null)
{
...
}

Even when I felt that there's no way that could be null. And after some time I got rid of the error.

Not very "scientific" approach but it solved my problem. :)

Alan Mendelevich
I've done exactly what you described before (usually a last ditch effort when all the "real" research turns up nothing). After reading your post, I tried it here, and discovered that I was adding a KeyUp event listener on the control's window in the Initialized event. Apparently the designer also runs the Initialized event--where the parent window was null.Thanks!
Klay