【问题标题】:WPF Windows 8 Compatibility IssueWPF Windows 8 兼容性问题
【发布时间】:2014-04-29 16:48:40
【问题描述】:

给定一个按钮(或组合框,或与此相关的任何控件):

<Button x:Name="button" Command="{Binding DoStuff}" Margin="10,0,5,5" Content="Do Stuff!" Style="{StaticResource buttonDefaults}"/>

搭配风格:

<Style x:Key="buttonDefaults" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Style.Resources>
            <ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/>
        </Style.Resources>
        <Setter Property="Background">
            <Setter.Value>
                <RadialGradientBrush>
                    <GradientStop Color="#F4083268" Offset="1"/>
                    <GradientStop Color="#FE5C9247"/>
                </RadialGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="Focusable" Value="False"/>
    </Style>

它在 Windows 8 中看起来与在 Windows 7 中非常不同。

现在,奇怪的是,在 DESIGNER 中,一切在 Windows 8 机器上看起来都很完美,就好像它是 Windows 7...但在运行时,样式并没有“应用”。

另一方面,它在 Windows 7 上的设计器和运行时看起来都很完美。有没有办法解决这个问题?

其他详情:

风格中的RD:

    <Style x:Key="buttonDefaults" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Style.Resources>
            <ResourceDictionary Source="/PresentationFramework.Aero,Version=4.0.0.0,Culture=Neutral,PublicKeyToken=31bf3856ad364e35,processorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml"/>
        </Style.Resources>
        <Setter Property="Background">
            <Setter.Value>
                <RadialGradientBrush>
                    <GradientStop Color="#F4083268" Offset="1"/>
                    <GradientStop Color="#FE5C9247"/>
                </RadialGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="Focusable" Value="False"/>
    </Style>

结果如下:

应用范围内的RD:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/PresentationFramework.Aero,Version=4.0.0.0,Culture=Neutral,PublicKeyToken=31bf3856ad364e35,processorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <Style x:Key="buttonDefaults" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background">
                <Setter.Value>
                    <RadialGradientBrush>
                        <GradientStop Color="#F4083268" Offset="1"/>
                        <GradientStop Color="#FE5C9247"/>
                    </RadialGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontFamily" Value="Arial"/>
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="Focusable" Value="False"/>
        </Style>
    </ResourceDictionary>
</Application.Resources>

结果如下:

我想将第二个的视觉效果应用到单个样式,而不是整个应用程序。

【问题讨论】:

  • 设置覆盖默认样式为真,看看会发生什么:-) HTH
  • 什么也没做,很遗憾。
  • 您是否尝试过使用模板?
  • 我将控制模板作为最后的手段。它会在我的 RD 中创建很多混乱的 xaml。

标签: c# wpf xaml compatibility resourcedictionary


【解决方案1】:

您在自己的风格中加入资源字典PresentationFramework.Aero 是否有实际原因?如果我没记错的话,对于每个版本的 Windows,它可能会有所不同,因为这可能是不同的View。另外我认为在这种情况下,为 Style BasedOn 指定没有多大意义,您也可以尝试将其删除。

有一些提示可以提供帮助:

  • 尝试删除对这个ResourceDictionary的引用

  • 尝试将App.xaml文件中的所有资源字典移出它们所属的位置

  • 为每个Control创建一个ControlTemplate,他的视图对于所有版本的Windows都是“恒定的”

【讨论】:

  • RD 的原因:在 stackoverflow 上显示简洁的代码。该行简单地定义了使用 RD 的样式,它还继续回答您关于 BasedOn 的第二个问题。 BasedOn 之所以存在,是因为它从样式的 RD(在本例中为 W7 的航空主题)中获取其数据。将样式移动到视图的网格中是可行的——但我想将其用作样式,以便可以将其应用于整个应用程序。我会试试 ControlTemplate。
【解决方案2】:

我认为您的问题源于您使用部分程序集名称这一事实。 According to MSDN,当您使用部分程序集名称时,不会在 GAC 中搜索程序集。

在您的其他详细信息更新后进行编辑:

看了你的截图,我终于明白你的新问题了。这个问题与资源的范围有关。您的按钮继承了按钮样式之前,当您在控件内定义样式时添加 aero 覆盖。您可以通过在所需控件顶部添加面板并将其用作资源的范围容器来轻松调整它。更新代码如下:

<Window x:Class="AeroTestSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="191" Width="246">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button Content="Do Stuff!" Padding="10" Margin="10"/>
        <StackPanel>
            <StackPanel.Resources>
                <ResourceDictionary Source="/PresentationFramework.Aero,Version=4.0.0.0,Culture=Neutral,PublicKeyToken=31bf3856ad364e35,processorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml"/>
            </StackPanel.Resources>
            <Button Content="Do Stuff!" Padding="10" Margin="10">
                <Button.Resources>
                    <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
                        <Setter Property="Background">
                            <Setter.Value>
                                <RadialGradientBrush>
                                    <GradientStop Color="#F4083268" Offset="1"/>
                                    <GradientStop Color="#FE5C9247"/>
                                </RadialGradientBrush>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="BorderBrush" Value="Transparent"/>
                        <Setter Property="Foreground" Value="White"/>
                        <Setter Property="FontFamily" Value="Arial"/>
                        <Setter Property="FontSize" Value="48"/>
                        <Setter Property="Focusable" Value="False"/>
                    </Style>
                </Button.Resources>
            </Button>
        </StackPanel>
    </StackPanel>
</Window>

效果截图:

【讨论】:

  • 我收到“Assembly PresentationFramework.Aero ...”这个项目没有引用。我在项目的参考资料中有 PresentationFramework.Aero,我错过了什么?你能提供你的解决方案文件吗?
  • 注意:Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" 工作正常,但完全限定名称给了我上述错误。我抓住了装配信息,它与你的答案相匹配,所以我不确定我错了什么。
  • 我也看到了这个错误,但在我的情况下这是一个 Resharper 错误,我忽略了。该项目运行良好。我将从此代码上传一个新的解决方案,因为我在完成示例后删除了自己的副本:)
  • 我已经将它上传到Github 并确认它可以与.net 4.0/4.5/4.5.1 一起使用即使设计师仍然抱怨没有引用,但你可以确定它是引用的,因为您可以在组件部分看到自动完成功能。即使您不参考 Aero 组件,它仍然可以正常工作,但您会失去智能感知。
  • 啊,这很奇怪。但是仍然存在一个问题——当资源字典的引用在样式本身内时,它不能按预期工作。您提供的解决方案将 RD 作为全局资源。理想情况下,我会避免将资源应用于所有内容,因为在 W7 和 W8 之间只有一些事情搞砸了。
【解决方案3】:

我不确定您是否想要修复 Windows 8 设计模式,或者您想要创建一个在不同操作系统上看起来相同的应用程序。如果是第二个here is a blog post,如何强制 WPF / .NET 在不同的 Windows 版本(例如 XP、7 和 8)上使用相同的样式。

【讨论】:

    【解决方案4】:

    设计者有一个bug,这就是为什么它在Windows 8中显示错误的样式。我认为问题源于Windows 7和8系统主题具有相同名称(aero.normalcolor),而WPF必须手动检查 Windows 8 并改用主题名称 aero2.normalcolor

    至于您的问题,我不会加载整个 (huge) 主题资源字典来设置单个按钮的样式。相反,将按钮样式和模板从 aero.normalcolor.xaml(位于 Blend 安装目录下,Blend\SystemThemes\Wpf\v4.0\aero.normalcolor.xaml)复制到您的资源字典中。 WPF 实际上并不使用操作系统中的任何资源,因此该代码可以在任何操作系统版本下运行。

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      相关资源
      最近更新 更多