开发者

Preprocessor conditional compilation in XAML

I've got some code written in C# WPF, and I've got some code 开发者_如何学JAVAfor 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?


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

public MainWindow()
{
    InitializeComponent();

#if DEBUG
    button1.IsEnabled = false;
#endif
}


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.


There are two ways to do this. One is using the Preprocessor directives that can mask complete sections of code running it only in a particular build. Or you can use the the Conditional Attribute to easily block out a complete method. http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=420
Here's a description of the difference between the two : http://www.thinkfarahead.com/2007/09/if-debug-vs-conditional.html . You can reference the controls in your code by providing an x:Name attribute in xaml and putting the code to disable the controls in conditional section of your code.

Updated: to be clearer mentioned x:Name attribute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜