Question

I read the following blog post regarding the new xaml editing features available in VS2013:

http://blogs.msdn.com/b/visualstudio/archive/2013/08/09/xaml-editor-improvements-in-visual-studio-2013.aspx

Data binding Intellisense is something I've been wanting for ages, so I gave it a try - but unfortunately it's returning an error in the error list (though it still builds fine).

This is what I've added to my UserControl declaration / tag:

d:DataContext="{d:DesignInstance Type=lTemplates:TemplateEditorCustomVM}"

This is the error in the list:

Error 95 Access is denied: System.Collections.ObjectModel.ObservableCollection'1[_.di1.TemplateEditorCustomVM+TemplateCriteriaVM]'.

I'm not entirely sure what it's attempting to do, both classes are declared as public (main view model and a nested class).

Anyone got any ideas?

If not it's not the end of the world, as Resource Key Intellisense appears to work which is still a huge bonus.

Edit

OK - I moved the nested classes out into the public namespace, and VS has given me a more detailed error:

Error 64 Attempt by method '_.di1.Templates.TemplateEditorCustomVM..ctor()' to access method 'System.ComponentModel.BindingList'1<System.__Canon>..ctor()' failed.

I'm a little confused I must say:

Firstly, why would intellisense need to instantiate the VM class, all it should care about is what properties are available and what type they are - all of which can be retrieved with reflection.

Secondly I don't understand why its erroring when it runs fine when the application is started.

I may have to do the old trick of having visual studio debug itself running the designer to see what it's attempting to do...

further edit

Right, I changed the BindingList properties to straight forward List properties (as the BindingList is from the WinForms side of things so I thought this might be worth changing to see what it does). But I got a similar error:

Error 64 Attempt by method '_.di3.Templates.TemplateEditorCustomVM..ctor()' to access method 'System.Collections.Generic.List'1<System.__Canon>..ctor()' failed.

I did a quick google on System.__Canon and its just a optimization detail :

https://stackoverflow.com/a/16856205/182568

Though still no closer to sussing out whats going on, ah well I'll keep digging further.

edit - now have a repo

Right, I began commenting out huge chunks of the VM to try to get to the bottom of this out of curiosity - and I now have a VM class which appears to reproduce the issue:

public class Nested
{
    public class TestCheck
    {
        public int One { get; set; }
        public int Two { get; set; }
    }
}

public class SanityTestVM
{
    public List<Nested.TestCheck> Test { get; set; }
} 

Gives:

Error 14 Attempt by method '_.di14.Templates.SanityTestVM..ctor()' to access method 'System.Collections.Generic.List'1<System.__Canon>..ctor()' failed.

It appears the issue is having a List which has a nested class for its type - if its a normal class (non nested), everything is fine.

I think I'm going to need to submit a connect case for this - before I do is anyone able to confirm this, I have 4 versions of VS on a Windows 8.1 machine and I just want to rule the development environment out.

Was it helpful?

Solution

Looks like this is indeed a bug in VS2013 - I've asked a few colleagues to see if they can reproduce it and they can, so its not just my environment.

I've submitted a connect case - so if anyone else hits this error feel free to vote it up:

https://connect.microsoft.com/VisualStudio/feedback/details/808323/

The issue is caused by having a view model that contains a property with a generic definition which contains a nested class type:

public class AClass
{
    public class AnotherClass
    {
        public int AProperty { get; set; }
    }
}

class TestVM
{
    public List<AClass.AnotherClass> NestedTypeList { get; set; }
}

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance Type=local:TestVM}"
        Title="MainWindow" Height="354" Width="535">
    <Grid>
    </Grid>
</Window>

Gives:

Error 1 Attempt by method '_.di1.WpfApplication1.TestVM..ctor()' to access method 'System.Collections.Generic.List'1<System.__Canon>..ctor()' failed.

OTHER TIPS

I don't think the "Type=lTemplates:TemplateEditorCustomVM" is neccesary. I use MVVM and datacontext in this way from a XAML page :

<Page.DataContext>
    <Binding Mode="OneWay" Path="YourProperty" Source="{StaticResource ViewModelLocator}"/>
</Page.DataContext>

Then in the App.xaml I have a static resource that I'm able to reference from any page:

 <locator:ViewModelLocator p6:Key="ViewModelLocator" xmlns:p6="http://schemas.microsoft.com/winfx/2006/xaml"/>

I would recommend you to load DesignTimeData in this way, you have more control over the design time data you are going to show on the designer.

How to use CollectionViewSource with design time data in Expression Blend?

hope it helps.

I always use d:DataContext like this

<UserControl ....
            lTemplates="clr-namespace:Your Path"
             d:DataContext="{d:DesignInstance lTemplates:TemplateEditorCustomVM}">
....
</UserControl>

and everything work fine.

The designer is trying to initiate constructors by default. This is probably done to allow the creator of VMs and other entities to initiate some test data at design-time, otherwise all strings are usually empty and most numbers are zero.

In your case, it seems that one of the inner properties is not initialized (null) and the binding is trying to get to an inner member of the property.

If what you're interested in is just the intellisence and you don't need the designer with "real" data, you can use:

d:DataContext="{d:DesignInstance Type=lTemplates:TemplateEditorCustomVM
                                 IsDesignTimeCreatable=False}"

If you'd like to avoid the solution above and have some test data at design-time, in constructors of your classes you can have:

if(DesignerProperties.GetIsInDesignMode(new DependencyObject()))
  {
      FillDummyData();
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top