1

Yet another strange WPF error:

I have a custom control in a simple XAML page. The project builds and runs perfectly, with no errors.

Here's the XAML:

<Window x:Class="Grapher2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:graph="clr-namespace:Grapher2"
Title="Grapher" Width="800" Height="600">

<StackPanel Name="container"  Width="700" Height="500">
    <graph:GraphCanvas x:Name="graphCanvas" Width="700" Height="500" Background="#ddd" />
</StackPanel>

But when I try to view the XAML page in the XAML designer window, I get the message:

"Problem Loading--The document contains errors that must be fixed..."

When I hit the "Reload the designer" link, an error pops up in the Error List which says:

"Object reference not set to an instance of an object"

and the line starting with "graph:GraphCanvas..." is underlined.

I was originally developing on our development server, and there, everything was the same, except the error was:

"Request for the permission of type 'System.Security.Permissions.SecurityPermission (...blah blah... failed.)"

Again, everything works perfectly, except that I can't view the page in the XAML designer window, which is keeping me from developing the rest of the app.

I've tried cleaning, building, rebuilding, and all combinations of orders of these commands, with no success.

EDIT:

Please bear in mind this is my first custom control, and I could be doing something horribly, horribly wrong. Like I said, the app compiles and works beautifully, so I'm kinda mystified.

EDIT #2:

My derived canvas is not a partial class. From my understanding of how InitializeComponent works (What does InitializeComponent() do, and how does it work in WPF?), there is no auto-generated partial class that contains InitializeComponent linked to my control. Again, the control works perfectly without it, except for breaking the XAML design view.

Community
  • 1
  • 1
Klay
  • 1,901
  • 5
  • 26
  • 40

4 Answers4

3

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
  • 3,531
  • 4
  • 28
  • 49
  • 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 Oct 05 '09 at 13:36
0

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
  • 8,871
  • 2
  • 25
  • 28
  • 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 Oct 01 '09 at 15:45
0

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
  • 4,145
  • 1
  • 14
  • 20
  • 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 Oct 01 '09 at 16:26
  • 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 Oct 01 '09 at 23:48
  • 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 Oct 02 '09 at 13:01
0

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.

Crispy
  • 5,377
  • 3
  • 28
  • 34
  • My control extends Canvas. But it doesn't recognize InitializeComponent. Could this be an issue? – Klay Oct 01 '09 at 16:28
  • 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. – Crispy Oct 01 '09 at 17:20
  • 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 Oct 01 '09 at 17:56
  • I added new item "User Control" and then replaced the : UserControl with : Canvas in both partial files that were created. – Crispy Oct 01 '09 at 19:11
  • 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 Oct 01 '09 at 20:01
  • 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 – Crispy Oct 01 '09 at 21:55
  • 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 Oct 01 '09 at 23:38