【发布时间】:2020-02-24 08:40:24
【问题描述】:
我有一个需要针对 .NET Framework 3.5 和 4.5 的项目,我想根据构建目标设置 WPF 控件的属性。例如我有一个文本块,如果构建目标是 3.5,我希望它的背景是 Azure,如果构建目标是 4.5,我希望它的背景是青色我该怎么做?
<Window x:Class="WpfAppMultipleTarget.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppMultipleTarget"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="Azure"/> <!-- If target is net framework 3.5 -->
<Setter Property="Background" Value="Cyan"/> <!-- If target is net framework 4.5 -->
</Style>
</Grid.Resources>
<TextBlock>Hello</TextBlock>
</Grid>
【问题讨论】:
-
嗨,欢迎来到 SO。据我了解,您可能无法仅在 XAML 中执行此操作。您需要检查后面代码中的版本并将其绑定到 TextBlock。在这种情况下,可能也不需要定义的样式。如果你觉得这没问题,请更新一下可能能提供帮助的人。
标签: wpf properties multitargeting