编译时绑定的检查? [英] Compile time checking of bindings?

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

问题描述

我是中等新MVVM,我了解一些MVVM超过背后XAML简单code(对于虚拟机的可测性的例子)的好处。我们正在使用微卡利进行一些结合但那是一种无关紧要的,我就可以使用特定的捆绑结合{...}和我的问题会或多或少地保持不变。

I am moderately new to MVVM and I understand some of the benefits of MVVM over simple code behind xaml (for example testability of the VM). We are using Caliburn Micro to perform some of the binding but that's kind of irrelevant, I could be using specific binding {binding ...} and my question would more or less remain the same.

据我了解,VM应该知道什么在V的,如果一切都做得正确的逻辑,但似乎因为我们使用动态运行时间在V和VM之间的结合,编译器可以对合法性没有检查每结合前pression。例如,如果我有一个名为UserName的UI组件,然后卡利将尝试绑定,要叫用户名在运行时VM性能(类似于做这样的事情的价值={绑定用户名})。但是,如果我重新命名我的虚拟机属性或拼错了UI组件的名称或结合前pression我们没有得到一个问题的迹象,直到运行时间。

I understand the logic that the VM should know nothing of the V if everything is done correctly, but it seems that because we are using dynamic run time binding between the V and the VM that the compiler can make no checks on the legality of each binding expression. For example if I have a UI component called UserName then Caliburn will try to bind that to a VM property called UserName at run time (similar to doing something like Value="{binding UserName}"). However if I rename my VM property or misspell the UI component name or binding expression we get no indication of a problem until run time.

在我看来,这将是非常好的,能够告诉UI组件或页面将被绑定到特定的接口,以便编译器可以做一些,否则将不得不工作的XAML由测试部门做(和重做)(这是我经常)。

It seems to me that it would be really nice to be able to tell the xaml that a UI component or page is going to be bound to a specific interface so that the compiler can do some of the work that will otherwise have to be done (and redone) by the test department (and that is me quite often).

有没有办法告诉XAML,我们绑定到一个接口或对象类型?有一些工具,可以插入一个XAML应用程序来验证绑定?

Is there a way to tell xaml that we are binding to an interface or object type? Is there some tool that can plug into a xaml application to validate bindings?

推荐答案

虽然编译器不提供对XAML的编译时间检查的支持,你也许可以通过编写实例每个XAML文件的自定义生成任务实现几乎相同的结果经由 XAMLReader 的类,然后用自定义的TraceListener 监听绑定错误。

While the compiler does not provide support for compile time checks of XAML, you can probably achieve much the same result by writing a custom build task that instantiates each XAML file via the XAMLReader class, and then using a custom TraceListener to listen for binding errors.

除此之外,如果你是使用ppared code做你的绑定$ P $,您可以创建直接引用propertynames绑定。类似如下(未经测试)

Beyond that, if you are prepared to do your databinding using code, you can create bindings that reference propertynames directly. Something like the following (untested)

Binding createBinding(Expression<Func<TProperty>> property, object source)
{
    MemberExpression me = property.Body as MemberExpression;
    if (me == null || me.Expression != property.Parameters[0]
          || me.Member.MemberType != MemberTypes.Property) {
        throw new InvalidOperationException(
            "Now tell me about the property");
    }
    Binding b = new Binding(me.Member.Name);
    b.Source = source;
}

// sample code
Binding b = createBinding(()=>this.FontSize, this);
textBlock1.SetBinding(TextBlock.FontSizeProperty, b);

这篇关于编译时绑定的检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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