xaml d:DataContext给出“访问被拒绝”错误

时间:2013-11-06 12:45:40

标签: c# wpf visual-studio xaml intellisense

我阅读了以下关于VS2013中可用的新xaml编辑功能的博文:

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

数据绑定智能感知是我多年来一直想要的东西,所以我试了一下 - 但遗憾的是它在错误列表中返回错误(虽然它仍然很好)。

这是我添加到UserControl声明/标签中的内容:

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

这是列表中的错误:

  

错误95拒绝访问:System.Collections.ObjectModel.ObservableCollection'1[_.di1.TemplateEditorCustomVM+TemplateCriteriaVM]'.

我不完全确定它正在尝试做什么,这两个类都被声明为public(主视图模型和嵌套类)。

有人有任何想法吗?

如果不是这不是世界末日,因为资源密钥智能感知似乎仍然是一个巨大的奖励。

修改

好的 - 我将嵌套的类移到公共名称空间中,VS给了我一个更详细的错误:

  

错误64 Attempt by method '_.di1.Templates.TemplateEditorCustomVM..ctor()' to access method 'System.ComponentModel.BindingList'1<System.__Canon>..ctor()'失败。

我有点困惑,我必须说:

首先,为什么intellisense需要实例化VM类,它应该关心的是什么属性可用以及它们是什么类型 - 所有这些都可以通过反射来检索。

其次,我不明白为什么在应用程序启动时运行正常时出错。

我可能不得不做一个让视觉工作室调试本身运行设计师的老技巧,看看它正在尝试做什么......

进一步编辑

是的,我将BindingList属性更改为直接的List属性(因为BindingList来自WinForms方面,所以我认为这可能值得改变以查看它的作用)。但我得到了类似的错误:

  

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

我在System .__ Canon上做了一个快速谷歌,它只是一个优化细节:

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

虽然仍然没有接近发生什么事情,但是我会继续深入挖掘。

编辑 - 现在有一个回购

是的,我开始评论VM的大块以试图从好奇心中找到底层 - 我现在有一个VM类似乎可以重现这个问题:

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; }
} 

给出:

  

错误14按方法'_.di14.Templates.SanityTestVM..ctor()' to access method 'System.Collections.Generic.List'1<System.__Canon>..ctor()' failed.

尝试

看来问题是有一个List,它的类型有一个嵌套类 - 如果它是一个普通类(非嵌套),一切都很好。

我想我需要为此提交一个连接案例 - 在我做之前有人能够确认这一点,我在Windows 8.1机器上有4个版本的VS而我只是想把开发环境排除在外。

4 个答案:

答案 0 :(得分:2)

看起来这确实是VS2013中的一个错误 - 我已经让一些同事看看他们是否可以重现它并且它们可以,所以它不仅仅是我的环境。

我已经提交了一个连接案例 - 所以如果有其他人遇到此错误,请随时投票:

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

问题是由于视图模型包含具有包含嵌套类类型的泛型定义的属性引起的:

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>

给出:

  

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

答案 1 :(得分:0)

我不认为“Type = lTemplates:TemplateEditorCustomVM”是必需的。我以这种方式从XAML页面使用MVVM和datacontext:

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

然后在App.xaml中我有一个静态资源,我可以从任何页面引用:

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

我建议您以这种方式加载DesignTimeData,您可以更好地控制您将在设计器上显示的设计时数据。

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

希望它有所帮助。

答案 2 :(得分:0)

我总是像这样使用d:DataContext

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

一切正常。

答案 3 :(得分:0)

设计人员正在尝试默认启动构造函数。这可能是为了允许VM和其他实体的创建者在设计时启动一些测试数据,否则所有字符串通常都是空的,大多数数字为零。

在您的情况下,似乎其中一个内部属性未初始化(null),并且绑定正试图获取属性的内部成员。

如果您感兴趣的只是整合,并且您不需要设计师使用“真实”数据,您可以使用:

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

如果您想避免上述解决方案并在设计时获得一些测试数据,那么在您的类的构造函数中,您可以拥有:

if(DesignerProperties.GetIsInDesignMode(new DependencyObject()))
  {
      FillDummyData();
  }
相关问题