1

I have had a look at Another Question, however the error I am getting is more specific.

This sample data uses a view model, which does not have a default parameterless constructor, it does have a public parameterised one though.

I believe that design time data uses reflection and should be able to deal with this (as it doesn't actually instantiate the class). Hence it confuses me as to why I get the following error at design time:

The type "AccessDeniedViewModel" does not include any accessible constructors.

The line in my XAML, which includes the design time data is d:DataContext="{d:DesignData /SampleData/AccessDeniedViewModelSampleData.xaml}". I used Blend's 'Create Sample Data from Class...' to generate this data file.

When I open this view in Visual Studio 2010 however, it is fully able to display it along with the design time data.

I do not really want to place parameterless constructors merely for use at design time, any way to get this design data working in Blend would be very much appreciated.

EDIT: I should also mention, the binding support is still there, when I add bindings to the view, the available properties are listed, like it is resolving to the correct type, just unable to instantiate it.

Community
  • 1
  • 1
Lukazoid
  • 19,016
  • 3
  • 62
  • 85

1 Answers1

2

You will need to supply a default constructor. Here is why: Why XML-Serializable class need a parameterless constructor

The only other option I can think of is writing logic in your view's constructor which loads the xaml at design-time so you are able to call the parameterized versions of the constructors.

Community
  • 1
  • 1
Emond
  • 50,210
  • 11
  • 84
  • 115
  • http://www.vladhorby.com/wpblog/2010/05/14/designdata-support-for-silverlight-in-visual-studio-2010-and-blend-4/ provides a sample where a parameterless constructor is not needed. I cannot see why Visual Studio would be able to manage it, but not blend. Thanks for the suggestions though :) – Lukazoid Nov 22 '11 at 15:59
  • So did you test that? Did it work without a parameterless constructor? What is the difference between the page you are referring to and your code? – Emond Nov 22 '11 at 16:09
  • I have applied the steps in that sample to my situation, it works in Visual Studio, however not in Blend. Blend complains about the lack of an accessible constructor. The only difference I can see is that my view model is a derived class. – Lukazoid Nov 22 '11 at 16:15
  • if the base class has no default constructor it might be that the environment cannot handle the deserialization. Have you tried to add it? – Emond Nov 22 '11 at 17:14
  • Hi Erno, I have tried added the default constructor, which gets rid of the errors and means the sample data is displayed correctly. However I had hoped this would not be required. For the time being however, it will have to suffice. I have made the constructor private however, as per your link, thanks for the help :) – Lukazoid Nov 23 '11 at 11:30
  • The Blend designer seems to be geared towards Silverlight while the Visual Studio designer prefers WPF. SL needs a default constructor. I've found that I generally don't trust the VS design surface because it misrepresents your code and is more forgiving than the actual runtime environment. Blend is a bit stricter and more accurate in my experience. – Mike Post Dec 16 '11 at 05:18