“条件编译"在 XAML 中? [英] "Conditional Compile" in XAML?

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

问题描述

我们在内部使用了一个程序来测试和验证我们销售的工业控制产品.我们希望能够为某些客户提供缩小版或轻量版.

我们的程序是使用 C# 和 WPF(即 XAML)编写的.使用条件编译功能可以轻松完成只为在我们的工厂中使用而构建的 C# 代码部分,例如,

#if FACTORY//我们的内部代码.........#万一

但是我们如何才能为我们程序的 XAML 部分完成类似的工作?

注意: **有一个关于此主题的 Stack Overflow 问题,其中有人发布了一个带有链接的简洁答案,但该链接似乎与该问题无关.因此,不要将其视为基于此的重复项,因为这不能回答问题.*XAML 条件编译

解决方案

链接的问题/答案中说明的技术可能在某种程度上有助于对 XAML 元素进行条件处理,但我没有认为它会给你你想要的东西.

还有另外两个选项可能更适合您的需求:程序化条件编译和构建时的条件包含.

对于程序化条件编译,您可以在视图背后的代码中使用常规条件编译(最好是因为它是您正在影响的 UI 元素)或在视图模型中(不是那么纯粹,但如果您必须包含,则完全可以接受)它在多层中).此条件编译可用于更改从属性返回的值(通过更改编译并因此执行的行)或通过消除代码块(这很笨重但仍然有效),然后您可以拥有一个 XAML DataTrigger有一个依赖于条件编译代码的表达式.

另一个选项是在 XAML 资源文件中指定控件模板并以编程方式选择它们或使用 MSBuild 属性在 ItemGroup proj 文件中的表达式中,以控制构建中包含哪些文件.结合您的模型/视图模型中的常规编程条件编译,这应该为您的问题提供一个很好的干净解决方案 - 实际上使用此选项您甚至可能不需要编程条件编译.

TemplateSelector 也可能有所帮助,但 IMVHO 有点肮脏.TemplateSelector 应该根据 type 交换模板,但您也可以利用它来包含额外的代码来确定要使用的模板——这可能是包含条件编译代码的好地方.

We have a program we use internally to test and validate an industrial control product we sell. We'd like to be able to offer a scaled-down or lightweight version of this to certain customers.

Our program is written in using C# and WPF (thus XAML). Having sections of C# code that only build for use in our factory is easily done using conditional compile features, e.g.,

#if FACTORY
    // our in-house code . . .  
   . . . 
   . . . 
#endif

But how can we accomplish something similar for the XAML portion of our program?

Note: **There's a Stack Overflow question on this topic where someone posted a terse answer with a link but the link doesn't seem to be related to the question. So don't count this as a duplicate based on that because that doesn't answer the question.* XAML Conditional Compilation

解决方案

The technique illustrated in the linked question/answer may go some way towards giving conditional processing of XAML elements, but I don't think it is going to give you exactly what you are after.

There are another two options that may be better suited to your needs: programmatic conditional compilation and conditional inclusion at build time.

For the programmatic conditional compilation you can use regular conditional compilation in the code behind of your view (preferable as it's a UI element you're affecting) or in the viewmodel (not so pure, but totally acceptable if you have to include it in multiple layers). This conditional compilation can be used to either change what values are returned from properties (by changing which lines are compiled in and therefore executed) or by eliminating blocks of code (this is clunky but still effective), you can then have a XAML DataTrigger that has an expression dependent on the conditionally compiled code.

The other option is to specify control templates in a XAML resource file and either programmatically select them or use a MSBuild property in an ItemGroup expression in your proj file to control which files are included in the build. Combined with regular programmatic conditional compilation in your models/viewmodels this should give you a nice clean solution for your problem - in fact using this option you possibly don't even need the programmatic conditional compilation.

A TemplateSelector may also help, but IMVHO its a bit of a filthy hack. A TemplateSelector is supposed to swap a template based on type, but you can also exploit this to include extra code to determine the template to use - this could be a good spot to include conditionally compiled code.

这篇关于“条件编译"在 XAML 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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