"Conditional compilation" in XAML?

We have a program that we use to verify and verify the industrial control product that we sell. We would like to be able to offer a shortened or lite version of this for specific customers.

Our program is written using C # and WPF (thus, XAML). The presence of sections of C # code, which are created only for use in our factory, is easily performed using conditional compilation functions, for example,

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

But how can we achieve something similar for the XAML part of our program?

Note: ** This section has a stack overflow question where someone posted a short answer with a link, but the link does not seem to be related to the question. Therefore, do not consider this a duplicate based on this, because it does not answer the question. * conditional compilation of XAML

+5
wpf xaml
source share
3 answers

The method illustrated in the related question / answer may go some way to providing conditional processing of XAML elements, but I don't think it will give you exactly what you need.

There are two more options that may better suit your needs: software conditional compilation and conditional inclusion at build time.

For programmatic conditional compilation, you can use regular conditional compilation in the code behind your view (preferable since it is a user interface element that you touch) or in view mode (not so clean, but quite acceptable if you need to enable this in several layers) . This conditional compilation can be used to change the values ​​returned from the properties (by changing those lines that were compiled and therefore executed) or by excluding blocks of code (this is inconvenient, but still effective), then you can have a XAML DataTrigger, which has an expression depending on conditionally compiled code .

Another option is to specify control templates in the XAML resource file and either select them programmatically or use the MSBuild property in the ItemGroup in the proj file to control which files are included in the assembly. Combined with the usual software conditional compilation in your models / view models, this should give you a good clean solution for your problem - in fact, using this option, you might not even need program conditional compilation.

The Selector pattern can also help, but IMVHO it's a little dirty to crack. The TemplateSelector is supposed to replace the template based on the type, but you can also use it to include additional code to determine which template to use - this may be a good place to include conditionally compiled code.

+2
source share

I would use the XSTL transform, this MSDN article explains how to apply them at compile time. It should be possible to configure the conversion to strip sections inside the tags of the pre-processor ( this SO question , as you can see, shows this), although I probably commented myself so that you would not break the Visual Studio constructor. There's also this SO question that shows how to include processor directives in the form of regular XML tags that you could re-embed in your XAML.

+1
source share
  • Define the FACTORY compiler constant.

  • Create a markup extension class:

     using System; using System.Windows.Markup; namespace Conditional { public class Condition : MarkupExtension { public object MyFactory { get; set; } public object Other { get; set; } public override object ProvideValue(IServiceProvider sp) { #if FACTORY return this.MyFactory; #else return this.Other; #endif } } } 
  • In the XAML markup:

    • Import namespace:
      xmlns: conditional = "clr-namespace: RootNamespace.Conditional"
    
    • Paste this block of code where XAML conditional markup is required:
      <ViewBox>
         <conditional: Condition>
             <conditional: Condition.MyFactory>
                 <TextBlock Text = "This is My Factory" />
             </conditional:Condition.MyFactory>
             <conditional: Condition.Other>
                 <TextBlock Text = "this is other" />
             </conditional:Condition.Other>
         </ conditional: Condition>
     </ViewBox>
    
0
source share

All Articles