加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

为什么WPF应用程序在Windows 7和Windows 8之间看起来不一样,可以

发布时间:2020-12-13 20:19:02 所属栏目:Windows 来源:网络整理
导读:我真的很惊讶,这个问题似乎没有被问到…如果有,但我只是找不到,道歉. 好的,所以我的工作电脑刚刚从Windows 7升级到Windows 8.对于我的绝对恐怖,我的WPF应用程序看起来不同的几种方式…不同的,我的意思是更糟糕,更糟糕的是,控件没有正确排列等.这是一个例子:
我真的很惊讶,这个问题似乎没有被问到…如果有,但我只是找不到,道歉.

好的,所以我的工作电脑刚刚从Windows 7升级到Windows 8.对于我的绝对恐怖,我的WPF应用程序看起来不同的几种方式…不同的,我的意思是更糟糕,更糟糕的是,控件没有正确排列等.这是一个例子:

Windows 7的:

Windows 8:

Windows 8问题(仅从此图像):

>标题栏错误,包括按钮(最小化,关闭等)
>标题栏中的字体大小错误
>标题中的Wright FontWeight(Windows 7 SemiBold设置= Windows 8粗体设置)
>标题栏中的图标(或文字)不对齐
标题栏中的图标非常模糊
>错误的填充和/或边距设置间隔左边的项目
>错误的填充和/或边距设置减少文本框高度在右边
>左侧的项目的“隐藏”默认选择颜色不再隐藏
>回到前面复选框勾选
>某些按钮上的图像非常模糊

所以我的问题是这样的:

为什么WPF应用程序在Windows 7和Windows 8之间看起来不一样,可以修复吗?

为了澄清这一点,我没有在两个操作系统上WPF之间的差异列表.上面列出的各个点也没有修复.我想让某人解释为什么这些UI看起来不同,例如.造成这些差异是什么我也听说过WPF中的一些系统设置,这将使我能够让PC将应用程序呈现在Windows 7上,但我不知道这是多么真实.

UPDATE>>>>

正如@AndrasSeb?所说,有一个名为Windows 7 theme for WPF?的StackOverflow问题,它修复了Windows XP的类似问题.不幸的是,它似乎没有对Windows 8有任何影响.有没有任何微软的用户实际上知道实现什么差异导致这个问题?还是任何人?

UPDATE 2>>>>

好的,所以经过一些更多的测试,我开始认为这个问题与Windows主题无关.使用@ Gusdor的答案中提供的代码,我尝试将主题更改为Aero,没有明显的区别…让我想到了.然后我将其更改为Luna来测试该代码,并且它的工作.

通过“工作”,我的意思是Windows主题改变了,但UI控制,或更准确地说,不正确的填充和保证金保留.然后,我尝试使用@AndrasSeb?提到的XAML方法将主题更改为Luna,同样的事情发生了… ScrollBars看起来不同,所以我可以看到主题已经改变,但问题依然存在.

所以现在我认为这可能与这个事实有关,这是一个全新的电脑,我正在努力…可能有一些dll或设置,我需要安装?我真的只是猜测这里 – 我将整个Microsoft .NET Framework安装到4.5.1版本,就像我在Windows 8.1上一样.

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

好的,不幸的是,没有快速解决这个问题.如果您处于类似情况,并且此处提供的答案也不适用于您,那么这里是为了使Windows 8上的UI显示与Windows上的UI相同的手动需要进行的更改的摘要7.

TextBox:需要将填充添加到默认样式:

<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,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" 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"/>
                    </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的转变一直是一个痛苦而令人不安的经历.我希望这个信息能够帮助别人稍微痛苦一点.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读