绑定的静态验证 [英] Static verification of bindings

查看:81
本文介绍了绑定的静态验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能确保您所有的绑定保持正确的?结果
(这是有点长,但忍耐一下,我试图使它尽可能短,我可以)

请看下面的例子:

    <TextBox Name="tb" />
    <TextBlock Text="{Binding Text.TheProp, ElementName=tb}" />

这是在编译时,绑定是不正确完全已知(即解析器知道元素的 TB ,因此,它知道它的类型是<$类型C $ C>文本属性,因此,它知道 TheProp 不存在)。结果
然而,这code编译并运行(尽管在调试输出绑定错误消息)。

It is perfectly known at compile time that the binding is incorrect (i.e. the parser knows the type of element tb, and therefore, it knows the type of it's Text property, and therefore, it knows that TheProp doesn't exist).
Yet, this code will compile and run (although with a binding error message in debug output).

这行为可能会在某些情况下非常方便:无论什么确切类型我的数据是,只要它适当命名的属性,我确定。因此,我们得到某种声明鸭打字。

This behavior may come in very handy in some situations: no matter what exact type my data is, as long as it has appropriately named properties, I'm ok. Thus, we get sort of "declarative duck typing".

然而,鸭打字并不总是一件好事。结果
具体而言,在使用MVVM模式,我知道(大部分时间)的确切类型我所有的ViewModel对象。另外,该机型随着时间的推移越来​​越复杂,这让我担心未来的重构:如果我决定什么重命名一些属性,或者,上帝保佑,把它们放在一个单独的聚合的对象?这是怎么回事我所有的绑定然后会发生什么?请问我的手耙所有XAML文件?即使没有重构 - ?如果我只是做一个印刷工

However, duck typing is not always a good thing.
Specifically, while using the MVVM pattern, I know (most of the time) the exact types of all my ViewModel objects. On the other hand, the models become more and more complex over time, which gets me worried about future refactoring: what if I decide to rename some properties, or, God forbid, put them in a separate aggregated object? What's going to happen with all my bindings then? Will I have to rake all XAML files by hand? And even without refactoring - what if I simply make a typo?

有一个类似的问题已经解决了XAML的其他地方。如果,例如,你在样式/二传手/ @地产把一个不正确的属性名称,你会得到一个编译时错误。结果
TemplateBinding 也提供了这样的验证。这是非常方便的。

A similar problem is already solved in other places of XAML. If, for instance, you put an incorrect property name in Style/Setter/@Property, you get a compile time error.
TemplateBinding also provides such verification. Which is very handy.

所以,理想情况下,我很乐意看到这样的内容:

So, ideally, I would love to see something like this:

ProductViewModel.cs:

    public class ProductViewModel
    {
        public Name { get; set; }
        public Price { get; set; }
    }

ProductView.XAML:

    <UserControl x:Class="Shopping.View.ProductView"
                 x:DataContextType="vm:ProductViewModel"
                 xmlns:vm="clr-namespace:Shopping.ViewModel"
                 ... >
        <TextBox Text="{Binding Name}" />  <!-- OK -->
        <TextBox Text="{Binding Price}" /> <!-- OK -->
        <TextBox Text="{Binding ABC}" />   <!-- Compile time error: there is no property ABC in ProductViewModel -->
    </UserControl>

ShoppingCart.XAML:

    <UserControl x:Class="Shopping.View.ShoppingCartView"
                 x:DataContextType="vm:ShoppingCartViewModel"
                 xmlns:vm="clr-namespace:Shopping.ViewModel"
                 ... >
        <ItemsControl ItemsSource="{Binding Products}"
                      ItemType="vm:ProductViewModel" >  <!-- Static check happens here 
                                                             ShoppingCartViewModel.Products must 
                                                             implement IEnumerable<ProductViewModel> -->
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="vm:ProductViewModel">
                    <view:ProductView /> <!-- DataContext is known to be of correct type
                                              because of DataTemplate.DataType property -->
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </UserControl>

不过,让我们回到现实中来。在现实中,所有的梦想只是不会在近期发生。

But let's get back to reality. In reality, all that dreaming is just not going to happen in the near future.

不过,我相信我不会有这个问题的第一人。结果
所以,最后,问题是:如何才能确保您的绑定是正确的?和他们保持这样的?

However, I am sure I'm not the first person to have this problem.
So, finally, the question is: How do you make sure your bindings are correct? And that they stay that way?

推荐答案

如何XAML中的静态分析作为生成后步骤进行?

How about static analysis of your Xaml performed as a post-build step?

由于.NET 4.0的一部分,微软发布了一个新的 System.Xaml 库来提供强大的XAML解析和序列化支持独立WPF的。他们现在正在开始建立各种有趣的东西在它上面,其中一些可能帮助你。

As part of .Net 4, Microsoft released a new System.Xaml library to provide robust Xaml parsing and serialization support independent of WPF. They are now beginning to build all kinds of interesting things on top of it, some of which may help you out.

XamlToolkit ,例如,你会发现<一个href=\"http://blogs.msdn.com/b/wpf/archive/2010/07/28/microsoft-xaml-toolkit-ctp-july-2010-xaml-dom.aspx\"相对=nofollow> XamlDOM ,让你做的XAML文件方便的静态分析。并采取一个远一点,有<一个href=\"http://blogs.msdn.com/b/wpf/archive/2010/07/28/microsoft-xaml-toolkit-ctp-july-2010-fxcop-integration.aspx\"相对=nofollow>的FxCop规则XAML 。

In the XamlToolkit, for example, you'll find the XamlDOM that enables you to do easy static analysis of Xaml files. And taking that a bit further, there's FxCop rules for XAML.

最感兴趣的是罗布Relyea的 BindingFinder 有型在XAML检查绑定的明确目标。这需要你有你的XAML类型的提示,像数据类型属性在一个DataTemplate,或新的 D:的DataContext属性的在你的浏览次数(的混合使用,以提供设计时的数据)。然后,它使用XamlDOM检查一切相匹配。

Of most interest is Rob Relyea's BindingFinder that has the explicit goal of type checking Bindings in Xaml. This requires that you have type hints in your Xaml, like the DataType attribute in a DataTemplate, or the the new d:DataContext attribute on your Views (which Blend uses to provide design-time data). It then uses the XamlDOM to check that everything matches up.

更新: ReSharper的6 现在提供的智能感知数据绑定,并警告如果你得到你的物业路径错了。

Update: Resharper 6 now provides intellisense for data bindings, and warnings if you get your property paths wrong.

这篇关于绑定的静态验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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