wpf - 为什么 WPF 应用程序在 Windows 7 和 Windows 8 之间看起来不同,这个问题可以修复吗?

标签 wpf windows render difference

我真的很惊讶这个问题似乎还没有被问到......如果有,但我找不到它,抱歉。

好的,所以我的工作计算机刚刚从 Windows 7 升级到 Windows 8。令我非常恐惧的是,我的 WPF 应用程序在几个方面看起来不同......不同,我的意思是更糟,更丑陋,控件未正确排列等。这是一个例子:

Windows 7:

enter image description here

Windows 8:

enter image description here

Windows 8 问题(仅来自这张图片):

  • 包含按钮(最小化、关闭等)的错误标题栏
  • 标题栏中的字体大小错误
  • 标题中的字体粗细错误(Windows 7 半粗体设置 = Windows 8 粗体设置)
  • 标题栏中的图标(或文本)未对齐
  • 标题栏中的图标非常模糊
  • 错误的填充和/或边距设置将左侧的项目隔开
  • 错误的填充和/或边距设置降低了右侧文本框的高度
  • 左侧项目的“隐藏”默认选择颜色不再隐藏
  • 回到前面勾选复选框
  • 一些按钮上的图像非常模糊

所以,我的问题是:

为什么 WPF 应用程序在 Windows 7 和 Windows 8 中看起来不同,这个问题可以修复吗?

为了澄清这一点,我不是在两个操作系统上的 WPF 差异列表之后。我也在修复了上面列出的各个点之后。我希望有人能解释为什么这些 UI 看起来不同,例如。是什么造成了这些差异。我还听说 WPF 中的一些系统设置使我能够使 PC 呈现应用程序就像它在 Windows 7 上一样,但我不知道这有多真实。 p>


更新>>>

正如@AndrasSebö 所指出的,有一个名为 Windows 7 theme for WPF? 的 StackOverflow 问题,它修复了 Windows XP 的类似问题。不幸的是,它似乎对 Windows 8 没有任何影响。是否有任何 Microsoft 用户实际上知道导致此问题的实现差异是什么?或者任何人?


更新 2 >>>

好的,经过更多测试后,我开始认为这个问题与 Windows 主题有关。使用@Gusdor 的回答中提供的代码,我尝试将主题更改为 Aero 并且没有明显的区别......这让我开始思考。然后我将其更改为 Luna 以测试该代码并且它有效。

“有效”是指 Windows 主题已更改,但 UI 控制,或更准确地说,不正确的 PaddingMargin 仍然存在。然后我尝试使用@AndrasSebö 提到的 XAML 方法将主题更改为 Luna,同样的事情发生了......ScrollBar 看起来不同,所以我可以看到主题已更改,但问题仍然存在。

所以现在我在想这可能与我正在使用的全新计算机这一事实有关...可能需要安装一些 dll 或设置?我真的只是在这里猜测 - 我在 Windows 8.1 上安装了整个 Microsoft .NET Framework 到版本 4.5.1。

这绝对是一场噩梦,因为我没有时间去修复这个大型应用程序中的每个 View 。如果可以,请帮忙。

最佳答案

好吧,不幸的是,没有快速解决这个问题的方法。如果您处于类似情况并且此处提供的答案也不适合您,那么这里是我手动需要进行的更改的摘要,以便使 Windows 8 上的 UI 看起来与 Windows 上的 UI 相同7.

TextBox:需要将 Padding 添加到默认 Style:

<Setter Property="Padding" Value="1.5,2" />

ListBoxItem:需要提供新的 ControlTemplate 以隐藏选择和鼠标悬停在背景颜色上:

<Style x:Key="DefaultListBoxItem" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Padding" Value="0" />
    <Setter Property="Margin" Value="2,0,1,0" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="Local" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Grid Background="{TemplateBinding Background}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver" />
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Opacity" Duration="0" To=".55" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="SelectionStates">
                            <VisualState x:Name="Unselected" />
                            <VisualState x:Name="Selected" /> 
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Visibility" Duration="0">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unfocused"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/>
                    <Rectangle x:Name="FocusVisualElement" Fill="{x:Null}" Stroke="{x:Null}" StrokeThickness="0" Visibility="Collapsed" RadiusX="1" RadiusY="1" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

ComboBoxItem:需要提供新的 ControlTemplate 来更改选择和鼠标悬停在背景颜色上:

<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                <Border x:Name="Border" Padding="2" SnapsToDevicePixels="true" Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="#FF47484C" /> <!-- Background mouse over colour -->
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="White" /> <!-- Foreground mouse over colour -->
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled" />
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="SelectionStates">
                            <VisualState x:Name="Unselected" />
                            <VisualState x:Name="Selected">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="#FF47484C" /> <!-- Background selection colour -->
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="White" /> <!-- Foreground selection colour -->
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="SelectedUnfocused">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="Red" />
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentPresenter />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

CheckBox:需要提供新的 ControlTemplate 以在 Bullet 位于 右侧时阻止刻度出现在前面内容(感谢 Fredrik 在 Stack Overflow 上对 Default ControlTemplate for CheckBox 问题的回答:

<SolidColorBrush x:Key="CheckBoxFillNormal" Color="#F4F4F4" />
<SolidColorBrush x:Key="CheckBoxStroke" Color="#8E8F8F" />
<Style x:Key="EmptyCheckBoxFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="1" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="CheckRadioFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="14,0,0,0" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="{x:Type CheckBox}" TargetType="{x:Type CheckBox}">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Background" Value="{StaticResource CheckBoxFillNormal}"/>
    <Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource EmptyCheckBoxFocusVisual}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <BulletDecorator Background="Transparent" SnapsToDevicePixels="true">
                    <BulletDecorator.Bullet>
                        <Aero:BulletChrome BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" IsChecked="{TemplateBinding IsChecked}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"/>
                    </BulletDecorator.Bullet>
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </BulletDecorator>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasContent" Value="true">
                        <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/>
                        <Setter Property="Padding" Value="4,0,0,0"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

要删除可怕的标题栏并显示默认的 Windows 8 标题栏:需要升级到 .NET 4.5 并使用包含的 System.Windows.Controls.Ribbon 命名空间库而不是单独的 'Microsoft Ribbon for WPF' (RibbonControlsLibrary) dll 以前使用过。

不幸的是,我从未发现如何在 Windows 8 上重现 FontWeight 属性的 SemiBold 设置。如果有人知道如何执行此操作,请告诉我。

总的来说,迁移到 Windows 8 是一段痛苦而麻烦的经历。我希望这些信息能以稍微减轻痛苦的方式帮助其他人。

关于wpf - 为什么 WPF 应用程序在 Windows 7 和 Windows 8 之间看起来不同,这个问题可以修复吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21727468/

相关文章:

c# - 绑定(bind)到 "IsExpanded"不适用于 TreeView 中的根节点

c# - WPF中没有WAV和MediaPlayer的音频

c# - 命令绑定(bind) WPF

c# - 为 Windows 编写我自己的自定义命令行 "wrapper"

windows - 在 XP 中如何查看哪些进程/程序正在通过互联网发送/接收数据?

php - 如何在zend框架2或AjaxContext中使用ajax?

java - 线程空指针异常

c# - 将组合框添加到数据网格中的单元格

windows - 我如何在 Perl 中使用 Control D?

ruby-on-rails-3 - 如何在rails中以pdf格式呈现格式