5

我阅读了以下有关 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

虽然还没有更进一步了解发生了什么,嗯,我会继续深入挖掘。

编辑 - 现在有一个回购

是的,出于好奇,我开始注释掉大量的虚拟机,试图弄清这个问题——我现在有一个虚拟机类,它似乎重现了这个问题:

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.

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

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

4

4 回答 4

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.

于 2013-11-12T10:57:42.873 回答
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,您可以更好地控制要在设计器上显示的设计时间数据。

如何在 Expression Blend 中使用 CollectionViewSource 和设计时数据?

希望能帮助到你。

于 2013-11-09T09:04:02.867 回答
0

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

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

如果您感兴趣的只是智能并且您不需要具有“真实”数据的设计器,您可以使用:

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

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

if(DesignerProperties.GetIsInDesignMode(new DependencyObject()))
  {
      FillDummyData();
  }
于 2013-11-11T22:09:06.393 回答
0

我总是像这样使用 d:DataContext

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

一切正常。

于 2013-11-11T13:58:07.867 回答