Search code examples
c#xamlvisual-studio-2012baml

Compiler arguments with XAML?


Possible Duplicate:
XAML Conditional Compilation

In C#, I can do the following in the code.

#if COMPILING_AS_WINDOWS_PHONE_7
    //some random code
#else
    //some random code
#endif

This allows me to have several projects that compile differently by using the compiler arguments of the projects. In other words, I don't have to reinvent the wheel every time.

However, how can I do this with XAML? I can't seem to find anything anywhere about this online.


Solution

  • You can't control XAML, because XAML should always have auto-generated code behind in .NET, which will be compiled automatically and ignore all prerocessor definitions.

    You can use comments and do this manually with <!-- --> comment symbols

    The only working solution - is completely convert your XAML definitions as fully encoded as C# class, so using pure C# classes into a class control library. Alternatively, you can use managed C++ for control composition.

    The only disadvantage is that will make it hard to develop, maintain, read, and change by hand. The advantage is that you can use one of the code generation tools for XAML.

    And, lastly, using #if #endif you can use one code library / code sources for both windows phone / 8 development, but in my experience, it's better to use partial classes and common code base (you can add a file as reference to the project).