Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preprocessor conditional compilation in XAML

Tags:

c#

wpf

xaml

I've got some code written in C# WPF, and I've got some code for debugging, which I currently compile on or off for debug or release mode. How can I enable or disable UI controls which are currently written in my XAML based on C# preprocessor definitions?

like image 379
Puppy Avatar asked Nov 28 '10 12:11

Puppy


2 Answers

There are no preprocessor-style directives for XAML. However, you could include and exclude XAML files based on the build configuration, providing you with some control. This could provide you with a way of including variations of a file depending on the chosen build configuration. Of course, the downside is that you would have to maintain multiple versions of a file. This could be mitigated through the use of T4 templates so that the different files are auto-generated according to the selected configuration.

like image 42
Jeff Yates Avatar answered Sep 17 '22 17:09

Jeff Yates


You can add some code in the constructor that enables/disables the elements:

public MainWindow()
{
    InitializeComponent();

#if DEBUG
    button1.IsEnabled = false;
#endif
}
like image 144
Pieter van Ginkel Avatar answered Sep 16 '22 17:09

Pieter van Ginkel