【问题标题】:xaml d:DataContext giving "Access is denied" errorxaml d:DataContext 给出“访问被拒绝”错误
【发布时间】:2013-11-12 18:57:42
【问题描述】:

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

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

数据绑定 Intellisense 是我多年来一直想要的东西,所以我试了一下 - 但不幸的是,它在错误列表中返回了一个错误(尽管它仍然可以正常构建)。

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

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

这是列表中的错误:

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

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

有人有什么想法吗?

如果不是,这不是世界末日,因为 Resource Key Intellisense 似乎可以工作,这仍然是一个巨大的好处。

编辑

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

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

我有点困惑,我必须说:

首先,为什么智能感知需要实例化 VM 类,它只需要关心哪些属性可用以及它们是什么类型——所有这些都可以通过反射来检索。

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

我可能不得不做一个老把戏,让 Visual Studio 自己调试自己运行设计器,看看它试图做什么......

进一步编辑

是的,我将 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; }
} 

给予:

方法'_.di14.Templates.SanityTestVM..ctor()' to access method 'System.Collections.Generic.List'1&lt;System.__Canon&gt;..ctor()' failed.尝试的错误14

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

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

【问题讨论】:

  • TemplateEditorCustomVM 是什么样的? (如果它可以简化为复制错误的几个简单属性)。
  • 不幸的是它相当庞大。我会看看是否可以将其减少到导致错误的属性。
  • 你可能会自己弄明白 =D 我试图重现这个问题,但似乎没有解决它。
  • 仍然无法复制问题,不知道这是否相关? stackoverflow.com/questions/13632209/…
  • 这是一个安全问题,我刚刚尝试使用管理员权限运行 VS,但不幸的是没有区别。

标签: c# wpf visual-studio xaml intellisense


【解决方案1】:

看起来这确实是 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&lt;System.__Canon&gt;..ctor()' failed.

【讨论】:

  • 请注意,此问题现已修复 - 来自 Microsoft 的评论:Thank you for using Visual Studio and for reporting this bug. We are happy to let you know that this issue has been fixed in Visual Studio 2013 Update 2. If you already have Visual Studio 2013, you can upgrade to Update 2 for free or you can install a trial version from: http://go.microsoft.com/?linkid=9832436
【解决方案2】:

我不认为“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?

希望对你有帮助。

【讨论】:

  • 嗨,感谢您的回答,但它本身并不是设计时数据的错误 - 我没有使用 Expression Blend。我所追求的是 VS2013 中 xaml 编辑器中的新智能感知功能。
【解决方案3】:

我总是这样使用 d:DataContext

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

一切正常。

【讨论】:

    【解决方案4】:

    设计者试图默认启动构造函数。这样做可能是为了允许虚拟机和其他实体的创建者在设计时启动一些测试数据,否则所有字符串通常都是空的,并且大多数数字为零。

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

    如果你感兴趣的只是智能,你不需要“真实”数据的设计者,你可以使用:

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

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

    if(DesignerProperties.GetIsInDesignMode(new DependencyObject()))
      {
          FillDummyData();
      }
    

    【讨论】:

    • 我尝试设置 IsDesignTimeCreatable=False 但我仍然收到拒绝访问错误。不过,有用的信息要知道:)
    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 2012-12-21
    • 2010-12-23
    相关资源
    最近更新 更多