Conditional Compilation in XAML
-
I want to use Conditional Compilation Directives (#if, #endif) in my XAML file as well as in the codebehind file. Can anybody provide me links or sample code? Is there any other technique to do the same? Please guide me. ex.
#if TASK
Add Task
#else
Add Contact
#endif -
I want to use Conditional Compilation Directives (#if, #endif) in my XAML file as well as in the codebehind file. Can anybody provide me links or sample code? Is there any other technique to do the same? Please guide me. ex.
#if TASK
Add Task
#else
Add Contact
#endifYou can only use the Conditional Compilation Directives in code behind. As you can see, they are not valid XML, so they are not suitable for use in XAML. From the look of the quick sample you've posted, you might want to use DataTemplates, Template Selectors and the MVVM pattern. Have a look at Josh Smith's article on MSDN to get a better understanding of this.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
You can only use the Conditional Compilation Directives in code behind. As you can see, they are not valid XML, so they are not suitable for use in XAML. From the look of the quick sample you've posted, you might want to use DataTemplates, Template Selectors and the MVVM pattern. Have a look at Josh Smith's article on MSDN to get a better understanding of this.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
I can do better than that - I can give you an idea of how I think, which hopefully means you can perform the same actions as me in future. This should mean that you don't have to wait for others to give you answers, instead you will be able to do your own research, saving you time, and increasing your effectiveness as a developer. First of all, I thought to myself - how can I find the link to this article? I can't remember the URL of this article, but I can think of some mechanism which will allow me to look something up off the internet. So, once I've ascertained that I can't remember the URL directly, I know that I need to look it up from a search engine. At this point, I need to think of the keywords that will help me perform an effective search. Well, we know that the article was written by Josh Smith, we also know it was written for the MSDN magazine and it was about MVVM, so I can make an educated guess at the keywords; namely Josh Smith MVVM MSDN magazine. A quick hop on over to Google and I enter these keywords, hit search and what do you know, the first result is the one I want[^]. When you can think how to search for answers yourself, and not rely on others to do the searching for you, you will be able to make drastic leaps forward in your effectiveness and ability as a developer.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
I can do better than that - I can give you an idea of how I think, which hopefully means you can perform the same actions as me in future. This should mean that you don't have to wait for others to give you answers, instead you will be able to do your own research, saving you time, and increasing your effectiveness as a developer. First of all, I thought to myself - how can I find the link to this article? I can't remember the URL of this article, but I can think of some mechanism which will allow me to look something up off the internet. So, once I've ascertained that I can't remember the URL directly, I know that I need to look it up from a search engine. At this point, I need to think of the keywords that will help me perform an effective search. Well, we know that the article was written by Josh Smith, we also know it was written for the MSDN magazine and it was about MVVM, so I can make an educated guess at the keywords; namely Josh Smith MVVM MSDN magazine. A quick hop on over to Google and I enter these keywords, hit search and what do you know, the first result is the one I want[^]. When you can think how to search for answers yourself, and not rely on others to do the searching for you, you will be able to make drastic leaps forward in your effectiveness and ability as a developer.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Pete O'Hanlon wrote:
Google
Here is an officialy recommended way to share Google links to your friends: http://lmgtfy.com/[^] Yours sincerely - - Google search engine development team ;P
Greetings - Jacek
Jacek Gajek wrote:
Here is an officialy recommended way to share Google links to your friends: http://lmgtfy.com/\[^\]
Give a man a fish, and he'll eat for a day. Teach a man to fish, and he'll buy expensive rods and dodgy waders. The moral of the story here - it's better to be the supplier to the person who teaches a man to fish; offer sufficient kickbacks and you'll never run out of business.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Jacek Gajek wrote:
Here is an officialy recommended way to share Google links to your friends: http://lmgtfy.com/\[^\]
Give a man a fish, and he'll eat for a day. Teach a man to fish, and he'll buy expensive rods and dodgy waders. The moral of the story here - it's better to be the supplier to the person who teaches a man to fish; offer sufficient kickbacks and you'll never run out of business.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
Hey man thanks for your reply. However I googled and got the link before you give. I thought you could have given me the idea after searching Google. So the link could be recorded in your browser history. That could save time for me to search again. However I am not looking for Routed Templates. I am looking for conditional Compilation in XAML, in which I can set the Parameters in the Project file and Include/Exclude some XAML output. I followed the article http://www.removingalldoubt.com/commentview.aspx/defa2a7d-b1e9-49eb-b8c8-438348be8d18[^] and solved the problem. I am posting this so that others can also refer to this solution.
-
Jacek Gajek wrote:
Here is an officialy recommended way to share Google links to your friends: http://lmgtfy.com/\[^\]
Give a man a fish, and he'll eat for a day. Teach a man to fish, and he'll buy expensive rods and dodgy waders. The moral of the story here - it's better to be the supplier to the person who teaches a man to fish; offer sufficient kickbacks and you'll never run out of business.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
Give a man a fish, and he'll eat for a day. Turn a man into a fish, and he'll be eaten for the rest of his life.
Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)