简体   繁体   中英

Custom control using WCF breaks VS2010 designer view

I've designed a custom control for a Silverlight website. When I run the debugger, or release it and pull it up over http, the control works perfectly. In the designer view for the control itself, it displays fine as well.

The problem comes when I add the control to the MainPage.xaml app as a control. The designer breaks saying: And Unhandled exception has occurred. System.NullReferenceException Object reference not set to an instance of an object.

The control pulls its data from a WCF service while it runs its Loaded() event. The WCF service is located on the remote IIS server, not localhost.

I imagine the control is breaking because it can't pull data from the service in designer-view. Is there any way around this problem? Thanks in advance.

You can use the DesignerProperties.IsInDesignTool property. This property has the value true if your code is running in a designer tool such as the VS designer or Expression Blend, and false otherwise.

In your Loaded event handler, you can read the value of this property and only call your WCF service if the value of this property is false .

Aside from what @Luke has said, you should actually fix the core of the problem: your data-loading code THROWS when it fails to load. Maybe just handle the error gracefully and CATCH the error and return empty data? Usually this is better for user experience to show nothing (or show a message) than to show a crash.. Your contorl will now crash also in the real release mode - ie if the data connection fails.

The IsInDesignTool property is stil very handy: once you fail to load the data, before you return empty set of data, check if the IsInDesignTool is true (as Luke said) - if it is, return an hardcoded example set of data. This way, when the contorl is unable to load and when it detects VisualStudio or Blend it will show some testing data and it will show up as filled with contents in the design view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM