C# XAML 条件编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1213576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XAML Conditional Compilation
提问by
Is there an easy way to use the same conditional compilation symbol that I'm using for my c# code, in my xaml files?
有没有一种简单的方法可以在我的 xaml 文件中使用我用于 c# 代码的相同条件编译符号?
回答by Reed Copsey
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
with Requires
against something flagged Ignorable. By doing this, you can actually have portions of your xaml unavailable based on conditions, and turn on or off.
在 XAML 中有一些对条件编译的支持。但是,它与 C# 中的代码不同。诀窍是使用AlternateContent
with 来 Requires
对抗标记为Ignorable 的东西。通过这样做,您实际上可以根据条件使部分 xaml 不可用,并打开或关闭。
回答by Akku
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.
我尝试了其他提到的解决方案,它可以编译并运行,即使 Visual Studio 会给您带来大量错误,而且对我来说,该解决方案似乎在 UI 线程上花费了大量时间,我不喜欢这两种情况。
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.
我想在我的应用程序中使用影响颜色的条件编译符号,但您也可以想象将相同的解决方案用于其他不同的样式甚至模板,或者这可以与通常的 if-else 逻辑一起使用,而不是编译符号。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class ="MyApp.Style.MainStyle">
<!--Version1 -->
<Color x:Key="AbMainColor">#068C00</Color>
<Color x:Key="AbLighterMainColor">#5EBD50</Color>
<Color x:Key="AbDarkerMainColor">DarkGreen</Color>
<Color x:Key="MainColor" />
<Color x:Key="LighterMainColor" />
<Color x:Key="DarkerMainColor" />
<!-- Version2 -->
<Color x:Key="OtherRedColor">#EF0000</Color>
<Color x:Key="LighterRedColor">#e62621</Color>
<Color x:Key="DarkerRedColor">#EF0000</Color>
<SolidColorBrush x:Key="MainBrush" Color="{DynamicResource MainColor}" />
<SolidColorBrush x:Key="LighterMainBrush" Color="{DynamicResource LighterMainColor}" />
<SolidColorBrush x:Key="DarkerMainBrush" Color="{DynamicResource DarkerMainColor}" />
The code-behind for this can manually be created by placing a MainStyle.xaml.cs in your application and use it like this:
可以通过在应用程序中放置 MainStyle.xaml.cs 并像这样使用它来手动创建此代码隐藏:
using System.Windows;
namespace MyApp.Style
{
partial class MainStyle : ResourceDictionary
{
public MainStyle()
{
InitializeComponent();
#if VERSION2
this["MainColor"] = this["OtherRedColor"];
this["LighterMainColor"] = this["LighterRedColor"];
this["DarkerMainColor"] = this["DarkerRedColor"];
#elif VERSION1
this["MainColor"] = this["AbMainColor"];
this["LighterMainColor"] = this["AbLighterMainColor"];
this["DarkerMainColor"] = this["AbDarkerMainColor"];
#endif
}
}
}
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.
需要注意的是,如果只引用我的 XAML 代码中未设置的值,这也适用于StaticResource
s,尽管构造函数只被调用一次。我想覆盖/使用更多的资源字典方法也可以,但这已经解决了我的问题,所以我没有尝试。