IntelliSense for Data Binding不工作 [英] IntelliSense for Data Binding not working

查看:200
本文介绍了IntelliSense for Data Binding不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过几个小时,尝试调试由 Binding 扩展名中的错误属性引起的数据绑定问题。一旦我注意到这个错误,认识到如果IntelliSense可用,我可能没有犯错误。作为错误/错误的名称时用于错误/警告的Visual Studio用户;也许我被宠坏了,但缺乏IntelliSense导致错误。



我做了一些研究,我发现 Intellisense for Data Binding可用于Visual Studio 2013 我正在使用(终极版)。我尝试在博客中的第二个例子之后创建一个简单的WPF应用程序。首先,博客中的第二个示例中出现编译器错误时出现错误。 Type = ViewModel:MainViewModel 属性替换为 d: 修复了编译器错误,但是我的View-Model类的属性仍然没有显示在Intellisense菜单中。我的代码在下面,在 GitHub



MainViewModel.cs:

  using System.ComponentModel; 
使用System.Runtime.CompilerServices;

命名空间IntelliSenseForDataBinding
{
public class MainViewModel:INotifyPropertyChanged
{
public MainViewModel()
{
Greeting =Hello世界;
答案= 42;
}

私人字符串_Greeting;
public string Greeting
{
get {return _Greeting; }
set {_Greeting = value; OnPropertyChanged(); }
}

private int _Answer;
public int Answer
{
get {return _Answer; }
set {_Answer = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler!= null)
handler(this,new PropertyChangedEventArgs(propertyName));
}

}
}

MainWindow。 xaml:

 < Window x:Class =IntelliSenseForDataBinding.MainWindow
xmlns =http:// schemas .microsoft.com / winfx / 2006 / xaml / presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:mc =http:/ $ c
xmlns:d =http://schemas.microsoft.com/expression/blend/2008
mc:Ignorable =dd :DesignHeight =300d:DesignWidth =450
d:DataContext ={d:DesignInstance Type = MainViewModel,IsDesignTimeCreatable = True}
Title =MainWindowHeight =350Width = 525>
< Grid>

< / Grid>
< / Window>

MainWindows.xaml.cs:

 使用System.Windows; 

命名空间IntelliSenseForDataBinding
{
///< summary>
/// MainWindow.xaml的交互逻辑
///< / summary>
public partial class MainWindow:Window
{
public MainWindow()
{
DataContext = new MainViewModel();
InitializeComponent();
}
}
}

这里的证据不是工作:





我希望在IntelliSense菜单中看到一个问候属性的项目。任何关于为什么不在那里的建议?我还尝试将Visual Studio设置重置为默认值,以防万一。



此外,有关阻止或检测Binding属性中的错误属性名称的其他方法的任何建议我在Visual Studio 2013中打开了你的GitHub项目,并且我得到了同样的行为;

解决方案

没有IntelliSense用于绑定。



设计数据是绑定解决方案的关键,它是失败的,所以我建议:


$ b $将您的项目命名空间添加到您的Window元素中: xmlns:local =clr-namespace:IntelliSenseForDataBinding可以帮助
  • 更改您的 d:DataContext 以使用 local 命名空间,而不是 d:键入,实质上提供您要使用的类型的位置: d:DataContext = {d:DesignInstance local:MainViewModel,IsDesignTimeCreatable = True}

  • 清理,构建和测试

  • 证明:


    After a couple of hours trying to debug an issue with data-binding that was caused by a mistyped property in a Binding extension. Once I noticed the mistake, the realization that if IntelliSense was available I may have not made the mistake in the first place. As a Visual Studio user who is used to error/warnings when mistyping a name; perhaps I'm spoiled, but the lack of IntelliSense led to the error.

    I did some research and I found that Intellisense for Data Binding is available is Visual Studio 2013 which I'm using (Ultimate edition). I tried creating a simple WPF app following the second example in the blog. Firstly, There appears to be an error in the second example in the blog that resulted compiler error. Prefixing the Type=ViewModel:MainViewModel attribute with d: fixed the compiler error, yet the properties of my View-Model class are still not showing in the Intellisense menu. My code is below and in GitHub.

    MainViewModel.cs:

    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    
    namespace IntelliSenseForDataBinding
    {
        public class MainViewModel : INotifyPropertyChanged
        {
            public MainViewModel()
            {
                Greeting = "Hello World";
                Answer = 42;
            }
    
            private string _Greeting;
            public string Greeting
            {
                get { return _Greeting; }
                set { _Greeting = value; OnPropertyChanged(); }
            }
    
            private int _Answer;
            public int Answer
            {
                get { return _Answer; }
                set { _Answer = value; OnPropertyChanged(); }
            }
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                    handler(this, new PropertyChangedEventArgs(propertyName));
            }
    
        }
    }
    

    MainWindow.xaml:

    <Window x:Class="IntelliSenseForDataBinding.MainWindow"
            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"
            d:DataContext="{d:DesignInstance Type=MainViewModel, IsDesignTimeCreatable=True}"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
    
        </Grid>
    </Window>
    

    MainWindows.xaml.cs:

    using System.Windows;
    
    namespace IntelliSenseForDataBinding
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                DataContext = new MainViewModel();
                InitializeComponent();
            }
        }
    }
    

    Here's the evidence that is not working:

    I would expect to see an item for the 'Greeting' property in the IntelliSense menu. Any suggestions on why it's not there? I've also tried resetting the Visual Studio settings to default, just in case.

    In addition, any suggestions on additional methods for preventing or detected mistyped property names in Binding attributes?

    解决方案

    I opened your GitHub project in Visual Studio 2013 and I got the same behavior; no IntelliSense for bindings.

    The design data is the key to the binding resolution which is failing, so I recommend this:

    1. Add your project namespace to your Window element: xmlns:local="clr-namespace:IntelliSenseForDataBinding" which can help resolve the location of VM.
    2. Change your d:DataContext to use thelocal namespace instead of d:Type, essentially providing the location of the type you're trying to use: d:DataContext="{d:DesignInstance local:MainViewModel, IsDesignTimeCreatable=True}"
    3. Clean, Build, and Test

    Proof:

    这篇关于IntelliSense for Data Binding不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆