【问题标题】:IntelliSense for Data Binding not working - followup用于数据绑定的 IntelliSense 不起作用 - 跟进
【发布时间】:2015-06-07 05:31:27
【问题描述】:

由于对我的previously raised question 的回答,我已经让 IntelliSense for Data Binding 在一个简单的测试应用程序中工作,现在我正在尝试将我学到的知识应用到我正在处理的实际应用程序中。我又遇到了我不明白的问题。我的代码的 sn-p 如下 - 我不得不更改名称以保护专有信息:

<Page x:Class="MyProject.Views.Pages.MyPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="450"
      xmlns:Converters="clr-namespace:MyProject.Converters"
      xmlns:ViewModels="clr-namespace:MyProject.ViewModels"
      Title="My View"
      SnapsToDevicePixels="True" KeepAlive="True" TextOptions.TextFormattingMode="Display">
    <Page.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        <Converters:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
    </Page.Resources>

    <StackPanel d:DataContext="{d:DesignInstance ViewModels:MyViewModel}">
        <!-- ... -->
    </StackPanel>
</Page>

我在&lt;StackPanel d:DataContext="{d:DesignInstance ViewModels:MyViewModel}"&gt; 行收到一条错误消息:

命名空间“clr-namespace:MyProject.ViewModels”中不存在名称“MyViewModel”。

错误没有意义 MyViewModel 确实存在于 MyProject.ViewModels namspace 中。 有什么建议?我已经尝试过干净的重建。

【问题讨论】:

    标签: wpf xaml data-binding visual-studio-2013 intellisense


    【解决方案1】:

    MyProject.ViewModels 命名空间与MyProject.Views.Pages 位于不同的程序集中,似乎有必要将;assembly=MyProject.ViewModels 添加到xmlns:ViewModels="clr-namespace:MyProject.ViewModels 声明中:

    xmlns:ViewModels="clr-namespace:MyProject.ViewModels;assembly=MyProject.ViewModels"
    

    我假设因为项目引用了程序集,所以我不需要指定程序集,就像在 C# 代码文件中使用命名空间时不必指定程序集一样。

    【讨论】:

      【解决方案2】:

      我刚刚遇到同样的错误,而且我之前也遇到过很多次。每次我遇到此错误时,都是因为我有其他错误并且我的代码由于其他原因无法编译。一旦我修复了所有其他错误,我的代码就会编译,VisualStudio 会找到所有 'd:' 缺失的引用......如果它们确实存在的话。

      另外,作为替代方案,如果您使用其默认构造函数实例化您的 ViewModel,我建议使用类似的东西(不使用 'd:'):

      ...
      </Page>
      <Page.DataContext>
          <ViewModels:MyViewModel/>
      </Page.DataContext>
      

      它将永远解决您的问题。

      我还没有尝试过,但也许使用 Roslyn(新的 VS2015 编译器)这个问题会消失。我希望:-)

      【讨论】:

        猜你喜欢
        • 2015-06-06
        • 1970-01-01
        • 2014-12-29
        • 2013-06-03
        • 1970-01-01
        • 1970-01-01
        • 2012-09-02
        • 2016-01-16
        相关资源
        最近更新 更多