2

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.

Community
  • 1
  • 1
Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187
  • I did see that link. I don't know how the link the answerer sent would help me, and he didn't explain it very well. – Mathias Lykkegaard Lorenzen Dec 17 '12 at 14:45
  • I can explain that given idiom of XAML coditionals lies to the ground of a partial class compilation, that is an idea: "if at compile time you provide more details of your class, that will lead to more specialzed implementation of your class". In any way, in XAML you should definetly define ALL possibilities of your XAML usage, so your XAML will be even bigger, which eliminates the idiom of #if #elseif preprocessor definitions. You really will not like the idea to hold all crap for widows phone 7 for EACH control in Windows 8 XAML controls, for example. – Alan Turing Dec 17 '12 at 14:59
  • I only need it in scenario. – Mathias Lykkegaard Lorenzen Dec 17 '12 at 21:19

1 Answers1

1

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).

Alan Turing
  • 2,482
  • 17
  • 20