在编译时验证XAML中的数据绑定 [英] Validate data binding in XAML in compile time

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

问题描述

我正在从事基于WPF的应用程序。环境是VS2008 SP1与.NET 3.5 SP 1.
在我们的开发中,我们广泛使用MVVM模式。



即e。应用程序开发人员编写Models和ViewModels(C#),然后UI开发人员将使用WPF Binding(XAML)编写Views。应用程序开发人员还在ViewModels之上编写单元测试。
我们正在使用连续集成方法,我们在每次修改时都进行diond构建和执行单元测试



问题是缺少数据绑定正确性的过程或工具在XAML中验证。
例如:


  1. 应用程序开发者写入属性 NmberOfApples 和单元测试以检查其正确的行为

  2. UI开发人员创建用户控件并将其绑定到属性

  3. 应用程序开发人员发现该属性有拼写错误,并将其名称修改为 NumberOfApples

  4. 任何C#代码中的编译时间错误都使用 NmberOfApples 属性,这样的错误将很容易捕获(Continuous Integration)

  5. XAML文件中的数据绑定不会被验证,它将运行时错误

我的问题将在编译时有帮助我们验证XAML中的数据绑定正确性的任何工具或方法?

解决方案

对于您的问题,请参见本文章



基本思想是创建一个静态(c#)类的ViewModel MetaData集,它们保存ViewModel类的属性的字符串值,然后可以在xaml中使用。本文介绍了如何使用T4文本生成创建这些静态元数据类。您可以使用任何您喜欢的代码生成工具。



所以您的VM具有以下功能:

 命名空间Mine 
{
public class MyViewModel
{
public int MyInt {get; set;}
public string MyString {get; set ;}
}
}

你的代码生成会创建: / p>

 命名空间Mine.MetaData 
{
public static class MyViewModelMetaData
{
public const string MyInt =MyInt;
public const string MyString =MyString;
}
}

然后在您的xaml中,您将添加命名空间您的xaml并将控件绑定到元数据类

 < TextBox Text ={Binding Path = {x:Static Metadata: MyViewModelMetadata.MyInt}}/> 

如果您使用加载项,如 resharper ,那么它会给你静态类的属性的智能感知,也是因为你在静态类中引用一个精确的属性,当静态类被重新生成时xaml不应该编译。



这很漂亮,我觉得很棒,它有机会保持大多数人的理智,但你的里程可能会有所不同。 :)



编辑:



顺便说一下,我不买ViewModels紧密耦合观点。在我看来,意见与他们的ViewModels是密不可分的,但它应该只是一种方式。 ViewModels应该完全独立于任何视图实现。就像ViewModel是接口,View是具体实现的类。所以因为这个原因,我没有把任何WPF特定的属性(例如Visibility枚举)放入我的ViewModel中,因为它绑定我使用WPF永久(这不是一件坏事:) :)但是它损害了维护。


I’m working on WPF based application. Environment is VS2008 SP1 with .NET 3.5 SP 1. In our development we are using MVVM pattern widely.

I.e. application developers write Models and ViewModels (C#), then UI developers will write Views using WPF Binding (XAML). Application developers also write unit tests on top of ViewModels. We are using Continuous Integration methodology and we are diond build and executing unit test on each modification

The problem is a lack of process or tools of data binding correctness validation in XAML. For example:

  1. App developer writes property NmberOfApples and unit tests to check its correct behavior
  2. UI developer creates user control and bind it to the property
  3. App developer finds that property has misspelling and fix its name to NumberOfApples
  4. It would be compilation time errors in any C# code uses NmberOfApples property, and such errors will be easy to catch (Continuous Integration)
  5. Data binding in XAML files are not going to be validated and it will be run time error

My question will be "Is there any tool or methodology that help us validate data binding correctness in XAML in compile time?"

解决方案

A solution to your problem is discussed in this article.

The basic idea is to create a ViewModel MetaData set of static(c#) classes that hold the string value of the properties of your ViewModel classes which you can then use in your xaml. The article explains how to use T4 text generation to create these static metadata classes. You could use any code generation tool of your preference.

so your VM has the following:

namespace Mine
{
  public class MyViewModel
  {
    public int MyInt {get;set;}
   public string MyString {get;set;}  
  }
}

And you code generation would create this:

namespace Mine.MetaData
{
  public static class MyViewModelMetaData
  {
    public const string MyInt = "MyInt";
    public const string MyString = "MyString";
  }
}

and then in your xaml you would add the namespace to your xaml and bind your controls to the metadata class

<TextBox Text="{Binding Path={x:Static Metadata:MyViewModelMetadata.MyInt}}"/>

If you use an add-in like resharper then it will give you intellisense on the properties of the static class and also because you are referencing an exact property in a static class, when the static class gets regenerated your xaml should not compile.

It's pretty slick, I think it's awesome and it has the chance of keeping most people sane, but your mileage may vary. :)

EDIT:

By the way, I don't buy the "ViewModels are tightly coupled to the Views". In my opinion Views are inextricably bound to their ViewModels, but it should only be one way. ViewModels should be completely independent of any view implementation. It's like the ViewModel is the interface and the View is the concrete implemented class. So for this reason I don't put in any WPF-specific properties(e.g. Visibility enumeration) into my ViewModel because that binds me to use WPF for eternity(which isn't really a bad thing :) ), but it compromises maintenance.

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

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