如何实现XAML的设计时验证,导致编译错误?

时间:2021-02-03 15:29:19

How to enforce that developers writing XAML in Visual Studio should follow certain standards and validations need to be run and if invalid compile time errors are thrown.

如何强制在Visual Studio中编写XAML的开发人员应该遵循某些标准,并且需要运行验证并且抛出无效的编译时错误。

For example, making sure that all the databinding expressions (some are real long) are written correctly as per 'a custom validation' I would like implement, during design time. Like,

例如,确保所有数据绑定表达式(有些是真正的长)按照我想要在设计时实现的“自定义验证”正确编写。喜欢,

<TextBox Text="{Binding Source={StaticResource CALCULATED}, Converter={StaticResource XPathConverter}, ConverterParameter=@FIRSTNAME_STRING, XPath=@FIRSTNAME}"/>

In the above sample if the binding expression in the Text property is not in that format, there should be a compilation error.

在上面的示例中,如果Text属性中的绑定表达式不是该格式,则应该存在编译错误。

Is there a way to do this?

有没有办法做到这一点?

2 个解决方案

#1


0  

Sorry, the XAML language service can’t be extended this way either. The best way to do this today is to author a build task. I think you can hook it into the MarkupCompilePass1DependsOn target and it will be invoked automatically when the user saves or changes a XAML file. You still have to scan the file redundantly from us, but you don’t have to wait for an actual build to make this work. This is a direct quote from one of the Microsoft architects who currently is working on the WPF designers.

抱歉,XAML语言服务也不能以这种方式扩展。今天做到这一点的最好方法是编写构建任务。我认为你可以将它挂钩到MarkupCompilePass1DependsOn目标,并在用户保存或更改XAML文件时自动调用它。您仍然需要从我们冗余扫描文件,但您不必等待实际构建才能使其工作。这是目前正在研究WPF设计人员的一位Microsoft架构师的直接引用。

#2


2  

There is no built-in way to do this. The best way you will be able to get this result is to run a custom tool on the input. This will require a lot of leg work on your part because it will involve parsing the file yourself but you should be able to get this scenario working.

没有内置的方法来做到这一点。获得此结果的最佳方法是在输入上运行自定义工具。这将需要您的大量工作,因为它将涉及自己解析文件,但您应该能够使这个方案工作。

Example site for creating a custom generator

用于创建自定义生成器的示例站点

http://www.drewnoakes.com/snippets/WritingACustomCodeGeneratorToolForVisualStudio/

#1


0  

Sorry, the XAML language service can’t be extended this way either. The best way to do this today is to author a build task. I think you can hook it into the MarkupCompilePass1DependsOn target and it will be invoked automatically when the user saves or changes a XAML file. You still have to scan the file redundantly from us, but you don’t have to wait for an actual build to make this work. This is a direct quote from one of the Microsoft architects who currently is working on the WPF designers.

抱歉,XAML语言服务也不能以这种方式扩展。今天做到这一点的最好方法是编写构建任务。我认为你可以将它挂钩到MarkupCompilePass1DependsOn目标,并在用户保存或更改XAML文件时自动调用它。您仍然需要从我们冗余扫描文件,但您不必等待实际构建才能使其工作。这是目前正在研究WPF设计人员的一位Microsoft架构师的直接引用。

#2


2  

There is no built-in way to do this. The best way you will be able to get this result is to run a custom tool on the input. This will require a lot of leg work on your part because it will involve parsing the file yourself but you should be able to get this scenario working.

没有内置的方法来做到这一点。获得此结果的最佳方法是在输入上运行自定义工具。这将需要您的大量工作,因为它将涉及自己解析文件,但您应该能够使这个方案工作。

Example site for creating a custom generator

用于创建自定义生成器的示例站点

http://www.drewnoakes.com/snippets/WritingACustomCodeGeneratorToolForVisualStudio/