Is there an easy way to use the same conditional compilation symbol that I'm using for my c# code, in my xaml files?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
There is some support for conditional compilation in XAML. It's not the same as in C#, code, though. The trick is to use
AlternateContent
withRequires
against something flagged Ignorable. By doing this, you can actually have portions of your xaml unavailable based on conditions, and turn on or off.I tried the other mentioned solution, and it compiles and works, even though Visual Studio will give you loads of errors, and for me the solution seems to use a lot of time on the UI thread, both of which I don't like.
The best solution I implemented instead was that I put all the conditional logic in the code behind of the control. As you don't mention your intention, this might be what you were looking for.
I wanted to have a conditional compilation symbol affect colors in my application, but you can also imagine the same solution to be used for other differing styles or even templates, or that this can be used with usual if-else logic instead of compilation symbols.
The code-behind for this can manually be created by placing a MainStyle.xaml.cs in your application and use it like this:
Important to note is that if reference only the unset values from my XAML code, and that this also works for
StaticResource
s, although the constructor only gets called once. I guess overwriting / using more of the resource dictionaries methods would also work, but this already solved my problem so I didn't try.